Module: Medo::CLISupport
- Included in:
- CLI
- Defined in:
- lib/medo/cli_support.rb
Instance Method Summary collapse
- #choose_task(select_options = {}) ⇒ Object
- #colorize ⇒ Object
- #committing_tasks ⇒ Object
- #edit_input(value) ⇒ Object
- #get_input ⇒ Object
- #load_commands ⇒ Object
- #process_input ⇒ Object
- #tasks ⇒ Object
- #tasks_changed? ⇒ Boolean
Instance Method Details
#choose_task(select_options = {}) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/medo/cli_support.rb', line 33 def choose_task( = {}) task_number = Integer([:number] || 1) rescue raise(ArgumentError, "Invalid task #: #{task_number}") task = tasks.reject { |t| [:done] ^ t.done? }.sort[task_number - 1] or raise(RuntimeError, "No such task!") [task, task_number] end |
#colorize ⇒ Object
29 30 31 |
# File 'lib/medo/cli_support.rb', line 29 def colorize yield unless [:"no-color"] == false end |
#committing_tasks ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/medo/cli_support.rb', line 20 def committing_tasks yield if tasks_changed? storage.write(tasks) storage.commit end end |
#edit_input(value) ⇒ Object
45 46 47 48 49 |
# File 'lib/medo/cli_support.rb', line 45 def edit_input(value) process_input do |path| File.open(path, "w") { |f| f.write(value) } end end |
#get_input ⇒ Object
41 42 43 |
# File 'lib/medo/cli_support.rb', line 41 def get_input process_input end |
#load_commands ⇒ Object
3 4 5 6 7 8 |
# File 'lib/medo/cli_support.rb', line 3 def load_commands Dir.glob(File.('../commands/*', __FILE__)).each do |f| contents = File.read(f) class_eval contents, f, 1 end end |
#process_input ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/medo/cli_support.rb', line 51 def process_input result = nil if [:editor] path = File.join(Dir::Tmpname.tmpdir, "taketo-input-#{Time.now.to_i}") yield path if block_given? status = system("$EDITOR #{path}") if status && File.exists?(path) result = File.read(path) FileUtils.rm(path) end else result = arguments.join(" ").strip end result.to_s end |
#tasks ⇒ Object
10 11 12 13 14 |
# File 'lib/medo/cli_support.rb', line 10 def tasks @tasks ||= storage.read @original_tasks ||= @tasks.map(&:dup) @tasks end |
#tasks_changed? ⇒ Boolean
16 17 18 |
# File 'lib/medo/cli_support.rb', line 16 def tasks_changed? defined? @tasks and @tasks != @original_tasks end |