class Ztodo::Controller
Class for processing user commands. This class uses Ztodo::Project
and Ztodo::Converter
classes
Public Class Methods
new()
click to toggle source
# File lib/ztodo/controller.rb, line 8 def initialize @proj = Ztodo::Project.new @conv = Ztodo::Converter.new end
Public Instance Methods
execute!(args)
click to toggle source
Execute necessary command
# File lib/ztodo/controller.rb, line 14 def execute! args return cmd_list if args.empty? handler = args[0] if methods.include? ('cmd_'+handler).to_sym args.slice! 0 method(('cmd_'+handler).to_sym).call args else cmd_help end end
Protected Instance Methods
cmd_add(params=[])
click to toggle source
# File lib/ztodo/controller.rb, line 89 def cmd_add params=[] load_or_die task = @proj.create original_key = params.join(' ') unless original_key.empty? task[:key] = original_key.to_sym end File.open(Dir.pwd+'/.ztodo-tmp', 'w+') {|f| f.write(@conv.hash_to_str task) } exit unless system("editor .ztodo-tmp") File.open(Dir.pwd+'/.ztodo-tmp') { |f| task = @conv.str_to_hash f.read begin @proj.add task rescue Exception, e puts 'Task with such key already exists. Postfix added'.yellow task[:key] += Random.rand(1000).to_s @proj.add task end @proj.save! } File.delete Dir.pwd+'/.ztodo-tmp' end
cmd_help(params=[])
click to toggle source
Print help message
# File lib/ztodo/controller.rb, line 30 def cmd_help params=[] puts 'ztodo - Simple Command Line Task Manager'.light_blue puts 'Usage:' puts '$ ztodo help'.green + ' - Show list of available commands' puts '' puts '$ ztodo init'.green + ' - Init project in current folder' puts '$ ztodo init hard'.yellow + ' - Init project in current folder and overwrite old if exists' puts '' puts '$ ztodo'.green + ' - Show list of uncompleted tasks' puts '$ ztodo list'.green + ' - Show list of uncompleted tasks (the same as previous)' puts '$ ztodo list all'.green + ' - Show list of all tasks' puts '' puts '$ ztodo add'.yellow + ' - Add task' puts '$ ztodo add %task-key%'.yellow + ' - Add task with key' puts '$ ztodo modify %task-key%'.yellow + ' - Modify task' puts '$ ztodo remove %task-key%'.yellow + ' - Remove task' end
cmd_init(params=[])
click to toggle source
# File lib/ztodo/controller.rb, line 51 def cmd_init params=[] if File.exists?(Dir.pwd+'/.ztodo') && params[0] != 'hard' puts "ztodo project already existed. Run 'ztodo init hard' for re-init".red return end @proj.init! puts 'ztodo project initialized.'.green end
cmd_list(params=[])
click to toggle source
# File lib/ztodo/controller.rb, line 67 def cmd_list params=[] load_or_die show_all_tasks = params[0] == 'all' tasks = @proj.tasks(show_all_tasks) puts '' orig = [] tasks.each do |key, task| ind = (task[:done] ? 5 : 0) - task[:priority] + 1 orig[ind] = [] if orig[ind].nil? orig[ind].push task end orig.each do |task_list| if task_list.class == Array task_list.each do |task| format_task task end end end end
cmd_modify(params=[])
click to toggle source
# File lib/ztodo/controller.rb, line 116 def cmd_modify params=[] load_or_die original_key = params.join(' ').to_sym task = @proj.tasks(true)[original_key] if task.nil? puts 'Invalid task key'.red exit end File.open(Dir.pwd+'/.ztodo-tmp', 'w+') {|f| f.write(@conv.hash_to_str task) } exit unless system("editor .ztodo-tmp") File.open(Dir.pwd+'/.ztodo-tmp') { |f| task = @conv.str_to_hash f.read begin @proj.modify original_key, task rescue Exception, e puts 'Task with such key already exists. Postfix added'.yellow task[:key] += Random.rand(1000).to_s @proj.modify original_key, task end @proj.save! } File.delete Dir.pwd+'/.ztodo-tmp' end
cmd_remove(params=[])
click to toggle source
# File lib/ztodo/controller.rb, line 144 def cmd_remove params=[] load_or_die begin @proj.remove params.join(' ').to_sym rescue Exception, e puts 'Invalid task key'.red end @proj.save! end
format_task(task)
click to toggle source
# File lib/ztodo/controller.rb, line 60 def format_task task puts task[:key].to_s+' '+ @conv.colored_priority(task[:priority]) + ' ('+(task[:done] ? 'done'.green : 'undone'.yellow)+')' puts task[:description].to_s.light_blue puts '' end
load_or_die()
click to toggle source
# File lib/ztodo/controller.rb, line 154 def load_or_die unless File.exists? Dir.pwd+'/.ztodo' puts 'ztodo project is not initialized! Run "ztodo init"'.red exit end @proj.load! end