Module: Hiiro::Tui::Terminal
- Included in:
- ListScreen
- Defined in:
- lib/hiiro/tui.rb
Instance Method Summary collapse
- #center_text(text, cols) ⇒ Object
- #env_dimension(name) ⇒ Object
- #read_escape_char(input) ⇒ Object
- #read_key(input) ⇒ Object
- #terminal_cols ⇒ Object
- #terminal_line(text, cols) ⇒ Object
- #terminal_rows ⇒ Object
- #truncate(text, cols) ⇒ Object
- #visible_text(text, cols, offset = 0) ⇒ Object
- #with_screen ⇒ Object
Instance Method Details
#center_text(text, cols) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/hiiro/tui.rb', line 63 def center_text(text, cols) truncated = truncate(text, cols) return truncated if truncated.length >= cols left_padding = [(cols - truncated.length) / 2, 0].max (' ' * left_padding) + truncated end |
#env_dimension(name) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/hiiro/tui.rb', line 87 def env_dimension(name) value = ENV[name].to_i return nil unless value.positive? value end |
#read_escape_char(input) ⇒ Object
41 42 43 44 45 |
# File 'lib/hiiro/tui.rb', line 41 def read_escape_char(input) return nil unless IO.select([input], nil, nil, 0.02) input.getch end |
#read_key(input) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/hiiro/tui.rb', line 19 def read_key(input) char = input.getch return :ctrl_c if char == "\u0003" return :enter if char == "\r" || char == "\n" if char == "\e" first = read_escape_char(input) return :escape if first.nil? return :escape unless first == '[' case read_escape_char(input) when 'A' then :up when 'B' then :down when 'C' then :right when 'D' then :left else :escape end else char end end |
#terminal_cols ⇒ Object
53 54 55 56 57 |
# File 'lib/hiiro/tui.rb', line 53 def terminal_cols env_dimension('COLUMNS') || IO.console.winsize[1] rescue StandardError 80 end |
#terminal_line(text, cols) ⇒ Object
59 60 61 |
# File 'lib/hiiro/tui.rb', line 59 def terminal_line(text, cols) truncate(text, cols) + "\e[K" end |
#terminal_rows ⇒ Object
47 48 49 50 51 |
# File 'lib/hiiro/tui.rb', line 47 def terminal_rows env_dimension('LINES') || IO.console.winsize[0] rescue StandardError 24 end |
#truncate(text, cols) ⇒ Object
71 72 73 74 75 76 |
# File 'lib/hiiro/tui.rb', line 71 def truncate(text, cols) return text if text.length <= cols return text[0, cols] if cols <= 1 text[0, cols - 1] + '…' end |
#visible_text(text, cols, offset = 0) ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/hiiro/tui.rb', line 78 def visible_text(text, cols, offset = 0) return truncate(text, cols) if offset <= 0 clipped = text[offset..] || '' return truncate(clipped, cols) if cols <= 1 truncate("«#{clipped}", cols) end |
#with_screen ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/hiiro/tui.rb', line 6 def with_screen input = $stdin $stdout.write("\e[?1049h\e[?25l") $stdout.flush input.raw do yield input end ensure $stdout.write("\r\e[0m\e[?25h\e[?1049l") $stdout.flush end |