class TaskTree::Tasky

Public Class Methods

new(options) click to toggle source
# File lib/tasktree/tasky.rb, line 10
def initialize(options)
  @output = options.output || 'default.json'
  gem_base_dir = Gem.loaded_specs["task-tree"].full_gem_path
  @font_path = options.figlet_font || "#{gem_base_dir}/fonts/larry3d"
  @prompt = TTY::Prompt.new
  @tree_root = Tree::TreeNode.new('__', '__')
  @current_node = @tree_root
  @screen = TTY::Screen
  @animations = options.animations || false
end

Public Instance Methods

draw_task(task) click to toggle source
# File lib/tasktree/tasky.rb, line 295
def draw_task(task)
  width = @screen.width - 2
  text = `figlet -f #{@font_path} -c -w #{width} #{task}`
  text
end
handle_action(action) click to toggle source
# File lib/tasktree/tasky.rb, line 61
def handle_action(action)
  case action
  when :clean
    print_current
    @prompt.keypress('_')
  when :add
    prompt_and_add_task
    print_current(action)
    save_state
  when :up
    move_up
    print_current(action)
  when :down
    move_down
    print_current(action)
  when :print
    print_tree
    print_current(action)
  when :complete
    set_as_complete
    print_current(action)
    save_state
  when :previous
    move_previous
    print_current(action)
  when :next
    move_next
    print_current(action)
  when :last_sibling
    move_last
    print_current
  when :first_sibling
    move_first
    print_current
  when :pommo
  when :pommo
    start_pommo
  end
end
join_page(left, right) click to toggle source
# File lib/tasktree/tasky.rb, line 256
def join_page(left, right)
  return nil if left.length != right.length
  joined = []
  left.length.times do |i|
    joined.push(left[i] + right[i]) unless left.empty?
  end
  joined
end
load_tree(json_hash) click to toggle source
# File lib/tasktree/tasky.rb, line 313
def load_tree(json_hash)
  node = Tree::TreeNode.new(json_hash['name'], json_hash['content'])
  json_hash['children'].each { |h| node << load_tree(h) } unless json_hash['children'].nil?
  node
end
move_down() click to toggle source
# File lib/tasktree/tasky.rb, line 133
def move_down
  @current_node = @current_node.first_child unless @current_node.first_child.nil?
  if @current_node.content == 'complete'
    move_up unless move_next || move_previous
  end
end
move_first(current=@current_node) click to toggle source
# File lib/tasktree/tasky.rb, line 171
def move_first(current=@current_node)
  first = current.first_sibling
  if first.content == 'complete'
    first = move_next(first)
  else
    @current_node = first
  end
end
move_last(current=@current_node) click to toggle source
# File lib/tasktree/tasky.rb, line 162
def move_last(current=@current_node)
  last = current.last_sibling
  if last.content == 'complete'
    last = move_previous(last)
  else
    @current_node = last
  end
end
move_next(nxt=@current_node) click to toggle source
# File lib/tasktree/tasky.rb, line 151
def move_next(nxt=@current_node)
  return false if nxt.is_only_child? || nxt.is_last_sibling?
  nxt = nxt.next_sibling
  if nxt.content == 'complete'
    nxt = move_next(nxt)
  else
    @current_node = nxt
  end
  true
end
move_previous(prev=@current_node) click to toggle source
# File lib/tasktree/tasky.rb, line 140
def move_previous(prev=@current_node)
  return false if prev.is_only_child? || prev.is_first_sibling?
  prev = prev.previous_sibling
  if prev.content == 'complete'
    prev = move_previous(prev)
  else
    @current_node = prev
  end
  true
end
move_up() click to toggle source
# File lib/tasktree/tasky.rb, line 129
def move_up
  @current_node = @current_node.parent if !@current_node.parent.nil?
end
print_current(direction=nil) click to toggle source
print_from_left(page) click to toggle source
print_from_right(page) click to toggle source
print_tree() click to toggle source
prompt_and_add_task() click to toggle source
# File lib/tasktree/tasky.rb, line 180
def prompt_and_add_task
  new_task = @prompt.ask('what to add?')
  return if new_task.nil? || new_task.empty?
  new_node = Tree::TreeNode.new(new_task, new_task)
  @current_node << new_node
  @current_node = new_node
end
remove_page_col(page, options={}) click to toggle source
# File lib/tasktree/tasky.rb, line 278
def remove_page_col(page, options={})
  new_page = []
  speed = options[:speed] || 1
  page.each do |line|
    if line.empty?
      new_page.push(line)
    else
      if options[:reverse]
        new_page.push(line[0..-speed])
      else
        new_page.push(line[speed..-1])
      end
    end
  end
  new_page
end
restore_state() click to toggle source
# File lib/tasktree/tasky.rb, line 305
def restore_state
  return unless File.exist?(@output)
  data = File.read(@output)
  parsed_data = JSON.parse(data)
  @tree_root = load_tree(parsed_data)
  @current_node = @tree_root
end
save_state() click to toggle source
# File lib/tasktree/tasky.rb, line 301
def save_state
  File.write(@output, @tree_root.to_json)
end
select_with_menu() click to toggle source
# File lib/tasktree/tasky.rb, line 115
def select_with_menu
  selected = @prompt.select("Current level:") do |menu|
    @current_node.children.each do |c|
      menu.choice c.name, c
    end
  end
  @current_node = selected
end
set_as_complete() click to toggle source
# File lib/tasktree/tasky.rb, line 124
def set_as_complete
  @current_node.content = 'complete'
  move_next
end
start() click to toggle source
# File lib/tasktree/tasky.rb, line 33
def start
  return unless verify_dependencies?
  restore_state
  print_current

  action = :quit
  loop do
    action = @prompt.expand('Next action?') do |q|
      q.choice key: 'a', name: 'add', value: :add
      q.choice key: 'u', name: 'up', value: :up
      q.choice key: 'd', name: 'down', value: :down
      q.choice key: 'n', name: 'next', value: :next
      q.choice key: 'p', name: 'previous', value: :previous
      q.choice key: 'P', name: 'print', value: :print
      q.choice key: 'c', name: 'complete', value: :complete
      q.choice key: 'q', name: 'quit', value: :quit
      q.choice key: 'x', name: 'quit without saving', value: :exit
      q.choice key: 's', name: 'start pommodoro timer', value: :pommo
      q.choice key: 'l', name: 'clean display', value: :clean
      q.choice key: '$', name: 'last', value: :last_sibling
      q.choice key: '_', name: 'first', value: :first_sibling
    end
    handle_action(action)
    break if action == :quit || action == :exit
  end
  save_state if action == :quit
end
start_pommo() click to toggle source
# File lib/tasktree/tasky.rb, line 101
def start_pommo
  minutes = @prompt.ask('how many minutes?', convert: :int)
  bar = TTY::ProgressBar.new('[:bar]', total: minutes*6)
  (minutes*6).times do
    sleep(10)
    bar.advance(1)
    # TODO: check for quit or pause?
  end
end
verify_dependencies?() click to toggle source
# File lib/tasktree/tasky.rb, line 21
def verify_dependencies?
  out, err, = Open3.capture3('which figlet')
  if err.include?('no figlet') || err.include?('not found')
    puts ''
    puts '---------------------------------------------------------------'
    puts '--> unfortunately you need to install figlet for this to work  '
    puts '---------------------------------------------------------------'
    return false
  end
  true
end