Module: Todo::CLI
Overview
CLI is the module that contains the methods to display the list as well as the methods to parse command line arguments.
Constant Summary collapse
- DATABASE =
The database
Sequel.sqlite Todo::Config[:task_database]
- OPTIONS =
The option flags
{ :is_num => false, :clear_all => false }
Instance Method Summary collapse
-
#commands_parser ⇒ Object
Helper method for parsing commands.
-
#display ⇒ Object
Displays the list in a human readable form:.
-
#option_parser ⇒ Object
Helper method for parsing the options using OptionParser.
-
#parse ⇒ Object
Parses the commands and options.
-
#split(string, width) ⇒ Object
splits string for wrapping.
Instance Method Details
#commands_parser ⇒ Object
Helper method for parsing commands.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/to-do/cli.rb', line 119 def commands_parser if ARGV.count > 0 case ARGV[0] when "display", "d" if Config[:working_list_exists] display else puts "Working List does not exist yet. Please create one" puts "todo create <list name>" end when "create", "switch" if ARGV.count > 0 name = ARGV[1..-1].map{|word| word.capitalize}.join(' ') Config[:working_list_name] = name Config[:working_list_exists] = true puts "Switch to #{name}" puts display else puts "Usage: todo #{ARGV[0]} <listname>" end when "add", "a" if Config[:working_list_exists] ARGV.count > 1 ? Tasks.add(ARGV[1..-1].join(' ')) : puts("Usage: todo add <task name>") puts display else puts "Working List does not exist yet. Please create one" puts "todo create <list name>" end when "finish", "f" if Config[:working_list_exists] ARGV.count > 1 ? Tasks.finish(ARGV[1..-1].join(' '), OPTIONS[:is_num]) : puts("Usage: todo finish <task name>") puts display else puts "Working List does not exist yet. Please create one" puts "todo create <list name>" end when "undo", "u" if Config[:working_list_exists] ARGV.count > 1 ? Tasks.undo(ARGV[1..-1].join(' '), OPTIONS[:is_num]) : puts("Usage: todo undo <task name>") puts display else puts "Working List does not exist yet. Please create one" puts "todo create <list name>" end when "clear" if Config[:working_list_exists] Tasks.clear OPTIONS[:clear_all] puts display else puts "Working List does not exist yet. Please create one" puts "todo create <list name>" end when "remove", "r" if ARGV.count > 1 Tasks.clear true, ARGV[1..-1].map{|word| word.capitalize}.join(' ') else puts "Usage todo remove <list name>" end else puts "Invalid command. See todo -h for help." end else if Config[:working_list_exists] display else puts "Working List does not exist yet. Please create one" puts "todo create <list name>" end end end |
#display ⇒ Object
Displays the list in a human readable form:
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/to-do/cli.rb', line 34 def display tasks = DATABASE[:Tasks].join(:Task_list, :Tasks__id => :Task_list__Task_id).join( :Lists, :Lists__id => :Task_list__List_id).select(:Tasks__Task_number, :Tasks__Name, :Tasks__Completed).filter(:Lists__Name => Config[:working_list_name]) tasks = tasks.order(:Task_number) list = DATABASE[:Lists][:Name=>Config[:working_list_name]] count = list.nil? ? 0 : list[:Total] completed_count = tasks.filter(:Completed=>1).count Config[:width].times do print "*".colorize(:light_red) end puts split_name = split Config[:working_list_name], Config[:width] split_name.each do |line| puts line.center(Config[:width]).colorize(:light_cyan) end Config[:width].times do print "*".colorize(:light_red) end puts puts puts "Todo:".colorize(:light_green) tasks.each do |task| next if task[:Completed] == 1 printf "%4d. ".to_s.colorize(:light_yellow), task[:Task_number] split_v = split task[:Name], Config[:width] - 6 puts split_v[0] split_v.shift split_v.each do |line| printf " %s\n", line end end print "\nCompleted:".colorize(:light_green) printf "%#{Config[:width]+4}s\n", "#{completed_count}/#{count}".colorize(:light_cyan) tasks.each do |task| next if task[:Completed] == 0 printf "%4d. ".to_s.colorize(:light_yellow), task[:Task_number] split_v = split task[:Name], Config[:width]-6 puts split_v[0] split_v.shift split_v.each do |line| printf " %s\n", line end end puts end |
#option_parser ⇒ Object
Helper method for parsing the options using OptionParser
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/to-do/cli.rb', line 82 def option_parser OptionParser.new do |opts| version_path = File.("../../VERSION", File.dirname(__FILE__)) opts.version = File.exist?(version_path) ? File.read(version_path) : "" opts. = "Usage: todo [COMMAND] [option] [arguments]" opts.separator "Commands:" opts.separator " create, switch <list name> creates a new list or switches to an existing one" opts.separator " <blank>, display, d displays the current list" opts.separator " add, a <task> adds the task to the current list" opts.separator " finish, f [option] <task> marks the task as completed" opts.separator " undo, u [option] <task> undos a completed task" opts.separator " clear [option] clears completed tasks" opts.separator " remove, rm <list name> removes the list completely (cannot undo)" opts.separator "Options: " opts.on('-n', 'with finish or undo, references a task by its number') do OPTIONS[:is_num] = true end opts.on('-a', 'with clear, resets the entire list') do OPTIONS[:clear_all] = true end opts.on('-h', '--help', 'displays this screen' ) do puts opts exit end opts.on('-w', "displays the name of the current list") do if Config[:working_list_exists] puts "Working list is #{Config[:working_list_name]}" else puts "Working List does not exist yet. Please create one" puts "todo create <list name>" end exit end end end |
#parse ⇒ Object
Parses the commands and options
196 197 198 199 200 201 202 203 204 |
# File 'lib/to-do/cli.rb', line 196 def parse optparse = option_parser begin optparse.parse! commands_parser rescue OptionParser::InvalidOption => e puts "#{e}. See todo -h for help." end end |
#split(string, width) ⇒ Object
splits string for wrapping
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/to-do/cli.rb', line 207 def split string, width split = Array.new if string.length > width #if the string needs to be split string_words = string.split(" ") line = "" string_words.each do |x| if x.length > width #if the word needs to be split #add the start of the word onto the first line (even if it has already started) while line.length < width line += x[0] x = x[1..-1] end split << line #split the rest of the word up onto new lines split_word = x.scan(%r[.{1,#{width}}]) split_word[0..-2].each do |word| split << word end line = split_word.last+" " elsif (line + x).length > width-1 #if the word would fit alone on its own line split << line.chomp line = x else #if the word can be added to this line line += x + " " end end split << line else #if the string doesn't need to be split split = [string] end #give back the split line return split end |