Class: Kaitai::Struct::Visualizer::Node
- Inherits:
-
Object
- Object
- Kaitai::Struct::Visualizer::Node
- Defined in:
- lib/kaitai/struct/visualizer/node.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#id ⇒ Object
Returns the value of attribute id.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#pos1 ⇒ Object
readonly
Returns the value of attribute pos1.
-
#pos2 ⇒ Object
readonly
Returns the value of attribute pos2.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #add(child) ⇒ Object
- #close ⇒ Object
-
#detect_str_mode ⇒ Object
Empirically detects a mode that would be best to show a designated string.
- #draw(ui) ⇒ Object
- #explore ⇒ Object
- #first_n_bytes_dump(s, n) ⇒ Object
-
#height ⇒ Object
Determine total height of an element, including all children if it’s open and visible.
- #hex? ⇒ Boolean
-
#initialize(tree, value, level, value_method = nil, pos1 = nil, pos2 = nil) ⇒ Node
constructor
A new instance of Node.
- #io ⇒ Object
-
#last_descendant ⇒ Object
Find out last (deepest) descendant of current node.
- #open ⇒ Object
- #open? ⇒ Boolean
- #openable? ⇒ Boolean
- #toggle ⇒ Object
Constructor Details
#initialize(tree, value, level, value_method = nil, pos1 = nil, pos2 = nil) ⇒ Node
Returns a new instance of Node.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 16 def initialize(tree, value, level, value_method = nil, pos1 = nil, pos2 = nil) @tree = tree @value = value @level = level @value_method = value_method unless pos1.nil? or pos2.nil? @pos1 = pos1 @pos2 = pos2 end @open = false @explored = false @children = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
13 14 15 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 13 def children @children end |
#id ⇒ Object
Returns the value of attribute id.
8 9 10 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 8 def id @id end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
10 11 12 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 10 def level @level end |
#parent ⇒ Object
Returns the value of attribute parent.
14 15 16 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 14 def parent @parent end |
#pos1 ⇒ Object (readonly)
Returns the value of attribute pos1.
11 12 13 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 11 def pos1 @pos1 end |
#pos2 ⇒ Object (readonly)
Returns the value of attribute pos2.
12 13 14 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 12 def pos2 @pos2 end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
9 10 11 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 9 def value @value end |
Instance Method Details
#add(child) ⇒ Object
33 34 35 36 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 33 def add(child) @children << child child.parent = self end |
#close ⇒ Object
69 70 71 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 69 def close @open = false end |
#detect_str_mode ⇒ Object
Empirically detects a mode that would be best to show a designated string
136 137 138 139 140 141 142 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 136 def detect_str_mode if @value.encoding == Encoding::ASCII_8BIT :hex else :str_esc end end |
#draw(ui) ⇒ Object
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 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 73 def draw(ui) print ' ' * level print(if @value.nil? '[?]' elsif open? '[-]' elsif openable? '[+]' else '[.]' end) print " #{@id}" pos = 2 * level + 4 + @id.length if @value.is_a?(Fixnum) or @value.is_a?(Bignum) print " = #{@value}" elsif @value.is_a?(Symbol) print " = #{@value}" elsif @value.is_a?(String) print ' = ' pos += 3 @str_mode = detect_str_mode unless @str_mode max_len = @tree.tree_width - pos case @str_mode when :str v = @value.encode('UTF-8') s = v[0, max_len] when :str_esc v = @value.encode('UTF-8') s = v.inspect[0, max_len] when :hex s = first_n_bytes_dump(@value, max_len / 3 + 1) else raise "Invalid str_mode: #{@str_mode.inspect}" end if s.length > max_len s = s[0, max_len - 1] s += '…' end print s elsif @value === true or @value === false print " = #{@value}" elsif @value.is_a?(Array) printf ' (%d = 0x%x entries)', @value.size, @value.size end puts end |
#explore ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 158 def explore return if @explored if @value.nil? @value = @parent.value.send(@value_method) end if @value.is_a?(Fixnum) or @value.is_a?(Bignum) or @value.is_a?(String) or @value.is_a?(Symbol) # do nothing else elsif @value.is_a?(Array) clean_id = @id[0] == '@' ? @id[1..-1] : @id debug_el = @parent.value._debug[clean_id] raise "Unable to get debugging aid for array: #{@parent.value._debug.inspect} using ID '#{clean_id}'" unless debug_el aid = debug_el[:arr] raise "Unable to get debugging aid for array: #{debug_el.inspect}" unless aid max_val_digits = @value.size.to_s.size fmt = "%#{max_val_digits}d" @value.each_with_index { |el, i| n = Node.new(@tree, el, level + 1, nil, aid[i][:start], aid[i][:end]) n.id = sprintf(fmt, i) add(n) } else # Gather seq attributes attrs = Set.new @value.instance_variables.each { |k| k = k.to_s next if k =~ /^@_/ el = @value.instance_eval(k) aid = @value._debug[k[1..-1]] if aid aid_s = aid[:start] aid_e = aid[:end] else #raise "Unable to get debugging aid for '#{k}'" aid_s = nil aid_e = nil end n = Node.new(@tree, el, level + 1, nil, aid_s, aid_e) n.id = k add(n) attrs << k.gsub(/^@/, '') } # Gather instances common_meths = Set.new @value.class.ancestors.each { |cl| next if cl == @value.class common_meths.merge(cl.instance_methods) } inst_meths = Set.new(@value.public_methods) - common_meths inst_meths.each { |meth| k = meth.to_s next if k =~ /^_/ or attrs.include?(k) n = Node.new(@tree, nil, level + 1, meth) n.id = k add(n) } end @explored = true end |
#first_n_bytes_dump(s, n) ⇒ Object
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 123 def first_n_bytes_dump(s, n) i = 0 r = '' s.each_byte { |x| r << sprintf('%02x ', x) i += 1 break if i >= n } r end |
#height ⇒ Object
Determine total height of an element, including all children if it’s open and visible
225 226 227 228 229 230 231 232 233 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 225 def height if @open r = 1 @children.each { |n| r += n.height } r else 1 end end |
#hex? ⇒ Boolean
51 52 53 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 51 def hex? @value.is_a?(String) end |
#io ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 144 def io return @io if @io if @parent.nil? @io = @value._io else obj = @parent while not obj.value.respond_to?(:_io) obj = obj.parent end @io = obj.value._io end end |
#last_descendant ⇒ Object
Find out last (deepest) descendant of current node
237 238 239 240 241 242 243 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 237 def last_descendant n = self while n.open? n = n.children.last end n end |
#open ⇒ Object
63 64 65 66 67 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 63 def open return unless openable? explore @open = true if @explored end |
#open? ⇒ Boolean
38 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 38 def open?; @open; end |
#openable? ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 40 def openable? not ( @value.is_a?(Fixnum) or @value.is_a?(Bignum) or @value.is_a?(String) or @value.is_a?(Symbol) or @value === true or @value === false ) end |
#toggle ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/kaitai/struct/visualizer/node.rb', line 55 def toggle if @open close else open end end |