Module: SpinalTap::ClientHelpers
- Defined in:
- lib/spinal_tap/client_helpers.rb
Instance Method Summary collapse
- #bell ⇒ Object
- #exception_to_s(e) ⇒ Object
- #exec_counts ⇒ Object
- #exec_eval(code) ⇒ Object
- #exec_help ⇒ Object
- #exec_history ⇒ Object
- #process_loop ⇒ Object
- #read_parsed_line ⇒ Object
- #redraw_cmd_line ⇒ Object
- #reset_cmd_line ⇒ Object
- #setup(server) ⇒ Object
Instance Method Details
#bell ⇒ Object
134 135 136 |
# File 'lib/spinal_tap/client_helpers.rb', line 134 def bell write([7].pack('C')) end |
#exception_to_s(e) ⇒ Object
180 181 182 |
# File 'lib/spinal_tap/client_helpers.rb', line 180 def exception_to_s(e) "#{e.}\r\n#{e.backtrace.join("\r\n")}" end |
#exec_counts ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/spinal_tap/client_helpers.rb', line 165 def exec_counts GC.start results = {} ObjectSpace.each_object() do |o| results[o.class] ||= 0 results[o.class] += 1 end results = results.sort { |a, b| a[1] <=> b[1] } results.each { |e| write("#{e[0]}: #{e[1]}\r\n") } end |
#exec_eval(code) ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/spinal_tap/client_helpers.rb', line 156 def exec_eval(code) begin result = eval(code, @binding) write("=> #{result.to_s}\r\n") rescue Exception => e write(exception_to_s(e)) end end |
#exec_help ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/spinal_tap/client_helpers.rb', line 142 def exec_help write("Commands:\r\n") write(" help - display help information.\r\n") write(" quit - quit this session.\r\n") write(" eval - execute ruby code.\r\n") write(" counts - display object counts.\r\n") end |
#exec_history ⇒ Object
150 151 152 153 154 |
# File 'lib/spinal_tap/client_helpers.rb', line 150 def exec_history @history.all.each_with_index do |entry, index| write("#{index.to_s.ljust(4, ' ')} #{entry}\r\n") end end |
#process_loop ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/spinal_tap/client_helpers.rb', line 27 def process_loop while (line = read_parsed_line) command = line[:command] args = line[:args] case command when 'help' then exec_help when 'history' then exec_history when 'eval' then exec_eval(args.join(' ')) when 'counts' then exec_counts when 'quit' close break else write("Unknown command\r\n") end end ensure close unless closed? @server.unregister(Thread.current) end |
#read_parsed_line ⇒ 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 132 |
# File 'lib/spinal_tap/client_helpers.rb', line 54 def read_parsed_line reset_cmd_line redraw_cmd_line while byte = getbyte byte_s = '' << byte # Delete Char. if byte == 127 if @buffer.length > 0 && @cursor_pos > 1 @cursor_pos -= 1 @buffer.slice!(@cursor_pos - 1) else bell end # Null Char. elsif byte == 0 next # Escape Char. elsif byte == 27 if (byte = getbyte) == 91 # [ Char. case getbyte when 65 # A Char - Up Arrow. if (result = @history.previous) @buffer = result @cursor_pos = @buffer.length + 1 else bell end when 66 # B Char - Down Arrow. if (result = @history.next) @buffer = result @cursor_pos = @buffer.length + 1 else bell end when 67 # C Char - Right Arrow. if @cursor_pos < @buffer.length + 1 @cursor_pos += 1 else bell end when 68 # D Char - Left Arrow. if @cursor_pos > 0 + 1 @cursor_pos -= 1 else bell end end end # Carriage Return Char. elsif byte == 13 write("\r\n") tokens = @buffer.split(' ') command = tokens.first args = tokens[1..-1] if @buffer.length > 0 @history.append(@buffer) end return {:command => command, :args => args} # Normal (letters, numbers, punctuation, etc) Chars. elsif byte >= 32 && byte <= 126 @buffer.insert(@cursor_pos - 1, byte_s) @cursor_pos += 1 # Ignore all other special characters. else end redraw_cmd_line end end |
#redraw_cmd_line ⇒ Object
138 139 140 |
# File 'lib/spinal_tap/client_helpers.rb', line 138 def redraw_cmd_line write("\e[2K\r>\e[s #{@buffer}\e[u\e[#{@cursor_pos}C") end |
#reset_cmd_line ⇒ Object
49 50 51 52 |
# File 'lib/spinal_tap/client_helpers.rb', line 49 def reset_cmd_line @buffer = @history.current @cursor_pos = 1 end |
#setup(server) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/spinal_tap/client_helpers.rb', line 9 def setup(server) @server = server @history = SpinalTap::History.new @binding = SpinalTap::BindingWrapper.new.binding reset_cmd_line setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1) suppress_go_ahead = [255, 251, 3].pack('C3') write(suppress_go_ahead) echo = [255, 251, 1].pack('C3') write(echo) 6.times { getc } end |