Class: RubyTodo::CLI

Inherits:
Thor
  • Object
show all
Includes:
DisplayFormatter, ImportExport, NotebookCommands, Statistics, TaskFilters, TemplateCommands, Thor::Actions
Defined in:
lib/ruby_todo/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TemplateCommands

#template_create, #template_delete, #template_list, #template_show, #template_use

Methods included from TemplateDisplay

#display_template_details, #display_template_list

Methods included from NotebookCommands

#notebook_create, #notebook_list, #notebook_set_default

Methods included from ImportExport

#export_all_notebooks, #export_notebook, #export_to_csv, #export_to_json, #import_all_notebooks, #import_tasks, #task_to_hash

Methods included from Statistics

#display_top_notebooks

Constructor Details

#initialize(*args) ⇒ CLI



45
46
47
48
49
# File 'lib/ruby_todo/cli.rb', line 45

def initialize(*args)
  super
  @prompt = TTY::Prompt.new
  Database.setup
end

Class Method Details

.exit_on_failure?Boolean



39
40
41
# File 'lib/ruby_todo/cli.rb', line 39

def self.exit_on_failure?
  true
end

Instance Method Details

#ai_ask(*prompt_args) ⇒ Object



401
402
403
404
405
406
# File 'lib/ruby_todo/cli.rb', line 401

def ai_ask(*prompt_args)
  require_relative "commands/ai_assistant"
  prompt = prompt_args.join(" ")
  ai_command = AIAssistantCommand.new
  ai_command.ask(prompt, verbose: options[:verbose], api_key: options[:api_key])
end

#ai_configureObject



409
410
411
412
413
# File 'lib/ruby_todo/cli.rb', line 409

def ai_configure
  require_relative "commands/ai_assistant"
  ai_command = AIAssistantCommand.new
  ai_command.configure
end

#export(notebook_name = nil, filename = nil) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/ruby_todo/cli.rb', line 277

def export(notebook_name = nil, filename = nil)
  # Determine what to export
  if options[:all]
    notebooks = Notebook.all
    if notebooks.empty?
      say "No notebooks found".yellow
      return
    end

    data = export_all_notebooks(notebooks)
    filename ||= "ruby_todo_export_all_#{Time.now.strftime("%Y%m%d_%H%M%S")}"
  else
    unless notebook_name
      say "Please specify a notebook name or use --all to export all notebooks".red
      return
    end

    notebook = Notebook.find_by(name: notebook_name)
    unless notebook
      say "Notebook '#{notebook_name}' not found".red
      return
    end

    data = export_notebook(notebook)
    filename ||= "ruby_todo_export_#{notebook_name}_#{Time.now.strftime("%Y%m%d_%H%M%S")}"
  end

  # Ensure export directory exists
  export_dir = File.expand_path("~/.ruby_todo/exports")
  FileUtils.mkdir_p(export_dir)

  # Determine export format and save
  if options[:format].downcase == "csv"
    filename = "#{filename}.csv" unless filename.end_with?(".csv")
    export_path = File.join(export_dir, filename)
    export_to_csv(data, export_path)
  else
    filename = "#{filename}.json" unless filename.end_with?(".json")
    export_path = File.join(export_dir, filename)
    export_to_json(data, export_path)
  end

  say "Tasks exported to #{export_path}".green
end

#import(filename) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/ruby_todo/cli.rb', line 325

def import(filename)
  # Validate file exists
  unless File.exist?(filename)
    expanded_path = File.expand_path(filename)
    if File.exist?(expanded_path)
      filename = expanded_path
    else
      export_dir = File.expand_path("~/.ruby_todo/exports")
      full_path = File.join(export_dir, filename)

      unless File.exist?(full_path)
        say "File '#{filename}' not found".red
        return
      end

      filename = full_path
    end
  end

  # Determine import format from file extension if not specified
  format = options[:format].downcase
  if format != "json" && format != "csv"
    if filename.end_with?(".json")
      format = "json"
    elsif filename.end_with?(".csv")
      format = "csv"
    else
      say "Unsupported file format. Please use .json or .csv files".red
      return
    end
  end

  # Parse the file
  begin
    if format == "json"
      data = JSON.parse(File.read(filename))
    else
      say "CSV import is not yet implemented".red
      return
    end
  rescue JSON::ParserError => e
    say "Error parsing JSON file: #{e.message}".red
    return
  rescue StandardError => e
    say "Error reading file: #{e.message}".red
    return
  end

  # Import the data
  if data.key?("notebooks")
    # This is a full export with multiple notebooks
    imported = import_all_notebooks(data)
    say "Imported #{imported[:notebooks]} notebooks with #{imported[:tasks]} tasks".green
  else
    # This is a single notebook export
    notebook_name = options[:notebook] || data["name"]
    notebook = Notebook.find_by(name: notebook_name)

    unless notebook
      if @prompt.yes?("Notebook '#{notebook_name}' does not exist. Create it?")
        notebook = Notebook.create(name: notebook_name)
      else
        say "Import cancelled".yellow
        return
      end
    end

    count = import_tasks(notebook, data["tasks"])
    say "Imported #{count} tasks into notebook '#{notebook.name}'".green
  end
end

#initObject



52
53
54
55
56
# File 'lib/ruby_todo/cli.rb', line 52

def init
  say "Initializing Ruby Todo...".green
  Database.setup
  say "Ruby Todo has been initialized successfully!".green
end

#stats(notebook_name = nil) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/ruby_todo/cli.rb', line 261

