From 10215fb3b2d5a178b8ab0419d58d30af3abab625 Mon Sep 17 00:00:00 2001 From: dam Date: Fri, 29 Mar 2024 01:04:23 +0000 Subject: Fixed cursor control on read_input_line. --- modules/TUI/module.jai | 51 +++++++++++++++++++++++++------------------------- ttt.jai | 10 ++++++---- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/modules/TUI/module.jai b/modules/TUI/module.jai index 98afe3d..5fdf3b9 100644 --- a/modules/TUI/module.jai +++ b/modules/TUI/module.jai @@ -389,18 +389,18 @@ read_input :: (count_limit: int = -1, terminators: .. u8) -> string { // TODO UNTESTED read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key { assert(count_limit >= 0, "Invalid value on count_limit parameter."); - str := alloc_string(count_limit); // TODO Alloc like a boss... - write_strings(Commands.StartBlinking, Commands.BlinkingUnderlineShape); - - row, col := get_cursor_position(); + str := alloc_string(count_limit); // TODO Alloc like a boss... + str.count = 0; idx := 0; - key := Keys.None; - // TODO Some of these may be nice to have: // > https://unix.stackexchange.com/questions/255707/what-are-the-keyboard-shortcuts-for-the-command-line + row, col := get_cursor_position(); + write_strings(Commands.StartBlinking, Commands.BlinkingUnderlineShape); + + key := Keys.None; while true { // TODO How to alloc/release temporary memory here? key = get_key(); @@ -412,29 +412,24 @@ read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key { break; case Keys.Left; - if idx == 0 continue; - idx -= 1; + if idx > 0 then idx -= 1; + + case Keys.Right; + if idx < str.count then idx += 1; case Keys.Home; idx = 0; - case Keys.Right; - if idx == str.count-1 continue; - idx += 1; - case Keys.End; - idx = str.count-1; - while str[idx] == 0 && idx > 0 { - idx -= 1; - } - idx += 1; + idx = str.count; case Keys.Delete; - if idx == str.count-1 continue; + if idx == str.count continue; for idx..str.count-2 { str.data[it] = str.data[it+1]; } str.data[str.count-1] = 0; + str.count -= 1; case Keys.Backspace; if idx == 0 continue; @@ -443,17 +438,21 @@ read_input_line :: (count_limit: int, is_visible: bool = true) -> string, Key { str.data[it] = str.data[it+1]; } str.data[str.count-1] = 0; + str.count -= 1; case; - if idx >= str.count continue; - if !is_escape_code(key) { - key_str := to_string(key); - for < str.count-1..idx { - str.data[it] = str.data[it-1]; - } - str.data[idx] = key_str.data[0]; - idx += 1; + if idx >= count_limit continue; + if is_escape_code(key) continue; + + for < count_limit..idx+1 { + str.data[it] = str.data[it-1]; } + + key_str := to_string(key); + str.data[idx] = key_str.data[0]; + + if str.count < count_limit then str.count += 1; + idx += 1; } diff --git a/ttt.jai b/ttt.jai index f0a3a9e..6f0f426 100644 --- a/ttt.jai +++ b/ttt.jai @@ -1396,7 +1396,8 @@ main :: () { TUI.clear_terminal(); TUI.set_cursor_position(1, 1); print("Enter some text (use Enter to finish, Esc to cancel, or resize to abort):"); - TUI.set_cursor_position(2, 1); + r, c := TUI.get_cursor_position(); + TUI.set_cursor_position(r+1, 1); str, key := TUI.read_input_line(15); TUI.set_cursor_position(3, 1); if key == { @@ -1421,16 +1422,17 @@ main :: () { } // BUG - SECOND TIME CALLING TO read_input_line fails... + // SECOND TIME CALLING TO read_input_line fails... - if 1 { + if 0 { print("TEST : hidden user input\n", to_standard_error = true); auto_release_temp(); TUI.start(); TUI.clear_terminal(); TUI.set_cursor_position(1, 1); print("Enter some text (use Enter to finish, Esc to cancel, or resize to abort):"); - TUI.set_cursor_position(2, 1); + r, c := TUI.get_cursor_position(); + TUI.set_cursor_position(r+1, 1); str, key := TUI.read_input_line(15, false); TUI.set_cursor_position(3, 1); if key == { -- cgit v1.2.3