Class: Tasku::CLI::App
- Inherits:
-
Thor
- Object
- Thor
- Tasku::CLI::App
- Defined in:
- lib/tasku/cli.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add ⇒ Object
- #categories ⇒ Object
- #delete(id) ⇒ Object
- #done(id) ⇒ Object
- #edit(id) ⇒ Object
- #list ⇒ Object
- #projects ⇒ Object
- #show(id) ⇒ Object
- #sql(query) ⇒ Object
- #stats ⇒ Object
- #version ⇒ Object
Class Method Details
.start(args = ARGV, **opts) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tasku/cli.rb', line 17 def self.start(args = ARGV, **opts) sql_index = args.index("--sql") if sql_index args.delete_at(sql_index) query = args[sql_index] if query.nil? || query.empty? puts pastel.red(" No query provided after --sql.") return end args.delete_at(sql_index) new.invoke(:sql, [query]) return end super end |
Instance Method Details
#add ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/tasku/cli.rb', line 106 def add attrs = if [:interactive] || .values_at(:name, :description, :project, :category).all?(&:nil?) interactive_add else option_add end task = Task.create(attrs) terminal.render_added(task) rescue Sequel::ValidationFailed => e abort pastel.red("Validation error: #{e.}") end |
#categories ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/tasku/cli.rb', line 264 def categories categories = Task.dataset.select(:category).where(Sequel.~(category: nil)).distinct.order(:category).map(:category) if categories.empty? puts pastel.yellow(" No categories found.") return end puts "" categories.each do |c| count = Task.where(category: c).count puts " #{pastel.bold(c)} #{pastel.dim("(#{count} task(s))")}" end puts "" end |
#delete(id) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/tasku/cli.rb', line 228 def delete(id) task = find_task(id) unless [:force] prompt = TTY::Prompt.new confirmed = prompt.yes?(pastel.red("Delete task ##{id} (#{task.name})?")) return unless confirmed end task.destroy terminal.render_deleted(task) end |
#done(id) ⇒ Object
220 221 222 223 224 |
# File 'lib/tasku/cli.rb', line 220 def done(id) task = find_task(id) task.update(status: "done") terminal.render_updated(task) end |
#edit(id) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/tasku/cli.rb', line 183 def edit(id) task = find_task(id) attrs = option_edit if attrs.empty? && ![:clear] puts pastel.yellow("No changes specified. Use --help to see available options.") return end if [:clear] clear_fields = [:clear].split(",").map(&:strip) clear_map = { "description" => :description, "start" => :start_day, "due" => :due_day, "model" => :model_name, "tags" => :tags, "hours" => :estimated_hours, "code" => :code } clear_fields.each do |f| col = clear_map[f] if col attrs[col] = nil else puts pastel.yellow("Unknown field to clear: #{f}") end end end task.update(attrs) terminal.render_updated(task) rescue Sequel::ValidationFailed => e abort pastel.red("Validation error: #{e.}") end |
#list ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/tasku/cli.rb', line 128 def list dataset = Task.dataset dataset = dataset.where(status: [:status]) if [:status] dataset = dataset.where(priority: [:priority]) if [:priority] dataset = dataset.where(project: [:project]) if [:project] dataset = dataset.where(category: [:category]) if [:category] if [:tags] tag_filter = [:tags].split(",").map(&:strip) tag_filter.each do |t| dataset = dataset.where(Sequel.ilike(:tags, "%#{t}%")) end end if [:overdue] today = Date.today dataset = dataset.where { due_day < today }.exclude(status: %w[done cancelled]) end sort_col = case [:sort] when "name" then :name when "priority" then Sequel.case(Task::VALID_PRIORITIES.each_with_index.to_h, 999, :priority) when "due" then :due_day when "status" then Sequel.case(Task::VALID_STATUSES.each_with_index.to_h, 999, :status) else :created_at end order = [:order] == "desc" ? Sequel.desc(sort_col) : Sequel.asc(sort_col) dataset = dataset.order(order) tasks = dataset.all terminal.render_list(tasks) end |
#projects ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/tasku/cli.rb', line 248 def projects projects = Task.dataset.select(:project).where(Sequel.~(project: nil)).distinct.order(:project).map(:project) if projects.empty? puts pastel.yellow(" No projects found.") return end puts "" projects.each do |p| count = Task.where(project: p).count puts " #{pastel.bold(p)} #{pastel.dim("(#{count} task(s))")}" end puts "" end |
#show(id) ⇒ Object
164 165 166 167 |
# File 'lib/tasku/cli.rb', line 164 def show(id) task = find_task(id) terminal.render_show(task) end |
#sql(query) ⇒ Object
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/tasku/cli.rb', line 37 def sql(query) if query.strip.upcase.match?(/\A(SELECT|PRAGMA|EXPLAIN)/) dataset = Tasku::Database.db.fetch(query) rows = dataset.all if rows.empty? puts pastel.yellow(" Query returned no results.") return end raw_keys = rows.first.keys has_code = raw_keys.include?(:code) columns = raw_keys.reject { |c| %i[model_name created_at updated_at code].include?(c.to_sym) } display_cols = columns.map { |c| c == :id ? "CODE-ID" : c.to_s } code_id_keys = columns.include?(:id) && has_code col_widths = columns.each_with_index.map do |c, i| data_vals = rows.map do |r| v = if c == :id && code_id_keys && r[:code] && !r[:code].to_s.empty? "#{r[:code]}-#{r[:id]}" else r[c].to_s end v.length end [display_cols[i].length, data_vals.max || 0].max end puts "" header = display_cols.each_with_index.map { |c, i| pastel.bold(c.ljust(col_widths[i])) } puts " #{header.join(' ')}" puts " #{display_cols.each_with_index.map { |_c, i| pastel.dim("\u2500" * col_widths[i]) }.join(' ')}" rows.each do |row| cells = columns.each_with_index.map do |col, i| val = if col == :id && code_id_keys && row[:code] && !row[:code].to_s.empty? "#{row[:code]}-#{row[:id]}" else row[col].to_s end colour_cell(col, val.ljust(col_widths[i]), row) end puts " #{cells.join(' ')}" puts " #{display_cols.each_with_index.map { |_c, i| pastel.dim("\u2500" * col_widths[i]) }.join(' ')}" end puts "" else affected = Tasku::Database.db.run(query) puts pastel.green(" Query executed successfully.") end rescue Sequel::DatabaseError => e abort pastel.red("SQL error: #{e.}") end |
#stats ⇒ Object
242 243 244 245 |
# File 'lib/tasku/cli.rb', line 242 def stats tasks = Task.dataset.all terminal.render_stats(tasks) if tasks end |