Class: Kaitai::TUI

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeTUI



11
12
13
14
15
16
17
18
19
# File 'lib/kaitai/tui.rb', line 11

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

#colsObject (readonly)

Returns the value of attribute cols.



8
9
10
# File 'lib/kaitai/tui.rb', line 8

def cols
  @cols
end

#rowsObject (readonly)

Returns the value of attribute rows.



9
10
11
# File 'lib/kaitai/tui.rb', line 9

def rows
  @rows
end

Instance Method Details

#bg_color=(col) ⇒ Object



69
70
71
72
73
74
# File 'lib/kaitai/tui.rb', line 69

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

#clearObject



21
22
23
# File 'lib/kaitai/tui.rb', line 21

def clear
  print @seq_clear
end

#draw_button(x, y, w, caption) ⇒ Object



174
175
176
177
# File 'lib/kaitai/tui.rb', line 174

def draw_button(x, y, w, caption)
  goto(x, y)
  puts "[ #{caption} ]"
end

#draw_rectangle(x, y, w, h, charset = DOUBLE_CHARSET) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/kaitai/tui.rb', line 155

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



62
63
64
65
66
67
# File 'lib/kaitai/tui.rb', line 62

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.



28
29
30
31
# File 'lib/kaitai/tui.rb', line 28

def goto(x, y)
  #print `tput cup #{y} #{x}`
  printf "\e[%d;%dH", y + 1, x + 1
end

#input_str(header, msg) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/kaitai/tui.rb', line 145

def input_str(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)
  Readline.readline('', false)
end

#message_box(header, msg) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/kaitai/tui.rb', line 131

def message_box(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
  draw_button(@cols / 2 - 10, top_y + 8, 10, 'OK')
  loop {
    c = read_char_mapped
    return if c == :enter
  }
end

#message_box_exception(e) ⇒ Object



116
117
118
# File 'lib/kaitai/tui.rb', line 116

def message_box_exception(e)
  message_box("Error while parsing", e.message)
end

#read_charObject

Reads keypresses from the user including 2 and 3 escape character sequences.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kaitai/tui.rb', line 81

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_mappedObject



97
98
99
100
101
# File 'lib/kaitai/tui.rb', line 97

def read_char_mapped
  c = read_char
  c2 = KEY_MAP[c]
  c2 ? c2 : c
end

#reset_colorsObject



76
77
78
# File 'lib/kaitai/tui.rb', line 76

def reset_colors
  print @seq_sgr0
end