Class: FullscreenTui

Inherits:
Object
  • Object
show all
Defined in:
lib/fullscreen_tui.rb,
lib/fullscreen_tui/footer.rb,
lib/fullscreen_tui/header.rb,
lib/fullscreen_tui/version.rb,
lib/fullscreen_tui/renderer.rb

Defined Under Namespace

Classes: Footer, Header, Renderer

Constant Summary collapse

ARROWS =
{'A' => :arrow_up, 'B' => :arrow_down, 'C' => :arrow_right, 'D' => :arrow_left}.freeze
VERSION =
'0.1.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers:, renderer: nil, enable_jk: true) ⇒ FullscreenTui

Returns a new instance of FullscreenTui.



13
14
15
16
17
18
19
20
# File 'lib/fullscreen_tui.rb', line 13

def initialize headers:, renderer: nil, enable_jk: true
  @header = Header.new headers
  @footer = Footer.new
  @selection = 0
  @renderer = renderer || Renderer.new
  @up_actions = [:arrow_up, enable_jk ? 'k' : nil].reject(&:nil?)
  @down_actions = [:arrow_down, enable_jk ? 'j' : nil].reject(&:nil?)
end

Instance Attribute Details

Returns the value of attribute footer.



12
13
14
# File 'lib/fullscreen_tui.rb', line 12

def footer
  @footer
end

#headerObject (readonly)

Returns the value of attribute header.



12
13
14
# File 'lib/fullscreen_tui.rb', line 12

def header
  @header
end

#linesObject

Returns the value of attribute lines.



11
12
13
# File 'lib/fullscreen_tui.rb', line 11

def lines
  @lines
end

#rendererObject (readonly)

Returns the value of attribute renderer.



12
13
14
# File 'lib/fullscreen_tui.rb', line 12

def renderer
  @renderer
end

#selectionObject

Returns the value of attribute selection.



11
12
13
# File 'lib/fullscreen_tui.rb', line 11

def selection
  @selection
end

Instance Method Details

#go_downObject



84
85
86
87
88
89
# File 'lib/fullscreen_tui.rb', line 84

def go_down
  footer.message = ''
  return if selection >= lines.size - 1

  self.selection += 1
end

#go_upObject



77
78
79
80
81
82
# File 'lib/fullscreen_tui.rb', line 77

def go_up
  footer.message = ''
  return if selection.zero?

  self.selection -= 1
end

#key_press(key) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/fullscreen_tui.rb', line 48

def key_press key
  case key
  when *@up_actions then go_up
  when *@down_actions then go_down
  when 'q', "\u0004" then throw :quit # ctrl-d
  else footer.message = "Unknown key: #{key.inspect}"
  end
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/fullscreen_tui.rb', line 31

def print_screen
  clear

  rows, columns = TermInfo.screen_size

  header_text = header.output
  rows -= required_lines header_text, columns
  footer_text = footer.output width: columns, lines: lines.size, current_line: selection
  rows -= required_lines footer_text, columns

  renderer.header header_text unless header_text.empty?

  print_lines rows, columns

  renderer.footer footer_text
end

#reactObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fullscreen_tui.rb', line 63

def react
  key = case char = STDIN.getch
  when "\u0003" then exit 1
  when "\e"
    case subchar = STDIN.getch
    when '['
      ARROWS.fetch(STDIN.getch) {|k| "\e[#{k}" }
    else "\e#{subchar}"
    end
  else char
  end
  key_press key
end

#read_full_line(message) ⇒ Object



91
92
93
94
# File 'lib/fullscreen_tui.rb', line 91

def read_full_line message
  puts message
  gets
end

#runObject



22
23
24
25
26
27
28
29
# File 'lib/fullscreen_tui.rb', line 22

def run
  catch :quit do
    loop do
      print_screen
      react
    end
  end
end

#selected_lineObject



57
58
59
# File 'lib/fullscreen_tui.rb', line 57

def selected_line
  lines[selection]
end