Module: Todo::Helpers::CLI

Extended by:
CLI
Included in:
CLI
Defined in:
lib/to-do/helpers/helpers_CLI.rb

Overview

Helper methods used in the Todo::CLI module

Constant Summary collapse

OPTIONS =

The option flags

{
  :is_num => false, 
  :clear_all => false,
  :change_priority => false,
  :priority => 1, 
  :sort => "n"
}
USAGE =

Usage messages for each of the commnands

{
  :default => "todo [COMMAND] [option] [arguments]", 
  :create  => "todo create|switch <LIST NAME>", 
  :display => "todo [display|d] [-s {p,n}]",
  :add => "todo add|a [-p {high, medium, low}] <TASK>", 
  :finish => "todo finish|f [-n <TASK_NUMBER>] [<TASK>]",
  :undo => "todo undo|u [-n <TASK_NUMBER>] [<TASK>]",
  :clear => "todo clear [-a]",
  :remove => "todo remove|rm <LIST NAME>",
  :set => "todo set|s [-p {high, medium, low}] [-n <TASK_NUMBER>] [<TASK>]", 
  :config => "todo [--color-scheme {light, dark, none}] [--width <WIDTH_NUMBER>]"
}

Instance Method Summary collapse

Instance Method Details

#create_color_hashObject

create a hash of the colors needed for display based on the config file



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/to-do/helpers/helpers_CLI.rb', line 70

def create_color_hash
  colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, :default]
  type = Config[:color]
  color_hash = Hash.new
  colors.each do |c|
    case type
    when "light"
      color_hash[c] = c
    when  "dark"
      color_hash[c] = ("light_" + c.to_s).to_sym
    else
      color_hash[c] = :default
    end
  end
  color_hash
end

#options_add(opts, colors) ⇒ Object

Helper method for the options parser that for add

Parameters:

  • opts

    the options switch

  • colors (Hash)

    the color hash



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/to-do/helpers/helpers_CLI.rb', line 129

def options_add opts, colors
  opts.separator ""
  opts.separator "  *".colorize(colors[:cyan])  + " add, a".colorize(colors[:yellow]) +  
  " adds the task to the current list".colorize(colors[:magenta])
  opts.separator "    usage: ".colorize(colors[:cyan]) + USAGE[:add].colorize(colors[:red])
  opts.on('-p PRIORITY', ["high", "medium", "low"], 'set the priority of the task to one of the', 'following. Default is medium') do |p|
    priorities = {
      "high" => 0,
      "medium" => 1,
      "low" => 2
    }
    OPTIONS[:change_priority] = true
    OPTIONS[:priority] = priorities[p]
  end
end

#options_clear(opts, colors) ⇒ Object

Helper method for the options parser for clear

Parameters:

  • opts

    the options switch

  • colors (Hash)

    the color hash



175
176
177
178
179
180
181
182
183
# File 'lib/to-do/helpers/helpers_CLI.rb', line 175

def options_clear opts, colors
  opts.separator ""
  opts.separator "  *".colorize(colors[:cyan])  + " clear".colorize(colors[:yellow]) +  
  " clears a completed tasks".colorize(colors[:magenta])
  opts.separator "    usage: ".colorize(colors[:cyan]) + USAGE[:clear].colorize(colors[:red])
  opts.on('-a', 'resets the entire list') do
    OPTIONS[:clear_all] = true
  end
end

#options_config(opts, colors) ⇒ Object

Helper method for the options parser for help and display working list

Parameters:

  • opts

    the options switch

  • colors (Hash)

    the color hash



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/to-do/helpers/helpers_CLI.rb', line 234

