Class: Kaitai::Struct::Visualizer::HexViewer
- Inherits:
-
Object
- Object
- Kaitai::Struct::Visualizer::HexViewer
- Defined in:
- lib/kaitai/struct/visualizer/hex_viewer.rb
Constant Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #addr ⇒ Object
- #addr=(a) ⇒ Object
- #addr_to_col(addr) ⇒ Object
- #addr_to_row(addr) ⇒ Object
- #buf=(buf) ⇒ Object
- #byte_at(i) ⇒ Object
- #byte_to_display_char(x) ⇒ Object
- #clamp_cursor ⇒ Object
- #col_to_col_char(c) ⇒ Object
- #col_to_col_hex(c) ⇒ Object
- #each_highlight_region ⇒ Object
- #ensure_visible ⇒ Object
- #highlight(regions) ⇒ Object
- #highlight_draw(p1, p2) ⇒ Object
- #highlight_draw_char(r, c, i, p2) ⇒ Object
- #highlight_draw_hex(r, c, i, p2) ⇒ Object
- #highlight_hide ⇒ Object
- #highlight_show ⇒ Object
-
#initialize(ui, buf, shift_x = 0, tree = nil) ⇒ HexViewer
constructor
A new instance of HexViewer.
- #redraw ⇒ Object
- #reset_cur ⇒ Object
- #row_col_to_addr(row, col) ⇒ Object
- #row_to_scr(r) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(ui, buf, shift_x = 0, tree = nil) ⇒ HexViewer
Returns a new instance of HexViewer.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 5 def initialize(ui, buf, shift_x = 0, tree = nil) @ui = ui @buf = buf @shift_x = shift_x @tree = tree @embedded = not(tree.nil?) @max_scr_ln = @ui.rows - 3 @addr = 0 @scroll_y = 0 reset_cur raise if @cur_x.nil? end |
Class Method Details
.line_width ⇒ Object
149 150 151 152 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 149 def self.line_width #8 + 2 + 3 * PER_LINE + 2 + PER_LINE 12 + 4 * PER_LINE end |
Instance Method Details
#addr ⇒ Object
24 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 24 def addr; @addr; end |
#addr=(a) ⇒ Object
25 26 27 28 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 25 def addr=(a) @addr = a reset_cur end |
#addr_to_col(addr) ⇒ Object
285 286 287 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 285 def addr_to_col(addr) addr % PER_LINE end |
#addr_to_row(addr) ⇒ Object
281 282 283 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 281 def addr_to_row(addr) addr / PER_LINE end |
#buf=(buf) ⇒ Object
20 21 22 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 20 def buf=(buf) @buf = buf end |
#byte_at(i) ⇒ Object
264 265 266 267 268 269 270 271 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 264 def byte_at(i) v = @buf[i] if v.nil? nil else v.ord end end |
#byte_to_display_char(x) ⇒ Object
273 274 275 276 277 278 279 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 273 def byte_to_display_char(x) if x < 0x20 or x >= 0x7f '.' else x.chr end end |
#clamp_cursor ⇒ Object
133 134 135 136 137 138 139 140 141 142 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 133 def clamp_cursor if @addr < 0 @addr = 0 @cur_x = 0 @cur_y = 0 elsif @addr >= @buf.size @addr = @buf.size - 1 reset_cur end end |
#col_to_col_char(c) ⇒ Object
159 160 161 162 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 159 def col_to_col_char(c) #8 + 2 + 3 * PER_LINE + 2 @shift_x + 12 + 3 * PER_LINE + c end |
#col_to_col_hex(c) ⇒ Object
154 155 156 157 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 154 def col_to_col_hex(c) #8 + 2 + 3 * c @shift_x + 10 + 3 * c end |
#each_highlight_region ⇒ Object
187 188 189 190 191 192 193 194 195 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 187 def each_highlight_region return if @hl_regions.nil? n = @hl_regions.size (n - 1).downto(0).each { |i| p1 = @hl_regions[i][0] p2 = @hl_regions[i][1] yield i, p1, p2 unless p1.nil? } end |
#ensure_visible ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 41 def ensure_visible scr_y = row_to_scr(@cur_y) if scr_y < 0 @scroll_y = @cur_y redraw highlight_show elsif scr_y > @max_scr_ln @scroll_y = @cur_y - @max_scr_ln redraw highlight_show end end |
#highlight(regions) ⇒ Object
35 36 37 38 39 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 35 def highlight(regions) highlight_hide @hl_regions = regions highlight_show end |
#highlight_draw(p1, p2) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 212 def highlight_draw(p1, p2) r = row_to_scr(addr_to_row(p1)) return if r > @max_scr_ln if r < 0 c = 0 r = 0 i = row_col_to_addr(@scroll_y, 0) return if i >= p2 else c = addr_to_col(p1) i = p1 end highlight_draw_hex(r, c, i, p2) highlight_draw_char(r, c, i, p2) end |
#highlight_draw_char(r, c, i, p2) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 246 def highlight_draw_char(r, c, i, p2) @ui.goto(col_to_col_char(c), r) while i < p2 v = byte_at(i) return if v.nil? print byte_to_display_char(v) c += 1 if c >= PER_LINE c = 0 r += 1 return if r > @max_scr_ln @ui.goto(col_to_col_char(c), r) end i += 1 end end |
#highlight_draw_hex(r, c, i, p2) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 229 def highlight_draw_hex(r, c, i, p2) @ui.goto(col_to_col_hex(c), r) while i < p2 v = byte_at(i) return if v.nil? printf('%02x ', v) c += 1 if c >= PER_LINE c = 0 r += 1 return if r > @max_scr_ln @ui.goto(col_to_col_hex(c), r) end i += 1 end end |
#highlight_hide ⇒ Object
197 198 199 200 201 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 197 def highlight_hide each_highlight_region { |i, p1, p2| highlight_draw(p1, p2) } end |
#highlight_show ⇒ Object
203 204 205 206 207 208 209 210 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 203 def highlight_show each_highlight_region { |i, p1, p2| @ui.bg_color = @ui.highlight_colors[i] @ui.fg_color = :black highlight_draw(p1, p2) } @ui.reset_colors end |
#redraw ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 168 def redraw i = row_col_to_addr(@scroll_y, 0) row = 0 while row <= @max_scr_ln do line = @buf[i, PER_LINE] return unless line @ui.goto(@shift_x, row) hex = line.bytes.map { |x| sprintf('%02x', x) }.join(' ') char = line.bytes.map { |x| byte_to_display_char(x) }.join printf FMT, i, hex, char i += PER_LINE row += 1 end end |
#reset_cur ⇒ Object
30 31 32 33 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 30 def reset_cur @cur_y = addr_to_row(@addr) @cur_x = addr_to_col(@addr) end |
#row_col_to_addr(row, col) ⇒ Object
289 290 291 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 289 def row_col_to_addr(row, col) row * PER_LINE + col end |
#row_to_scr(r) ⇒ Object
164 165 166 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 164 def row_to_scr(r) r - @scroll_y end |
#run ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/kaitai/struct/visualizer/hex_viewer.rb', line 54 def run c = nil loop { @ui.goto(0, @max_scr_ln + 1) printf "%08x (%d, %d)", @addr, @cur_x, @cur_y @ui.goto(col_to_col_char(@cur_x), row_to_scr(@cur_y)) c = @ui.read_char_mapped case c when :tab return if @embedded when :left_arrow if @addr > 0 @addr -= 1 @cur_x -= 1 if @cur_x < 0 @cur_y -= 1 @cur_x = PER_LINE - 1 end end when :right_arrow if @addr < @buf.size @addr += 1 @cur_x += 1 if @cur_x >= PER_LINE @cur_y += 1 @cur_x = 0 end end when :up_arrow @addr -= PER_LINE @cur_y -= 1 clamp_cursor when :down_arrow @addr += PER_LINE @cur_y += 1 clamp_cursor when :pg_dn @addr += PER_LINE * PAGE_ROWS @cur_y += PAGE_ROWS clamp_cursor when :pg_up @addr -= PER_LINE * PAGE_ROWS @cur_y -= PAGE_ROWS clamp_cursor when :home if @cur_x == 0 @addr = 0 @cur_y = 0 else @addr -= @cur_x @cur_x = 0 end clamp_cursor when :end if @cur_x == PER_LINE - 1 @addr = @buf.size - 1 reset_cur else @addr = @addr - @cur_x + PER_LINE - 1 @cur_x = PER_LINE - 1 end clamp_cursor when 'w' fn = @ui.input_str('Write buffer to file', 'Filename') File.open(fn, 'w') { |out| out.write(@buf) } @ui.clear redraw when 'q' @tree.do_exit if @tree return end ensure_visible } end |