Class: AI::LineEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/line_editor.rb

Overview

── Mini line editor with history + Tab completion ───────────────────

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
PASTE_STYLE =
"\e[48;5;18;38;5;231m"
BPASTE_ON =
"\e[?2004h"
BPASTE_OFF =
"\e[?2004l"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLineEditor

Returns a new instance of LineEditor.



19
# File 'lib/line_editor.rb', line 19

def initialize; @history = []; @completer = nil; end

Instance Attribute Details

#completerObject

Returns the value of attribute completer.



4
5
6
# File 'lib/line_editor.rb', line 4

def completer
  @completer
end

#historyObject

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



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
64
65
66
67
68
69
# File 'lib/line_editor.rb', line 21

def readline(prompt)
  @prompt = prompt.to_s; @buf = +""; @cur = 0
  @hpos = @history.length; @stash = nil
  @prev_rows = 1; @prev_cur_row = 0
  @cycle = nil
  @pastes = []
  print BPASTE_ON
  render
  begin
    $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 "\e[200~" then collect_paste(io)
        when "\r", "\n" then insert("\n")
        when "\e\r", "\e\n" then return submit
        when "\t" then handle_tab
        when "\x03" then end_render; print "\r\n"; raise Interrupt
        when "\e[3~" then forward_delete
        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" then backspace
        when "\x04"
          if @buf.empty? then end_render; print "\r\n"; return nil else forward_delete end
        when "\x0b" then del_range(@cur, @buf.length); render
        when "\x15" then del_range(0, @cur); render
        when "\x17" then j = word_back; del_range(j, @cur); render
        when "\ed" then j = word_fwd; del_range(@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
  ensure
    print BPASTE_OFF
  end
end