Class: Kward::NativeShell::LineEditor
- Inherits:
-
Object
- Object
- Kward::NativeShell::LineEditor
- Defined in:
- lib/kward/shell/native_line_editor.rb
Overview
Non-modal edit state. The terminal adapter owns byte reads and redraws.
Constant Summary collapse
- WORD_CHARACTERS =
Keep path and flag punctuation in words, like the default Zsh Emacs keymap.
"[:alnum:]#{Regexp.escape('*?_-.[]~=/&;!#$%^(){}<>')}".freeze
- WORD_START =
/\G[#{WORD_CHARACTERS}]*[^#{WORD_CHARACTERS}]*/- WORD_END =
/\G[^#{WORD_CHARACTERS}]*[#{WORD_CHARACTERS}]*/- HISTORY_SEARCH_KEYS =
[TerminalKeys::CTRL_R, TerminalKeys::CTRL_S].freeze
- CUT_LIMIT =
8- UNDO_LIMIT =
100
Instance Method Summary collapse
- #encoding ⇒ Object
-
#initialize(runtime) ⇒ LineEditor
constructor
A new instance of LineEditor.
- #read(get:, paste:, changed:, alert:, clear:, erase:, kill:, eof:, interrupt:, suggestions: false, draft: nil, completion_cancel: nil) ⇒ Object
Constructor Details
#initialize(runtime) ⇒ LineEditor
Returns a new instance of LineEditor.
19 20 21 22 |
# File 'lib/kward/shell/native_line_editor.rb', line 19 def initialize(runtime) @runtime = runtime @cuts = [] end |
Instance Method Details
#encoding ⇒ Object
24 25 26 |
# File 'lib/kward/shell/native_line_editor.rb', line 24 def encoding @locale.encoding end |
#read(get:, paste:, changed:, alert:, clear:, erase:, kill:, eof:, interrupt:, suggestions: false, draft: nil, completion_cancel: nil) ⇒ Object
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 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 |
# File 'lib/kward/shell/native_line_editor.rb', line 28 def read(get:, paste:, changed:, alert:, clear:, erase:, kill:, eof:, interrupt:, suggestions: false, draft: nil, completion_cancel: nil) Locale.with(@runtime.variables) do |locale| @locale = locale @line = draft.to_s.b.force_encoding(encoding).scrub @position = @line.length @history = @runtime.history @cuts.clear if @private_edit != @history.paused? @private_edit = @history.paused? suggestions &&= !@private_edit @history.refresh @entries = @history.entries @index = @entries.length @history_prefix = nil @vertical_column = nil @menu = nil @last_tab = false @continued_cut = false @undo = [] @last_yank = nil loop do suggestion = suggestion_for_draft if suggestions draw(changed, suggestion) key = get.call return nil unless key raise Interrupt if key == interrupt next if @menu && (key) if HISTORY_SEARCH_KEYS.include?(key) && ![erase, kill, eof].include?(key) @last_tab = @continued_cut = false @last_yank = nil refresh_history if @index == @entries.length @history_prefix = nil key = search_history(key, get: get, changed: changed, alert: alert, clear: clear, erase: erase, interrupt: interrupt) return nil unless key next if key == :cancel_search end if TerminalKeys::RETURN.include?(key) draw(changed, nil) if suggestion return @line + "\n" end return nil if key == eof && @line.empty? @vertical_column = nil unless (TerminalKeys::UP + TerminalKeys::DOWN).include?(key) @last_tab = false unless TerminalKeys::TAB.include?(key) @last_yank = nil unless TerminalKeys::ALT_Y.include?(key) consecutive_cut = @continued_cut @continued_cut = false case key when *TerminalKeys::TAB then complete(alert, completion_cancel) when *TerminalKeys::UP then move_vertical(-1) when *TerminalKeys::DOWN then move_vertical(1) when TerminalKeys::CTRL_P then recall(-1) when TerminalKeys::CTRL_N then recall(1) when *TerminalKeys::LEFT, TerminalKeys::CTRL_B then @position = [@position - 1, 0].max when *TerminalKeys::RIGHT suggestion ? insert(suggestion) : move_right when TerminalKeys::CTRL_F then move_right when *TerminalKeys::HOME, TerminalKeys::CTRL_A then @position = line_start when *TerminalKeys::END_KEY, TerminalKeys::CTRL_E then @position = line_end when erase, *TerminalKeys::BACKSPACE replace(@position - 1, 1) if @position.positive? when kill then cut(0, @line.length) when eof, *TerminalKeys::DELETE then replace(@position, 1) if @position < @line.length when *TerminalKeys::ALT_LEFT, *TerminalKeys::ALT_B then @position = previous_word when *TerminalKeys::ALT_RIGHT, *TerminalKeys::ALT_F then @position = @line.match(WORD_START, @position).end(0) when TerminalKeys::CTRL_K then cut(@position, kill_line_end, consecutive_cut) when TerminalKeys::CTRL_W, *TerminalKeys::ALT_BACKSPACE then cut(previous_word, @position, consecutive_cut) when *TerminalKeys::ALT_D then cut(@position, @line.match(WORD_END, @position).end(0), consecutive_cut) when TerminalKeys::CTRL_Y then yank(0) when *TerminalKeys::ALT_Y then yank_pop when *TerminalKeys::ALT_LAST_WORD then insert_last_word(alert) when TerminalKeys::CTRL_L then clear.call when TerminalKeys::CTRL_T, TerminalKeys::CTRL_T_CSI_U then transpose when TerminalKeys::CTRL_UNDERSCORE then undo when *TerminalKeys::SHIFT_ENTER then insert("\n") when TerminalSequences::BRACKETED_PASTE_START text = paste.call return nil unless text insert(text) else insert(key) if printable_key?(key) end rescue EncodingError, ExpansionError alert.call end end ensure @locale = @line = @draft = @entries = @undo = @last_yank = @history_prefix = @menu = nil end |