class TodoTxtViewer::Ui

Public Class Methods

new(main_file, archive_file) click to toggle source
Calls superclass method
# File lib/todo_txt_viewer/ui.rb, line 11
def initialize main_file, archive_file
  super headers: []

  @main_file = main_file.to_s
  @archive_file = archive_file.to_s
  load_file
  self.selection += 1 if selected_line.is_a? HeaderLine
end

Public Instance Methods

go_down() click to toggle source
Calls superclass method
# File lib/todo_txt_viewer/ui.rb, line 42
def go_down
  super
  return unless selected_line.is_a? HeaderLine

  selection == lines.size - 1 ? go_up : super
end
go_up() click to toggle source
Calls superclass method
# File lib/todo_txt_viewer/ui.rb, line 35
def go_up
  super
  return unless selected_line.is_a? HeaderLine

  selection.zero? ? go_down : super
end
key_press(key) click to toggle source
Calls superclass method
# File lib/todo_txt_viewer/ui.rb, line 20
def key_press key # rubocop:disable Metrics/CyclomaticComplexity
  case key
  when 'd' then task_action :do!
  when 'u' then task_action :undo!
  when 'a' then archive_task
  when '+' then task_action :priority_inc!
  when '-' then task_action :priority_dec!
  when ('0'..'9') then select_priority key
  when 'i' then create_task
  when 'r' then load_file
  when 'h', '?' then help
  else super
  end
end

Private Instance Methods

add_section_lines(title, data) click to toggle source
# File lib/todo_txt_viewer/ui.rb, line 116
def add_section_lines title, data
  lines << HeaderLine.new(title)
  data.sort.reverse_each {|t| lines << TaskLine.new(t) }
end
archive_task() click to toggle source
# File lib/todo_txt_viewer/ui.rb, line 60
def archive_task
  return if selected_line.is_a? HeaderLine

  archive = Todo::List.new @archive_file
  task = selected_line.task
  @list.delete_if {|other| other.object_id == task.object_id }
  archive << task
  archive.save!
  @list.save!
  load_lines
  footer.message = "Archived the Task #{task}"
end
create_task() click to toggle source
# File lib/todo_txt_viewer/ui.rb, line 84
def create_task
  line = read_full_line 'Please enter a task:'
  task = Todo::Task.new line
  task.instance_variable_set(:@created_on, Date.today) unless task.created_on
  @list << task
  @list.save!
  load_lines selected_task: task
  footer.message = 'Task inserted'
end
help() click to toggle source
# File lib/todo_txt_viewer/ui.rb, line 94
def help
  HelpUi.new.run
end
load_file() click to toggle source
# File lib/todo_txt_viewer/ui.rb, line 98
def load_file
  @list = Todo::List.new @main_file
  load_lines
  footer.message = "Loaded: #{@main_file}"
end
load_lines(selected_task: nil) click to toggle source
# File lib/todo_txt_viewer/ui.rb, line 104
def load_lines selected_task: nil
  self.lines = []

  add_section_lines '# todo', @list.by_not_done
  add_section_lines '# done', @list.by_done

  return if selected_task.nil?

  idx = lines.index {|l| l.respond_to?(:task) && l.task.object_id == selected_task.object_id }
  self.selection = idx if idx
end
select_priority(key) click to toggle source
# File lib/todo_txt_viewer/ui.rb, line 73
def select_priority key
  return if selected_line.is_a? HeaderLine

  task = selected_line.task
  key = key.to_i
  priority = key.zero? ? nil : ('A'.ord + key - 1).chr
  task.instance_variable_set :@priority, priority
  @list.save!
  load_lines selected_task: task
end
task_action(action) click to toggle source
# File lib/todo_txt_viewer/ui.rb, line 51
def task_action action
  return if selected_line.is_a? HeaderLine

  task = selected_line.task
  selected_line.task_action action
  @list.save!
  load_lines selected_task: task
end