Class: Taskcmd::CLI::Main
- Inherits:
-
Thor
- Object
- Thor
- Taskcmd::CLI::Main
- Defined in:
- lib/taskcmd/cli/main.rb
Overview
Main CLI entry point
Class Method Summary collapse
Instance Method Summary collapse
- #about ⇒ Object
- #add(description) ⇒ Object
- #done(id) ⇒ Object
- #edit(id) ⇒ Object
- #list ⇒ Object
- #rm(id) ⇒ Object
- #undo(id) ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
203 204 205 |
# File 'lib/taskcmd/cli/main.rb', line 203 def self.exit_on_failure? true end |
Instance Method Details
#about ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/taskcmd/cli/main.rb', line 177 def about project_link = 'https://github.com/dparpyani/taskcmd' say("Opening project page: #{project_link}") if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ system('start', project_link) elsif RbConfig::CONFIG['host_os'] =~ /darwin/ system('open', project_link) elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/ system('xdg-open', project_link) end end |
#add(description) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/taskcmd/cli/main.rb', line 59 def add(description) project = Taskcmd.storage.load_project(validate_project()) task = Taskcmd::Task.new(project.next_id) task.priority = [:priority].to_sym task.description = description.strip project.tasks.push(task) project.increment_next_id Taskcmd.storage.save_project(project) say("Task was created in project '#{project.name}' successfully!\n\n") say(task) say() end |
#done(id) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/taskcmd/cli/main.rb', line 103 def done(id) project = Taskcmd.storage.load_project(validate_project()) task = project.tasks.find { |x| x.id.to_s == id.to_s } if task.nil? say("No task with id=#{id} in project '#{project.name}'") return elsif task.done? say("Task with id=#{id} in project '#{project.name}' is already done.") return end task.complete! Taskcmd.storage.save_project(project) say("Task with id=#{id} in project '#{project.name}' was marked as completed.\n\n") say(task) say() end |
#edit(id) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/taskcmd/cli/main.rb', line 79 def edit(id) project = Taskcmd.storage.load_project(validate_project()) task = project.tasks.find { |x| x.id.to_s == id.to_s } if task.nil? say("No task with id=#{id} in project '#{project.name}'") return end if [:priority] task.priority = [:priority].to_sym end if [:description] task.description = [:description].strip end Taskcmd.storage.save_project(project) say("Task with id=#{id} in project '#{project.name}' was successfully updated!\n\n") say(task) say() end |
#list ⇒ Object
18 19 20 21 22 23 24 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 50 51 52 |
# File 'lib/taskcmd/cli/main.rb', line 18 def list() project = Taskcmd.storage.load_project(validate_project()) tasks = project.tasks id = [:id] priority = [:priority]&.to_sym if id say("Searching for task with id: #{id}\n\n") tasks.select! { |x| x.id == id } elsif priority unless Taskcmd::Task::PRIORITY_CHOICES.include?(priority) raise Taskcmd::Error, "invalid priority '#{priority}'" end say("Searching for tasks with priority: #{priority}\n\n") tasks.select! { |x| x.priority == priority } end if tasks.empty? say("No task found.") else if [:expand] tasks.each do |task| say(task) say() end else headers = ['id', 'priority', 'created at', 'completed at', 'description'] print_table( [headers] + tasks.map {|x| [x.id, x.priority, x.created_at, x.completed_at || '-', x.description].map(&:to_s) } ) end end end |
#rm(id) ⇒ Object
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 |
# File 'lib/taskcmd/cli/main.rb', line 148 def rm(id) project = Taskcmd.storage.load_project(validate_project()) task = project.tasks.find { |x| x.id.to_s == id.to_s } if task.nil? say("No task with id=#{id} in project '#{project.name}'") return end say("Found the following task with id=#{id} in project '#{project.name}'.\n\n") say(task) say() if [:force] sure = true else sure = yes?("Delete this task? This is non-recoverable. Are you sure? Enter 'y' or 'yes' to confirm.", [:white, :on_red]) say() end if sure project.tasks.delete(task) Taskcmd.storage.save_project(project) say("Task has been deleted.") else say("Task was not deleted.") end end |
#undo(id) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/taskcmd/cli/main.rb', line 125 def undo(id) project = Taskcmd.storage.load_project(validate_project()) task = project.tasks.find { |x| x.id.to_s == id.to_s } if task.nil? say("No task with id=#{id} in project '#{project.name}'") return elsif !task.done? say("Task with id=#{id} in project '#{project.name}' is not marked as done.") return end task.undo! Taskcmd.storage.save_project(project) say("Task with id=#{id} in project '#{project.name}' was marked as incomplete.\n\n") say(task) say() end |