summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorKevin Fischer <[email protected]>2020-09-05 20:22:22 +0900
committerKevin Fischer <[email protected]>2020-09-05 20:22:22 +0900
commit8c69db94621010caa2b417891cae1933cc904bd2 (patch)
treea9331d8eae237a05179c8fc16a6802a9fcca1dc1
parent958cf43779d2bf528869e80511c4c4f2a433b2db (diff)
downloaddragonruby-game-toolkit-contrib-8c69db94621010caa2b417891cae1933cc904bd2.tar.gz
dragonruby-game-toolkit-contrib-8c69db94621010caa2b417891cae1933cc904bd2.zip
Save cursor position in prompt
-rw-r--r--dragon/console_prompt.rb16
1 files changed, 14 insertions, 2 deletions
diff --git a/dragon/console_prompt.rb b/dragon/console_prompt.rb
index aea5df8..2aa1e48 100644
--- a/dragon/console_prompt.rb
+++ b/dragon/console_prompt.rb
@@ -18,23 +18,35 @@ module GTK
@cursor_color = Color.new [187, 21, 6]
@console_text_width = console_text_width
+ @cursor_position = 0
+
@last_autocomplete_prefix = nil
@next_candidate_index = 0
end
+ def current_input_str=(str)
+ @current_input_str = str
+ @cursor_position = str.length
+ end
+
def <<(str)
- @current_input_str << str
+ @current_input_str = @current_input_str[0...@cursor_position] + str + @current_input_str[@cursor_position..-1]
+ @cursor_position += str.length
@current_input_changed_at = Kernel.global_tick_count
reset_autocomplete
end
def backspace
- @current_input_str.chop!
+ return if current_input_str.length.zero? || @cursor_position.zero?
+
+ @current_input_str = @current_input_str[0...(@cursor_position - 1)] + @current_input_str[@cursor_position..-1]
+ @cursor_position -= 1
reset_autocomplete
end
def clear
@current_input_str = ''
+ @cursor_position = 0
reset_autocomplete
end