Class: Kaitai::ConsoleWindows

Inherits:
Object
  • Object
show all
Defined in:
lib/kaitai/console_windows.rb

Constant Summary collapse

GET_STD_HANDLE =
Win32API.new('kernel32', 'GetStdHandle', 'L', 'L')
GET_CONSOLE_SCREEN_BUFFER_INFO =
Win32API.new('kernel32', 'GetConsoleScreenBufferInfo', 'LP', 'L')
FILL_CONSOLE_OUTPUT_ATTRIBUTE =
Win32API.new('kernel32', 'FillConsoleOutputAttribute', 'LILLP', 'I')
FILL_CONSOLE_OUTPUT_CHARACTER =
Win32API.new('kernel32', 'FillConsoleOutputCharacter', 'LILLP', 'I')
SET_CONSOLE_CURSOR_POSITION =
Win32API.new('kernel32', 'SetConsoleCursorPosition', 'LI', 'I')
SET_CONSOLE_TEXT_ATTRIBUTE =
Win32API.new('kernel32', 'SetConsoleTextAttribute', 'LL', 'I')
WRITE_CONSOLE =
Win32API.new("kernel32", "WriteConsole", ['l', 'p', 'l', 'p', 'p'], 'l')
GETCH =
Win32API.new("msvcrt", "_getch", [], 'I')
COLORS =
{
  :black => 0,
  :blue => 1,
  :green => 2,
  :aqua => 3,
  :red => 4,
  :purple => 5,
  :yellow => 6,
  :white => 7,
  :gray => 8,
  :light_blue => 9,
  :light_green => 0xa,
  :light_aqua => 0xb,
  :light_red => 0xc,
  :light_purple => 0xd,
  :light_yellow => 0xe,
  :bright_white => 0xf,
}
ZERO_ESCAPE =
0.chr
E0_ESCAPE =
0xe0.chr
KEY_MAP =
{
  "\b" => :backspace,
  "\t" => :tab,
  "\r" => :enter,

  # Regular AT keyboard arrows
  E0_ESCAPE + "H" => :up_arrow,
  E0_ESCAPE + "P" => :down_arrow,
  E0_ESCAPE + "K" => :left_arrow,
  E0_ESCAPE + "M" => :right_arrow,
  E0_ESCAPE + "I" => :pg_up,
  E0_ESCAPE + "Q" => :pg_dn,
  E0_ESCAPE + "G" => :home,
  E0_ESCAPE + "O" => :end,

  # Keypad
  ZERO_ESCAPE + "H" => :up_arrow,
  ZERO_ESCAPE + "P" => :down_arrow,
  ZERO_ESCAPE + "K" => :left_arrow,
  ZERO_ESCAPE + "M" => :right_arrow,
  ZERO_ESCAPE + "I" => :pg_up,
  ZERO_ESCAPE + "Q" => :pg_dn,
  ZERO_ESCAPE + "G" => :home,
  ZERO_ESCAPE + "O" => :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
@@is_windows =
(RUBY_PLATFORM =~ /cygwin|mswin|mingw|bccwin|wince|emx/) ? true : false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsoleWindows

Returns a new instance of ConsoleWindows.



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
# File 'lib/kaitai/console_windows.rb', line 23

def initialize
  @stdin_handle = GET_STD_HANDLE.call(-10)
  @stdout_handle = GET_STD_HANDLE.call(-11)
  @fg_color = 7
  @bg_color = 0

  # typedef struct _CONSOLE_SCREEN_BUFFER_INFO {
  #   COORD      dwSize;
  #   COORD      dwCursorPosition;
  #   WORD       wAttributes;
  #   SMALL_RECT srWindow;
  #   COORD      dwMaximumWindowSize;
  # } CONSOLE_SCREEN_BUFFER_INFO;

  # 4 + 4 + 2 + 4 * 2 + 4 = 22
     
  csbi = 'X' * 22
  
  GET_CONSOLE_SCREEN_BUFFER_INFO.call(@stdout_handle, csbi)
  @buf_cols, @buf_rows,
    cur_x, cur_y, cur_attr,
    win_left, win_top, win_right, win_bottom,
    max_win_x, max_win_y = csbi.unpack('vvvvvvvvvvv')

  # Getting size of actual visible portion of Windows console
  # http://stackoverflow.com/a/12642749/487064
  @cols = win_right - win_left + 1
  @rows = win_bottom - win_top + 1
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



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

def cols
  @cols
end

#rowsObject (readonly)

Returns the value of attribute rows.



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

def rows
  @rows
end

Class Method Details

.is_windows?Boolean

Detects if current platform is Windows-based.

Returns:

  • (Boolean)


243
244
245
# File 'lib/kaitai/console_windows.rb', line 243

def self.is_windows?
  @@is_windows
end

Instance Method Details

#bg_color=(col) ⇒ Object



118
119
120
121
122
123
# File 'lib/kaitai/console_windows.rb', line 118

def bg_color=(col)
  code = COLORS[col]
  raise "Invalid color: #{col}" unless code
  @bg_color = code
  update_colors
end

#clearObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/kaitai/console_windows.rb', line 64

def clear
  con_size = @buf_cols * @buf_rows
  num_written = 'XXXX'
  FILL_CONSOLE_OUTPUT_CHARACTER.call(
    @stdout_handle,
    0x20, # ' '
    con_size,
    0, # [0, 0] coords
    num_written
  )
  FILL_CONSOLE_OUTPUT_ATTRIBUTE.call(
    @stdout_handle,
    current_color_code,
    con_size,
    0, # [0, 0] coords
    num_written
  )
  goto(0, 0)
end

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



233
234
235
236
# File 'lib/kaitai/console_windows.rb', line 233

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

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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/kaitai/console_windows.rb', line 214

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



111
112
113
114
115
116
# File 'lib/kaitai/console_windows.rb', line 111

def fg_color=(col)
  code = COLORS[col]
  raise "Invalid color: #{col}" unless code
  @fg_color = code
  update_colors
end

#goto(x, y) ⇒ Object

Put the cursor up to screen position (x, y). First line is 0, first column is 0.



87
88
89
90
# File 'lib/kaitai/console_windows.rb', line 87

def goto(x, y)
  coord = [x, y].pack('vv').unpack('V')[0]
  SET_CONSOLE_CURSOR_POSITION.call(@stdout_handle, coord)
end

#input_str(header, msg) ⇒ Object



204
205
206
207
208
209
210
211
212
# File 'lib/kaitai/console_windows.rb', line 204

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



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/kaitai/console_windows.rb', line 190

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



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

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

#on_resize=(handler) ⇒ Object



53
54
55
# File 'lib/kaitai/console_windows.rb', line 53

def on_resize=(handler)
  @on_resize = handler
end

#puts(s) ⇒ Object



57
58
59
60
61
62
# File 'lib/kaitai/console_windows.rb', line 57

def puts(s)
  Kernel::puts s
#    num_written = 'XXXX'
#    reserved = 'XXXX'
#    WRITE_CONSOLE.call(@stdout_handle, s, s.length, num_written, reserved)
end

#read_charObject

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



135
136
137
138
139
140
141
# File 'lib/kaitai/console_windows.rb', line 135

def read_char
  input = GETCH.call.chr
  if input == E0_ESCAPE || input == ZERO_ESCAPE
    input << GETCH.call.chr
  end
  return input
end

#read_char_mappedObject



143
144
145
146
147
# File 'lib/kaitai/console_windows.rb', line 143

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

#reset_colorsObject



125
126
127
128
129
# File 'lib/kaitai/console_windows.rb', line 125

def reset_colors
  @fg_color = 7
  @bg_color = 0
  update_colors
end