Class: Intent::Review::Commands

Inherits:
CommandDispatcher
  • Object
show all
Defined in:
lib/intent/verbs/review.rb

Instance Method Summary collapse

Instance Method Details

#draw_card(top, item, focus) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/intent/verbs/review.rb', line 40

def draw_card(top, item, focus)

  style = unless top == focus
    { fg: :white, border: { fg: :white } }
  else
    { fg: :bright_white, border: { fg: :bright_magenta, bg: :black } }
  end

  title = if top == focus
    {bottom_right: "[x: close][enter: launch]"}
  else
    {}
  end

  #pastel = Pastel.new
  width = 90

  TTY::Box.frame(width: width, height: 4, left: 4, title: title, border: :thick, style: style) do
    "\s#{truncate(item[:title], 88)}\n\s#{truncate(item[:href], 88)}"
  end
end

#draw_list(items, focus) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/intent/verbs/review.rb', line 71

def draw_list(items, focus)
  buffer = []
  items.each_with_index do |item, i|
    buffer << draw_card(i, item, focus)
  end
  result = buffer.join("")

  print $cursor.clear_screen + $cursor.move_to(0,0)
  print result
end

#launch_densityplotObject



12
13
14
# File 'lib/intent/verbs/review.rb', line 12

def launch_densityplot
  UnicodePlot.barplot(data: {'calyx': 20, 'fictive': 50}, title: "Projects").render
end

#launch_ui_loopObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/intent/verbs/review.rb', line 16

def launch_ui_loop
  list = Todo::List.new(ENV['TODO_TXT'])

  box = TTY::Box.frame(
    width: TTY::Screen.width,
    height: TTY::Screen.height-1,
    align: :center,
    border: :thick,
    style: {
      fg: :bright_yellow,
      border: {
        fg: :white,
      }
    }
  )
  
  focus_index = 0
  
  def truncate(str, width)
    return str unless str.length >= width
  
    "#{str.slice(0, width-3)}..."
  end
  
  def draw_card(top, item, focus)
  
    style = unless top == focus
      { fg: :white, border: { fg: :white } }
    else
      { fg: :bright_white, border: { fg: :bright_magenta, bg: :black } }
    end
  
    title = if top == focus
      {bottom_right: "[x: close][enter: launch]"}
    else
      {}
    end
  
    #pastel = Pastel.new
    width = 90
  
    TTY::Box.frame(width: width, height: 4, left: 4, title: title, border: :thick, style: style) do
      "\s#{truncate(item[:title], 88)}\n\s#{truncate(item[:href], 88)}"
    end
  end
  
  items_per_page = (TTY::Screen.height-1) / 4
  
  items = list.by_not_done.by_context("@reading").slice(0, items_per_page).map do |item|
    { title: item.text, href: item.text }
  end
  
  $cursor = TTY::Cursor
  print $cursor.hide
  
  def draw_list(items, focus)
    buffer = []
    items.each_with_index do |item, i|
      buffer << draw_card(i, item, focus)
    end
    result = buffer.join("")
  
    print $cursor.clear_screen + $cursor.move_to(0,0)
    print result
  end
  
  draw_list(items, focus_index)
  
  reader = TTY::Reader.new(interrupt: :exit)
  
  reader.on(:keyctrl_x, :keyescape) do
    print $cursor.clear_screen + $cursor.move_to(0,0)
    print $cursor.show
    exit
  end
  
  reader.on(:keytab, :keydown) do
    if focus_index == items.count - 1
      focus_index = 0
    else
      focus_index += 1
    end
    draw_list(items, focus_index)
  end
  
  reader.on(:keyup) do
    if focus_index == 0
      focus_index = items.count - 1
    else
      focus_index -= 1
    end
    draw_list(items, focus_index)
  end
  
  reader.on(:keypress) do |key|
    if key.value == "x"
      items.delete_at(focus_index)
      focus_index -= 1 if focus_index >= items.count
      draw_list(items, focus_index)
    end
  end
  
  reader.on(:keyenter) do
    `chrome-cli open #{items[focus_index][:href]}`
  end
  
  loop do
    reader.read_line(echo: false)
  end
end

#run(args, output = STDOUT) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/intent/verbs/review.rb', line 4

def run(args, output=STDOUT)
  if args.empty?
    launch_densityplot
  else
    print_help(output)
  end
end

#truncate(str, width) ⇒ Object



34
35
36
37
38
# File 'lib/intent/verbs/review.rb', line 34

def truncate(str, width)
  return str unless str.length >= width

  "#{str.slice(0, width-3)}..."
end