Class: Kaitai::TUI
- Inherits:
-
Object
- Object
- Kaitai::TUI
- Defined in:
- lib/kaitai/tui.rb
Constant Summary collapse
- COLORS =
{ :black => 0, :gray => 7, :gray0 => 232, :gray1 => 233, :gray2 => 234, :gray3 => 235, :gray4 => 236, :gray5 => 237, :gray6 => 238, :gray7 => 239, :gray8 => 240, :gray9 => 241, :gray10 => 242, :gray11 => 243, :gray12 => 244, :gray13 => 245, :gray14 => 246, :gray15 => 247, :gray16 => 248, :gray17 => 249, :gray18 => 250, :gray19 => 251, :gray20 => 252, :gray21 => 253, :gray22 => 254, :gray23 => 255, }
- KEY_MAP =
{ "\t" => :tab, "\r" => :enter, "\e[A" => :up_arrow, "\e[B" => :down_arrow, "\e[C" => :right_arrow, "\e[D" => :left_arrow, "\e[5~" => :pg_up, "\e[6~" => :pg_dn, "\e[H" => :home, "\e[F" => :end, }
- SINGLE_CHARSET =
'┌┐└┘─│'
- HEAVY_CHARSET =
'┏┓┗┛━┃'
- DOUBLE_CHARSET =
'╔╗╚╝═║'
- CHAR_TL =
0
- CHAR_TR =
1
- CHAR_BL =
2
- CHAR_BR =
3
- CHAR_H =
4
- CHAR_V =
5
Instance Attribute Summary collapse
-
#cols ⇒ Object
readonly
Returns the value of attribute cols.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
Instance Method Summary collapse
- #bg_color=(col) ⇒ Object
- #clear ⇒ Object
- #draw_button(x, y, w, caption) ⇒ Object
- #draw_rectangle(x, y, w, h, charset = DOUBLE_CHARSET) ⇒ Object
- #fg_color=(col) ⇒ Object
-
#goto(x, y) ⇒ Object
Put the cursor up to screen position (x, y).
-
#initialize ⇒ TUI
constructor
A new instance of TUI.
- #message_box(header, msg) ⇒ Object
- #message_box_exception(e) ⇒ Object
-
#read_char ⇒ Object
Reads keypresses from the user including 2 and 3 escape character sequences.
- #read_char_mapped ⇒ Object
- #reset_colors ⇒ Object
Constructor Details
#initialize ⇒ TUI
Returns a new instance of TUI.
10 11 12 13 14 15 16 17 18 |
# File 'lib/kaitai/tui.rb', line 10 def initialize @cols = `tput cols`.to_i @rows = `tput lines`.to_i @seq_clear = `tput clear` @seq_sgr0 = `tput sgr0` @seq_fgcolor = [] @seq_bgcolor = [] end |
Instance Attribute Details
#cols ⇒ Object (readonly)
Returns the value of attribute cols.
7 8 9 |
# File 'lib/kaitai/tui.rb', line 7 def cols @cols end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows.
8 9 10 |
# File 'lib/kaitai/tui.rb', line 8 def rows @rows end |
Instance Method Details
#bg_color=(col) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/kaitai/tui.rb', line 68 def bg_color=(col) #print @seq_bgcolor[col] ||= `tput setab #{col}` code = COLORS[col] raise "Invalid color: #{col}" unless code print "\e[48;5;#{code}m" end |
#clear ⇒ Object
20 21 22 |
# File 'lib/kaitai/tui.rb', line 20 def clear print @seq_clear end |
#draw_button(x, y, w, caption) ⇒ Object
163 164 165 166 |
# File 'lib/kaitai/tui.rb', line 163 def (x, y, w, caption) goto(x, y) puts "[ #{caption} ]" end |
#draw_rectangle(x, y, w, h, charset = DOUBLE_CHARSET) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/kaitai/tui.rb', line 144 def draw_rectangle(x, y, w, h, charset = DOUBLE_CHARSET) goto(x, y) print charset[CHAR_TL] print charset[CHAR_H] * (w - 2) print charset[CHAR_TR] ((y + 1)..(y + h - 1)).each { |i| goto(x, i) print charset[CHAR_V] print ' ' * (w - 2) print charset[CHAR_V] } goto(x, y + h) print charset[CHAR_BL] print charset[CHAR_H] * (w - 2) print charset[CHAR_BR] end |
#fg_color=(col) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/kaitai/tui.rb', line 61 def fg_color=(col) #print @seq_fgcolor[col] ||= `tput setaf #{col}` code = COLORS[col] raise "Invalid color: #{col}" unless code print "\e[38;5;#{code}m" end |
#goto(x, y) ⇒ Object
Put the cursor up to screen position (x, y). First line is 0, first column is 0.
27 28 29 30 |
# File 'lib/kaitai/tui.rb', line 27 def goto(x, y) #print `tput cup #{y} #{x}` printf "\e[%d;%dH", y + 1, x + 1 end |
#message_box(header, msg) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/kaitai/tui.rb', line 130 def (header, msg) top_y = @rows / 2 - 5 draw_rectangle(10, top_y, @cols - 20, 10) goto(@cols / 2 - (header.length / 2) - 1, top_y) print ' ', header, ' ' goto(11, top_y + 1) puts msg (@cols / 2 - 10, top_y + 8, 10, 'OK') loop { c = read_char_mapped return if c == :enter } end |
#message_box_exception(e) ⇒ Object
115 116 117 |
# File 'lib/kaitai/tui.rb', line 115 def (e) ("Error while parsing", e.) end |
#read_char ⇒ Object
Reads keypresses from the user including 2 and 3 escape character sequences.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/kaitai/tui.rb', line 80 def read_char $stdin.echo = false $stdin.raw! input = $stdin.getc.chr if input == "\e" then input << $stdin.read_nonblock(3) rescue nil input << $stdin.read_nonblock(2) rescue nil end ensure $stdin.echo = true $stdin.cooked! return input end |
#read_char_mapped ⇒ Object
96 97 98 99 100 |
# File 'lib/kaitai/tui.rb', line 96 def read_char_mapped c = read_char c2 = KEY_MAP[c] c2 ? c2 : c end |
#reset_colors ⇒ Object
75 76 77 |
# File 'lib/kaitai/tui.rb', line 75 def reset_colors print @seq_sgr0 end |