aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordam <dam@gudinoff>2024-01-02 01:48:31 +0000
committerdam <dam@gudinoff>2024-01-02 01:48:31 +0000
commit698836ed1c6a9175903edd41f4cafdf7cb4b946d (patch)
tree755bc2fef4b667696408aa2aa191eae7e9426855
parentac9e0033f39a9f2feaa024024a7c443d4e6df77f (diff)
downloadtask-time-tracker-698836ed1c6a9175903edd41f4cafdf7cb4b946d.tar.zst
task-time-tracker-698836ed1c6a9175903edd41f4cafdf7cb4b946d.zip
Initial implementation of procedure to read user input from a single line.
-rw-r--r--TUI/module.jai39
-rw-r--r--ttt.jai31
2 files changed, 63 insertions, 7 deletions
diff --git a/TUI/module.jai b/TUI/module.jai
index 8c1d7ed..4e87589 100644
--- a/TUI/module.jai
+++ b/TUI/module.jai
@@ -11,14 +11,17 @@
#import "Thread";
Drawings :: struct {
- // TODO Maybe just use unicode instead of jumping back and forth between drawing mode?!
+ /*
+ TODO
+ Using unicode allows to just-print instead of jumping back and forth between drawing/text modes.
+ But it seems that not all terminals support unicode?! Do we even bother with those terminals?!
+ */
// test_drawing_without_mode :: () {
// top := "┌───┐";
// btm := "└───┘";
// print(">%<\n", top);
// print(">%<\n", btm);
// }
- // #run test_drawing_without_mode();
CornerBR :: "\x6A";
CornerTR :: "\x6B";
CornerTL :: "\x6C";
@@ -129,7 +132,10 @@ set_style_colors :: (foreground: u8, background: u8) {
set_style :: (bold: bool, underline: bool = false, strike_through: bool = false, negative: bool = false) {
print(
#run sprint(Commands.SetGraphicsRendition, "%;%;%;%"),
- ifx bold then 1 else 22, ifx underline then 4 else 24, ifx strike_through then 9 else 29, ifx negative then 7 else 27);
+ ifx bold then 1 else 22,
+ ifx underline then 4 else 24,
+ ifx strike_through then 9 else 29,
+ ifx negative then 7 else 27);
}
// TODO Maybe make the OS_* procedures as inline?!
@@ -566,14 +572,33 @@ get_string :: (count_limit: int = -1) -> string {
}
}
-read_string :: (count_limit: int = -1) -> string, Key {
- // TODO WIP FIXME WIP
+// TODO UNTESTED
+user_line_input :: (count_limit: int = -1, is_visible: bool = true) -> string, Key {
+
+ // TODO Show blinking cursor
+ row, col := get_cursor_position();
+
+ key := Keys.None;
+ builder: String_Builder;
+ init_string_builder(*builder);
+
+ while key != Keys.Resize && key != Keys.Escape {
+ // TODO How to alloc/release temporary memory here?
+ key = get_key();
+ if key == Keys.Enter then break;
+ str := to_string(key);
+ print("%", str);
+ print_to_builder(*builder, "%", str);
+ }
+
/*
Use the get_key to read user input and show it on screen... should allow to move the cursor left and right and to delete/backspace.
- Enter should be used to end the input.
- Escape should discard the input returning an empty string and a Enter key.
+ Enter should end the input, returning the input string and the Enter key.
+ Escape should discard the input returning an empty string and a None key.
Resize should discard the input returning an empty string and a Resize key.
*/
+ result := ifx key == Keys.Enter then builder_to_string(*builder) else "";
+ return result, key;
}
start :: () {
diff --git a/ttt.jai b/ttt.jai
index 62c2e6c..4e63204 100644
--- a/ttt.jai
+++ b/ttt.jai
@@ -1364,6 +1364,37 @@ main :: () {
print("size(CxR): %x%\n", xcolumns, xrows);
}
+ if 1 {
+ print("TEST : 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);
+ str, key := TUI.user_line_input();
+ TUI.set_cursor_position(3, 1);
+ if key == {
+ case TUI.Keys.Escape; {
+ print("Have you pressed Esc? (y/n)");
+ assert(TUI.get_key() == #char "y", "Failed to read line on Esc.");
+ print("> success\n", to_standard_error = true);
+ }
+
+ case TUI.Keys.Resize; {
+ print("Have you resized the terminal? (y/n)");
+ assert(TUI.get_key() == #char "y", "Failed to read line on resize.");
+ print("> success\n", to_standard_error = true);
+ }
+ case; {
+ print("Have you entered '%'? (y/n)", str);
+ assert(TUI.get_key() == #char "y", "Failed to read line.");
+ print("> success\n", to_standard_error = true);
+ }
+ }
+ TUI.stop();
+ }
+
// -- -- -- Testing TUI -- STOP