def options_config opts, colors
  opts.separator ""
  opts.separator "Configuration Options: ".colorize(colors[:green])
  opts.separator "    usage: ".colorize(colors[:cyan]) + USAGE[:config].colorize(colors[:red])

  #todo -h
  opts.on('--color-scheme SCHEME', ["light", "dark", "none"],  "State whether you are using a light" , 
    "scheme or dark scheme.  This is used","for the text colors.  If none work",  "with your current 
    color scheme,", "you can turn it off.  Default is light." ) do |scheme|
    Config[:color] = scheme
    exit
  end
  #todo -w
  opts.on('--width WIDTH', Integer, "Changes the width for formatting") do |width|
    Config[:width] = width
    exit
  end
end

#options_create(opts, colors) ⇒ Object

Helper method for the options parser for create, switch

Parameters:

  • opts

    the options switch

  • colors (Hash)

    the color hash



104
105
106
107
108
109
# File 'lib/to-do/helpers/helpers_CLI.rb', line 104

def options_create opts, colors
  #todo create, switch
  opts.separator "  *".colorize(colors[:cyan])  + " create, switch".colorize(colors[:yellow]) +  
  " creates a new list or switches to an existing one".colorize(colors[:magenta])
  opts.separator "    usage: ".colorize(colors[:cyan]) + USAGE[:create].colorize(colors[:red])
end

#options_display(opts, colors) ⇒ Object

Helper method for the options parser for display

Parameters:

  • opts

    the options switch

  • colors (Hash)

    the color hash



115
116
117
118
119
120
121
122
123
# File 'lib/to-do/helpers/helpers_CLI.rb', line 115

def options_display opts, colors
  opts.separator ""
  opts.separator "  *".colorize(colors[:cyan])  + " display, d".colorize(colors[:yellow]) +  
  " displays the current list".colorize(colors[:magenta])
  opts.separator "    usage: ".colorize(colors[:cyan]) + USAGE[:display].colorize(colors[:red])
  opts.on('-s TYPE', [:p, :n], "sorts the task by Type") do |s|
    OPTIONS[:sort] = s
  end
end

#options_finish(opts, colors) ⇒ Object

Helper method for the options parser that for finish

Parameters:

  • opts

    the options switch

  • colors (Hash)

    the color hash



149
150
151
152
153
154
155
156
157
# File 'lib/to-do/helpers/helpers_CLI.rb', line 149

def options_finish opts, colors
  opts.separator ""
  opts.separator "  *".colorize(colors[:cyan])  + " finish , f".colorize(colors[:yellow]) +  
  " marks a task as finished".colorize(colors[:magenta])
  opts.separator "    usage: ".colorize(colors[:cyan]) + USAGE[:finish].colorize(colors[:red])
  opts.on('-n',  'references a task by its number') do |n|
    OPTIONS[:is_num] = true
  end
end

#options_other(opts, colors) ⇒ Object

Helper method for the options parser for help and display working list

Parameters:

  • opts

    the options switch

  • colors (Hash)

    the color hash



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/to-do/helpers/helpers_CLI.rb', line 214

def options_other opts, colors
  opts.separator ""
  opts.separator "Other Options: ".colorize(colors[:green])

  #todo -h
  opts.on('-h', '--help', 'displays this screen' ) do
    puts opts
    exit
  end
  #todo -w
  opts.on('-w', "displays the name of the current list") do
    puts "Working list is #{Config[:working_list_name]}"
    exit
  end
end

#options_remove(opts, colors) ⇒ Object

Helper method for the options parser for remove

Parameters:

  • opts

    the options switch

  • colors (Hash)

    the color hash



189
190
191
192
193
194
# File 'lib/to-do/helpers/helpers_CLI.rb', line 189

def options_remove opts, colors
  opts.separator ""
  opts.separator "  *".colorize(colors[:cyan])  + " remove, rm".colorize(colors[:yellow]) +  
  " removes the list completely.".colorize(colors[:magenta])
  opts.separator "    usage: ".colorize(colors[:cyan]) + USAGE[:remove].colorize(colors[:red])
end

#options_set(opts, colors) ⇒ Object

Helper method for the options parser that for set

Parameters:

  • opts

    the options switch

  • colors (Hash)

    the color hash



200
201
202
203
204
205
206
207
208
# File 'lib/to-do/helpers/helpers_CLI.rb', line 200

def options_set opts, colors
  opts.separator ""
  opts.separator "  *".colorize(colors[:cyan]) + " set, s".colorize(colors[:yellow]) + 
  " adds additional information to a task".colorize(colors[:magenta])
  opts.separator "    usage: ".colorize(colors[:cyan]) + USAGE[:set].colorize(colors[:red])
  opts.separator "    -p TYPE                          set the priority of the task to one of the \n" + 
  "                                     following. Default is medium"
  opts.separator "    -n                               references a task by its number"
end

#options_title(opts, colors) ⇒ Object

Helper method for the options parser that displays the title

Parameters:

  • opts

    the options switch

  • colors (Hash)

    the color hash



91
92
93
94
95
96
97
98
# File 'lib/to-do/helpers/helpers_CLI.rb', line 91

def options_title opts, colors
  version_path = File.expand_path("../../VERSION", File.dirname(__FILE__))
  opts.version = File.exist?(version_path) ? File.read(version_path) : ""
  opts.banner = "Todo: A simple command line todo application\n\n".colorize(colors[:green]) +
  "    usage:".colorize(colors[:cyan]) + " todo [COMMAND] [option] [arguments]".colorize(colors[:red])
  opts.separator ""
  opts.separator "Commands:".colorize(colors[:green])
end

#options_undo(opts, colors) ⇒ Object

Helper method for the options parser for undo

Parameters:

  • opts

    the options switch

  • colors (Hash)

    the color hash



163
164
165
166
167
168
169
# File 'lib/to-do/helpers/helpers_CLI.rb', line 163

def options_undo opts, colors
  opts.separator ""
  opts.separator "  *".colorize(colors[:cyan])  + " undo, u".colorize(colors[:yellow]) +  
  " undos a completed task".colorize(colors[:magenta])
  opts.separator "    usage: ".colorize(colors[:cyan]) + USAGE[:undo].colorize(colors[:red])
  opts.separator "    -n                               references a task by its number"
end

print the header out

Parameters:

  • colors (Hash)

    the color hash



290
291
292
293
294
295
296
297
298
# File 'lib/to-do/helpers/helpers_CLI.rb', line 290

def print_header colors
  Helpers::CLI::print_stars colors
  puts
  split_name = split Config[:working_list_name], Config[:width]
  split_name.each do |line|
    puts line.center(Config[:width]).colorize(colors[:cyan])
  end
  Helpers::CLI::print_stars colors
end

print asterisks

Parameters:

  • colors (Hash)

    the color hash



281
282
283
284
285
# File 'lib/to-do/helpers/helpers_CLI.rb', line 281

def print_stars colors
  Config[:width].times do 
    print "*".colorize(colors[:red])
  end
end

Print out the tasks

Parameters:

  • completed (Integer)

    whether to print out completed or uncompleted items. 0 if completed. 1 if not

  • tasks (Dataset)

    the dataset of tasks to print out

  • colors (Hash)

    the color hash



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/to-do/helpers/helpers_CLI.rb', line 259

def print_tasks completed, tasks, colors
  priority = {
    0 => "**",
    1 => "*",
    2 => ""
  }
  tasks.each do |task|
    next if task[:Completed] == completed
    printf "%2s".colorize(colors[:magenta]), priority[task[:Priority]]
    printf "%3d. ".to_s.colorize(colors[:yellow]), task[:Task_number]
    split_v = split task[:Name], Config[:width]-7
    puts split_v[0]
    split_v.shift
    split_v.each do |line|
      printf "      %s\n", " " + line
    end
  end
end

#split(string, width) ⇒ Object

splits string for wrapping

Parameters:

  • string (String)

    the string to be split

  • width (Width)

    the width of the line



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
# File 'lib/to-do/helpers/helpers_CLI.rb', line 35

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