def stats(notebook_name = nil)
  if notebook_name
    notebook = Notebook.find_by(name: notebook_name)
    unless notebook
      say "Notebook '#{notebook_name}' not found".red
      return
    end
    display_notebook_stats(notebook)
  else
    display_global_stats
  end
end

#task_add(notebook_name, title) ⇒ Object



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
# File 'lib/ruby_todo/cli.rb', line 64

def task_add(notebook_name, title)
  notebook = find_notebook(notebook_name)
  return unless notebook

  # Get parameters from options
  description = options[:description]
  due_date = parse_due_date(options[:due_date]) if options[:due_date]
  priority = options[:priority]
  tags = options[:tags]&.split(",")&.map(&:strip)&.join(",")

  task = Task.create(
    notebook: notebook,
    title: title,
    description: description,
    due_date: due_date,
    priority: priority,
    tags: tags,
    status: "todo"
  )

  if task.valid?
    say "Added task: #{title}".green
    say "Description: #{description}" if description
    say "Due date: #{format_due_date(due_date)}" if due_date
    say "Priority: #{priority}" if priority
    say "Tags: #{tags}" if tags
  else
    say "Error adding task: #{task.errors.full_messages.join(", ")}".red
  end
end

#task_delete(notebook_name, task_id) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/ruby_todo/cli.rb', line 206

def task_delete(notebook_name, task_id)
  notebook = Notebook.find_by(name: notebook_name)
  unless notebook
    say "Notebook '#{notebook_name}' not found".red
    return
  end

  task = notebook.tasks.find_by(id: task_id)
  unless task
    say "Task with ID #{task_id} not found".red
    return
  end

  task.destroy
  say "Deleted task #{task_id}".green
end

#task_edit(notebook_name, task_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
175
176
177
178
179
180
181
182
# File 'lib/ruby_todo/cli.rb', line 148

def task_edit(notebook_name, task_id)
  notebook = Notebook.find_by(name: notebook_name)
  unless notebook
    say "Notebook '#{notebook_name}' not found".red
    return
  end

  task = notebook.tasks.find_by(id: task_id)
  unless task
    say "Task with ID #{task_id} not found".red
    return
  end

  updates = {}
  updates[:title] = options[:title] if options[:title]
  updates[:description] = options[:description] if options[:description]
  updates[:priority] = options[:priority] if options[:priority]
  updates[:status] = options[:status] if options[:status]
  updates[:tags] = options[:tags] if options[:tags]

  if options[:due_date]
    updates[:due_date] = parse_due_date(options[:due_date])
  end

  if updates.empty?
    say "No updates specified. Use --title, --description, etc. to specify updates.".yellow
    return
  end

  if task.update(updates)
    say "Updated task #{task_id}".green
  else
    say "Error updating task: #{task.errors.full_messages.join(", ")}".red
  end
end

#task_list(notebook_name) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ruby_todo/cli.rb', line 101

def task_list(notebook_name)
  notebook = find_notebook(notebook_name)
  return unless notebook

  tasks = apply_filters(notebook.tasks)

  if tasks.empty?
    say "No tasks found in notebook '#{notebook_name}'".yellow
    return
  end

  display_tasks(tasks)
end

#task_move(notebook_name, task_id, status) ⇒ Object



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/ruby_todo/cli.rb', line 185

def task_move(notebook_name, task_id, status)
  notebook = Notebook.find_by(name: notebook_name)
  unless notebook
    say "Notebook '#{notebook_name}' not found".red
    return
  end

  task = notebook.tasks.find_by(id: task_id)
  unless task
    say "Task with ID #{task_id} not found".red
    return
  end

  if task.update(status: status)
    say "Moved task #{task_id} to #{status}".green
  else
    say "Error moving task: #{task.errors.full_messages.join(", ")}".red
  end
end

#task_search(query) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/ruby_todo/cli.rb', line 225

def task_search(query)
  notebooks = if options[:notebook]
                [Notebook.find_by(name: options[:notebook])].compact
              else
                Notebook.all
              end

  if notebooks.empty?
    say "No notebooks found".yellow
    return
  end

  results = []
  notebooks.each do |notebook|
    notebook.tasks.each do |task|
      next unless task.title.downcase.include?(query.downcase) ||
                  (task.description && task.description.downcase.include?(query.downcase)) ||
                  (task.tags && task.tags.downcase.include?(query.downcase))

      results << [notebook.name, task.id, task.title, format_status(task.status)]
    end
  end

  if results.empty?
    say "No tasks matching '#{query}' found".yellow
    return
  end

  table = TTY::Table.new(
    header: %w[Notebook ID Title Status],
    rows: results
  )
  puts table.render(:ascii)
end

#task_show(notebook_name, task_id) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ruby_todo/cli.rb', line 116

def task_show(notebook_name, task_id)
  notebook = Notebook.find_by(name: notebook_name)
  unless notebook
    say "Notebook '#{notebook_name}' not found".red
    return
  end

  task = notebook.tasks.find_by(id: task_id)
  unless task
    say "Task with ID #{task_id} not found".red
    return
  end

  say "\nTask Details:".green
  say "ID: #{task.id}"
  say "Title: #{task.title}"
  say "Status: #{format_status(task.status)}"
  say "Priority: #{format_priority(task.priority) || "None"}"
  say "Due Date: #{format_due_date(task.due_date)}"
  say "Tags: #{task.tags || "None"}"
  say "Description: #{task.description || "No description"}"
  say "Created: #{task.created_at}"
  say "Updated: #{task.updated_at}"
end

#versionObject



35
36
37
# File 'lib/ruby_todo/cli.rb', line 35

def version
  puts "Ruby Todo version #{RubyTodo::VERSION}"
end