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.
Instance Method Summary collapse
-
#command_switch ⇒ Object
Helper method for parsing commmands.
-
#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.
Instance Method Details
#command_switch ⇒ Object
Helper method for parsing commmands.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/to-do/cli.rb', line 93 def command_switch if ARGV.count > 1 should_display = true case ARGV[0] when "create", "switch" name = ARGV[1..-1].map{|word| word.capitalize}.join(' ') Config[:working_list_name] = name Config[:working_list_exists] = true puts "Switch to #{name}" when "add", "a" Tasks.add(ARGV[1..-1].join(' '), Helpers::CLI::OPTIONS[:priority]) when "finish", "f" Tasks.finish(ARGV[1..-1].join(' '), Helpers::CLI::OPTIONS[:is_num]) when "undo", "u" Tasks.undo(ARGV[1..-1].join(' '), Helpers::CLI::OPTIONS[:is_num]) when "remove", "r" Tasks.clear true, ARGV[1..-1].map{|word| word.capitalize}.join(' ') should_display = false when "set", "s" if Helpers::CLI::OPTIONS[:change_priority] Tasks.set_priority Helpers::CLI::OPTIONS[:priority], ARGV[1..-1].join(' '), OPTIONS[:is_num] end else puts "Invalid command. See todo -h for help." should_display = false end end should_display end |
#commands_parser ⇒ Object
Helper method for parsing commands.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/to-do/cli.rb', line 70 def commands_parser if ARGV.count > 0 should_display = command_switch if ARGV[0] == "clear" Tasks.clear Helpers::CLI::OPTIONS[:clear_all] should_display = true end if ARGV[0] == "display" || ARGV[0] == "d" || should_display puts display elsif Helpers::CLI::USAGE[ARGV[0].to_sym].nil? && ARGV.count == 1 puts "Invalid command. See todo -h for help." elsif ARGV.count == 1 puts "Usage: #{Helpers::CLI::USAGE[ARGV[0].to_sym]}" end else display end end |
#display ⇒ Object
Displays the list in a human readable form:
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/to-do/cli.rb', line 25 def display colors = Helpers::CLI.create_color_hash tasks = Helpers.task_names tasks = Helpers::CLI::OPTIONS[:sort] == "n" ? tasks.order(:Task_number) : tasks.order(:Priority, :Task_number) list = Helpers::DATABASE[:Lists][:Name=>Config[:working_list_name]] count = list.nil? ? 0 : list[:Total] completed_count = tasks.filter(:Completed=>1).count #print out the header Helpers::CLI.print_header colors puts puts #prints out incomplete tasks puts "Todo:".colorize(colors[:green]) Helpers::CLI.print_tasks 1, tasks, colors #Prints out complete tasks print "\nCompleted:".colorize(colors[:green]) printf "%#{Config[:width]+4}s\n", "#{completed_count}/#{count}".colorize(colors[:cyan]) Helpers::CLI.print_tasks 0, tasks, colors puts end |
#option_parser ⇒ Object
Helper method for parsing the options using OptionParser
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/to-do/cli.rb', line 52 def option_parser colors = Helpers::CLI.create_color_hash OptionParser.new do |opts| Helpers::CLI. opts, colors Helpers::CLI. opts, colors Helpers::CLI. opts, colors Helpers::CLI. opts, colors Helpers::CLI. opts, colors Helpers::CLI. opts, colors Helpers::CLI. opts, colors Helpers::CLI. opts, colors Helpers::CLI. opts, colors Helpers::CLI. opts, colors Helpers::CLI. opts, colors end end |
#parse ⇒ Object
Parses the commands and options
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/to-do/cli.rb', line 124 def parse optparse = option_parser begin optparse.parse! commands_parser rescue OptionParser::InvalidOption, OptionParser::InvalidArgument, OptionParser::MissingArgument, OptionParser::NeedlessArgument => e puts "#{e.to_s. capitalize}. See todo -h for help." end end |