class Viewer
Constants
- Color
Attributes
pos[RW]
text[RW]
x[RW]
y[RW]
Public Class Methods
new(text)
click to toggle source
# File samples/dasmnavig.rb, line 97 def initialize(text) text = File.read(text) if File.exist? text rescue nil @text = text.gsub("\t", " "*8).to_a.map { |l| l.chomp } @pos = @posh = 0 @x = @y = 0 @mode = :navig @searchtext = 'x' @posstack = [] @h, @w = Ansi.get_terminal_size @h -= 2 @w -= 1 if y = @text.index('entrypoint:') view(0, y) end end
Public Instance Methods
handle_key(k)
click to toggle source
# File samples/dasmnavig.rb, line 222 def handle_key(k) case @mode when :navig handle_key_navig(k) when :search handle_key_search(k) end end
handle_key_search(k)
click to toggle source
# File samples/dasmnavig.rb, line 231 def handle_key_search(k) case k when ?\n; @mode = :navig ; @posstack << [@posh, @pos, @x, @y] ; search_next when 0x20..0x7e; @searchtext << k when :backspace, 0x7f; @searchtext.chop! end end
main_loop()
click to toggle source
# File samples/dasmnavig.rb, line 113 def main_loop Ansi.set_term_canon(true) $stdout.write Ansi::ClearScreen begin loop do refresh if not IO.select([$stdin], nil, nil, 0) handle_key(Ansi.getkey) end ensure Ansi.set_term_canon(false) $stdout.write Ansi.set_cursor_pos(@h+2, 0) + Ansi::ClearLineAfter end end
outline(l, hl=nil)
click to toggle source
# File samples/dasmnavig.rb, line 153 def outline(l, hl=nil) l = l[@posh, @w] || '' hlr = /\b#{Regexp.escape(hl)}\b/i if hl case l when /^\/\//; Color[:comment] + l + Color[:normal] when /^\S+:$/; Color[:label] + l + Color[:normal] when /^(.*)(;.*)$/ str = $1 cmt = $2 str.gsub!(hlr, Color[:hilight]+hl+Color[:normal]) if hl str + Color[:comment] + cmt + Color[:normal] else l = l.gsub(hlr, Color[:hilight]+hl+Color[:normal]) if hl l end end
readtext()
click to toggle source
# File samples/dasmnavig.rb, line 215 def readtext return if not l = @text[@pos+@y] x = (l.rindex(/\W/, [@posh+@x-1, 0].max) || -1)+1 t = l[x..-1][/^\w+/] t if t and @posh+@x < x+t.length end
refresh()
click to toggle source
# File samples/dasmnavig.rb, line 127 def refresh case @mode when :navig refresh_navig when :search refresh_search end end
refresh_search()
click to toggle source
# File samples/dasmnavig.rb, line 149 def refresh_search $stdout.write '' << Ansi.set_cursor_pos(@h+2, 1) << '/' << @searchtext << Ansi::ClearLineAfter end
search_next()
click to toggle source
# File samples/dasmnavig.rb, line 184 def search_next return if @searchtext == '' y = @pos+@y+1 loop do y = 0 if not @text[y] if x = (@text[y] =~ /#@searchtext/i) view(x, y) return end break if y == @pos+@y or (y >= @text.length and not @text[@pos+@y]) y += 1 end end
search_prev()
click to toggle source
# File samples/dasmnavig.rb, line 170 def search_prev return if @searchtext == '' y = @pos+@y-1 loop do y = @text.length-1 if not @text[y] or y < 0 if x = (@text[y] =~ /#@searchtext/i) view(x, y) return end y -= 1 break if y == @pos+@y end end
view(x, y)
click to toggle source
# File samples/dasmnavig.rb, line 198 def view(x, y) @posh, @x = 0, x if @x > @w @posh = @w-@x @x = @w end if @pos+@h < y @y = @h/2-1 @pos = y-@y elsif @pos > y @y = 1 @pos = y-@y else @y = y-@pos end end