Class: AI::LineEditor
- Inherits:
-
Object
- Object
- AI::LineEditor
- Defined in:
- lib/line_editor.rb
Overview
── Mini line editor with history + Tab completion (with cycling) ────
Constant Summary collapse
- SIGNALS =
{ "\ej" => :sess_next, "\ek" => :sess_prev, "\el" => :redraw, "\eL" => :redraw, "\e[3;7~" => :del_session, "\e\e[3;5~" => :del_session, "\e[3;5~" => :del_session, "\e\e[3^" => :del_session, "\e[3^" => :del_session, "\e[3;8~" => :del_session }.freeze
Instance Attribute Summary collapse
-
#completer ⇒ Object
Returns the value of attribute completer.
-
#history ⇒ Object
Returns the value of attribute history.
Instance Method Summary collapse
-
#initialize ⇒ LineEditor
constructor
A new instance of LineEditor.
- #readline(prompt) ⇒ Object
Constructor Details
#initialize ⇒ LineEditor
Returns a new instance of LineEditor.
15 |
# File 'lib/line_editor.rb', line 15 def initialize; @history = []; @completer = nil; end |
Instance Attribute Details
#completer ⇒ Object
Returns the value of attribute completer.
4 5 6 |
# File 'lib/line_editor.rb', line 4 def completer @completer end |
#history ⇒ Object
Returns the value of attribute history.
4 5 6 |
# File 'lib/line_editor.rb', line 4 def history @history end |
Instance Method Details
#readline(prompt) ⇒ Object
17 18 19 20 21 22 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 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/line_editor.rb', line 17 def readline(prompt) @prompt = prompt; @buf = +""; @cur = 0 @hpos = @history.length; @stash = nil @prev_rows = 0; @prev_cur_row = 0 @cycle = nil render $stdin.raw do |io| loop do k = read_key(io) or next if (sig = SIGNALS[k]) then return signal(sig) end @cycle = nil unless k == "\t" case k when "\r", "\n" then return submit when "\t" then handle_tab when "\x03" then end_render; print "\r\n"; raise Interrupt when "\e[3~" @buf.slice!(@cur); render if @cur < @buf.length when "\x02", "\e[D" then move(-1) when "\x06", "\e[C" then move(+1) when "\x01", "\e[H", "\eOH", "\e[1~" then @cur = 0; render when "\x05", "\e[F", "\eOF", "\e[4~" then @cur = @buf.length; render when "\eb" then @cur = word_back; render when "\ef" then @cur = word_fwd; render when "\x10", "\e[A" then history_step(-1) when "\x0e", "\e[B" then history_step(+1) when "\x7f", "\b", "\x08" (@buf.slice!(@cur - 1); @cur -= 1; render) if @cur > 0 when "\x04" if @buf.empty? then end_render; print "\r\n"; return nil elsif @cur < @buf.length then @buf.slice!(@cur); render end when "\x0b" then @buf.slice!(@cur..); render when "\x15" then @buf.slice!(0...@cur); @cur = 0; render when "\x17" j = word_back; @buf.slice!(j...@cur); @cur = j; render when "\ed" j = word_fwd; @buf.slice!(@cur...j); render when "\eu" then case_word(:upcase); render when "\eU" then case_word(:downcase); render when "\x14" then transpose; render when "\x0c" then print "\e[H\e[2J"; @prev_rows = 0; @prev_cur_row = 0; render when "\e" then nil else insert(k) if printable?(k) end end end end |