Class: Tasku::CLI::App

Inherits:
Thor
  • Object
show all
Defined in:
lib/tasku/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(args = ARGV, **opts) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/tasku/cli.rb', line 17

def self.start(args = ARGV, **opts)
  pastel = Pastel.new
  $stdout.puts ""
  $stdout.puts pastel.bright_cyan(LOGO)
  $stdout.puts pastel.bright_cyan("  #{TAGLINE}")
  $stdout.puts ""
  super
end

Instance Method Details

#addObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tasku/cli.rb', line 41

def add
  attrs = if options[:interactive] || options.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.message}")
end

#categoriesObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/tasku/cli.rb', line 197

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



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/tasku/cli.rb', line 161

def delete(id)
  task = find_task(id)

  unless options[: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



153
154
155
156
157
# File 'lib/tasku/cli.rb', line 153

def done(id)
  task = find_task(id)
  task.update(status: "done")
  terminal.render_updated(task)
end

#edit(id) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tasku/cli.rb', line 117

def edit(id)
  task = find_task(id)

  attrs = option_edit
  if attrs.empty? && !options[:clear]
    puts pastel.yellow("No changes specified. Use --help to see available options.")
    return
  end

  if options[:clear]
    clear_fields = options[:clear].split(",").map(&:strip)
    clear_map = {
      "description" => :description,
      "start"       => :start_day,
      "due"         => :due_day,
      "model"       => :model_name,
      "tags"        => :tags,
      "hours"       => :estimated_hours
    }
    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.message}")
end

#listObject



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
91
92
93
94
95
96
# File 'lib/tasku/cli.rb', line 63

def list
  dataset = Task.dataset

  dataset = dataset.where(status: options[:status]) if options[:status]
  dataset = dataset.where(priority: options[:priority]) if options[:priority]
  dataset = dataset.where(project: options[:project]) if options[:project]
  dataset = dataset.where(category: options[:category]) if options[:category]

  if options[:tags]
    tag_filter = options[:tags].split(",").map(&:strip)
    tag_filter.each do |t|
      dataset = dataset.where(Sequel.ilike(:tags, "%#{t}%"))
    end
  end

  if options[:overdue]
    today = Date.today
    dataset = dataset.where { due_day < today }.exclude(status: %w[done cancelled])
  end

  sort_col = case options[: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 = options[:order] == "desc" ? Sequel.desc(sort_col) : Sequel.asc(sort_col)
  dataset = dataset.order(order)

  tasks = dataset.all
  terminal.render_list(tasks)
end

#projectsObject



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/tasku/cli.rb', line 181

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



99
100
101
102
# File 'lib/tasku/cli.rb', line 99

def show(id)
  task = find_task(id)
  terminal.render_show(task)
end

#statsObject



175
176
177
178
# File 'lib/tasku/cli.rb', line 175

def stats
  tasks = Task.dataset.all
  terminal.render_stats(tasks) if tasks
end

#versionObject



213
214
215
# File 'lib/tasku/cli.rb', line 213

def version
  puts "tasku #{Tasku::VERSION}"
end