Class: RubyRich::LineEditor
- Inherits:
-
Object
- Object
- RubyRich::LineEditor
- Defined in:
- lib/ruby_rich/line_editor.rb
Constant Summary collapse
- WORD_PATTERN =
/[^\s]+/
Instance Attribute Summary collapse
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#history ⇒ Object
readonly
Returns the value of attribute history.
-
#history_path ⇒ Object
Returns the value of attribute history_path.
-
#max_history ⇒ Object
Returns the value of attribute max_history.
-
#multiline ⇒ Object
Returns the value of attribute multiline.
Instance Method Summary collapse
- #add_history(text, persist: true) ⇒ Object
- #backspace ⇒ Object
- #buffer_end ⇒ Object
- #buffer_start ⇒ Object
- #clear ⇒ Object
- #cursor_line_col ⇒ Object
- #delete ⇒ Object
- #empty? ⇒ Boolean
- #end ⇒ Object
- #home ⇒ Object
-
#initialize(multiline: false, history: [], history_path: nil, max_history: 200) ⇒ LineEditor
constructor
A new instance of LineEditor.
- #insert(text) ⇒ Object
- #kill_to_end ⇒ Object
- #kill_to_start ⇒ Object
- #kill_word_back ⇒ Object
- #lines ⇒ Object
- #move_down ⇒ Object
- #move_left ⇒ Object
- #move_right ⇒ Object
- #move_up ⇒ Object
- #newline ⇒ Object
- #render_lines(width:, placeholder: nil, focused: true) ⇒ Object
- #submit_value ⇒ Object
- #value ⇒ Object
- #value=(text) ⇒ Object
Constructor Details
#initialize(multiline: false, history: [], history_path: nil, max_history: 200) ⇒ LineEditor
Returns a new instance of LineEditor.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ruby_rich/line_editor.rb', line 10 def initialize(multiline: false, history: [], history_path: nil, max_history: 200) @multiline = multiline @history_path = history_path @max_history = max_history @history = [] @history_index = nil @chars = [] @cursor = 0 load_history history.each { |item| add_history(item, persist: false) } end |
Instance Attribute Details
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
7 8 9 |
# File 'lib/ruby_rich/line_editor.rb', line 7 def cursor @cursor end |
#history ⇒ Object (readonly)
Returns the value of attribute history.
7 8 9 |
# File 'lib/ruby_rich/line_editor.rb', line 7 def history @history end |
#history_path ⇒ Object
Returns the value of attribute history_path.
8 9 10 |
# File 'lib/ruby_rich/line_editor.rb', line 8 def history_path @history_path end |
#max_history ⇒ Object
Returns the value of attribute max_history.
8 9 10 |
# File 'lib/ruby_rich/line_editor.rb', line 8 def max_history @max_history end |
#multiline ⇒ Object
Returns the value of attribute multiline.
8 9 10 |
# File 'lib/ruby_rich/line_editor.rb', line 8 def multiline @multiline end |
Instance Method Details
#add_history(text, persist: true) ⇒ Object
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/ruby_rich/line_editor.rb', line 149 def add_history(text, persist: true) item = text.to_s return self if item.strip.empty? @history.delete(item) @history << item @history.shift while @history.length > @max_history persist_history if persist && @history_path self end |
#backspace ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/ruby_rich/line_editor.rb', line 60 def backspace return false if @cursor.zero? @chars.delete_at(@cursor - 1) @cursor -= 1 true end |
#buffer_end ⇒ Object
100 101 102 103 |
# File 'lib/ruby_rich/line_editor.rb', line 100 def buffer_end @cursor = @chars.length self end |
#buffer_start ⇒ Object
95 96 97 98 |
# File 'lib/ruby_rich/line_editor.rb', line 95 def buffer_start @cursor = 0 self end |
#clear ⇒ Object
37 38 39 40 41 42 |
# File 'lib/ruby_rich/line_editor.rb', line 37 def clear @chars.clear @cursor = 0 @history_index = nil self end |
#cursor_line_col ⇒ Object
165 166 167 168 169 |
# File 'lib/ruby_rich/line_editor.rb', line 165 def cursor_line_col before = @chars[0...@cursor].join parts = before.empty? ? [""] : before.split("\n", -1) [parts.length - 1, parts.last.to_s.chars.length] end |
#delete ⇒ Object
68 69 70 71 72 73 |
# File 'lib/ruby_rich/line_editor.rb', line 68 def delete return false if @cursor >= @chars.length @chars.delete_at(@cursor) true end |
#empty? ⇒ Boolean
33 34 35 |
# File 'lib/ruby_rich/line_editor.rb', line 33 def empty? @chars.empty? end |
#end ⇒ Object
90 91 92 93 |
# File 'lib/ruby_rich/line_editor.rb', line 90 def end @cursor = current_line_end self end |
#home ⇒ Object
85 86 87 88 |
# File 'lib/ruby_rich/line_editor.rb', line 85 def home @cursor = current_line_start self end |
#insert(text) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ruby_rich/line_editor.rb', line 44 def insert(text) incoming = normalize_insert_text(text) return self if incoming.empty? new_chars = incoming.chars @chars.insert(@cursor, *new_chars) @cursor += new_chars.length @history_index = nil self end |
#kill_to_end ⇒ Object
117 118 119 120 121 122 |
# File 'lib/ruby_rich/line_editor.rb', line 117 def kill_to_end return false if @cursor >= @chars.length @chars.slice!(@cursor...current_line_end) true end |
#kill_to_start ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/ruby_rich/line_editor.rb', line 124 def kill_to_start start = current_line_start return false if @cursor <= start @chars.slice!(start...@cursor) @cursor = start true end |
#kill_word_back ⇒ Object
133 134 135 136 137 138 139 140 |
# File 'lib/ruby_rich/line_editor.rb', line 133 def kill_word_back return false if @cursor.zero? start = previous_word_start @chars.slice!(start...@cursor) @cursor = start true end |
#lines ⇒ Object
160 161 162 163 |
# File 'lib/ruby_rich/line_editor.rb', line 160 def lines text = value text.empty? ? [""] : text.split("\n", -1) end |
#move_down ⇒ Object
111 112 113 114 115 |
# File 'lib/ruby_rich/line_editor.rb', line 111 def move_down return history_next if !@multiline || single_empty_line? || @history_index move_vertical(1) end |
#move_left ⇒ Object
75 76 77 78 |
# File 'lib/ruby_rich/line_editor.rb', line 75 def move_left @cursor = [@cursor - 1, 0].max self end |
#move_right ⇒ Object
80 81 82 83 |
# File 'lib/ruby_rich/line_editor.rb', line 80 def move_right @cursor = [@cursor + 1, @chars.length].min self end |
#move_up ⇒ Object
105 106 107 108 109 |
# File 'lib/ruby_rich/line_editor.rb', line 105 def move_up return history_previous if !@multiline || single_empty_line? || @history_index move_vertical(-1) end |
#newline ⇒ Object
55 56 57 58 |
# File 'lib/ruby_rich/line_editor.rb', line 55 def newline insert("\n") if @multiline self end |
#render_lines(width:, placeholder: nil, focused: true) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ruby_rich/line_editor.rb', line 171 def render_lines(width:, placeholder: nil, focused: true) content = value return [placeholder.to_s] if content.empty? && placeholder rendered = [] line_index, col = cursor_line_col lines.each_with_index do |line, index| marker_col = focused && index == line_index ? col : nil rendered.concat(wrap_line_with_cursor(line, width, marker_col)) end rendered.empty? ? [""] : rendered end |
#submit_value ⇒ Object
142 143 144 145 146 147 |
# File 'lib/ruby_rich/line_editor.rb', line 142 def submit_value submitted = value add_history(submitted) unless submitted.strip.empty? clear submitted end |
#value ⇒ Object
22 23 24 |
# File 'lib/ruby_rich/line_editor.rb', line 22 def value @chars.join end |
#value=(text) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/ruby_rich/line_editor.rb', line 26 def value=(text) @chars = text.to_s.chars @cursor = @chars.length @history_index = nil self end |