diff options
| author | dam <dam@gudinoff> | 2023-09-12 01:04:16 +0100 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2023-09-12 01:04:16 +0100 |
| commit | 7469ce1ab6c761e81334b918a34972fedba0923e (patch) | |
| tree | f1554542318dd54b7b91e1df482fa5d105476fe0 /tui.jai | |
| parent | 76f3be800b9306a0a63c9f888b5e61c27ea35c0e (diff) | |
| download | task-time-tracker-7469ce1ab6c761e81334b918a34972fedba0923e.tar.zst task-time-tracker-7469ce1ab6c761e81334b918a34972fedba0923e.zip | |
Adding a terminal user interface module (like ncurses but way simpler).
Diffstat (limited to 'tui.jai')
| -rw-r--r-- | tui.jai | 106 |
1 files changed, 106 insertions, 0 deletions
@@ -0,0 +1,106 @@ +#import "Basic"; + +Drawings :: struct { + CornerBR :: "\x6A"; + CornerTR :: "\x6B"; + CornerTL :: "\x6C"; + CornerBL :: "\x6D"; + Cross :: "\x6E"; + LineH :: "\x71"; + TeeL :: "\x74"; + TeeR :: "\x75"; + TeeB :: "\x76"; + TeeT :: "\x77"; + LineV :: "\x78"; +} + +Commands :: struct { + EnterAlternateBuffer :: "\e[?1049h"; + EnterMainBuffer :: "\e[?1049l"; + + EnterDrawingMode :: "\e(0"; + EnterNormalMode :: "\e(B"; + ClearScreen :: "\e[2J"; + + // Cursor Visibility + ShowCursor :: "\e[?25h"; + HideCursor :: "\e[?25l"; + StartBlinking :: "\e[?25h]"; + StopBlinking :: "\e[?25l]"; + + // Cursor Shape + DefaultShape :: "\e[0 q"; + BlinkingBlockShape :: "\e[1 q"; + SteadyBlockShape :: "\e[2 q"; + BlinkingUnderlineShape :: "\e[3 q"; + SteadyUnderlineShape :: "\e[4 q"; + BlinkingBarShape :: "\e[5 q"; + SteadyBarShape :: "\e[6 q"; + + // Input Mode + KeypadAppMode :: "\e="; + KeypadNumMode :: "\e>"; + CursorAppMode :: "\e[?1h"; + CursorNormalMode :: "\e[?1l"; + + // Query State + QueryCursorPos :: "\e[6n"; // Emits the cursor position as: ESC [ <r> ; <c> R Where <r> = row and <c> = column. + QueryDeviceAttributes :: "\e[0c"; +} + +start :: () { + write_string(Commands.EnterAlternateBuffer); +} + +stop :: () { + write_string(Commands.EnterMainBuffer); +} + +draw_box :: (x: int, y: int, width: int, height: int, to_standard_error := false) { + + write_string(Commands.HideCursor); + write_strings( + // Commands.EnterNormalMode, + Commands.EnterDrawingMode, + "\e[1;1H", // Move to position 1,1 + // TODO // Move pointer to top-left corner. + Drawings.CornerTL, + to_standard_error = to_standard_error); + + for 1..width-2 { + write_string(Drawings.LineH, to_standard_error = to_standard_error); + } + write_string(Drawings.CornerTR, to_standard_error = to_standard_error); + + + // TODO Take care of the temporary allocations. + for idx: 2..height-1 { + tmpL := tprint("\e[%;%H", idx, 1); + tmpR := tprint("\e[%;%H", idx, width); + write_strings( + tmpL, + Drawings.LineV, + tmpR, + Drawings.LineV, + to_standard_error = to_standard_error); + } + + tmpBL := tprint("\e[%;%H", height, 1); + write_strings( + tmpBL, + Drawings.CornerBL, + to_standard_error = to_standard_error); + for 1..width-2 { + write_string(Drawings.LineH, to_standard_error = to_standard_error); + } + write_string(Drawings.CornerBR, to_standard_error = to_standard_error); + + write_strings( + // TODO // print + Commands.EnterNormalMode, + to_standard_error = to_standard_error); +} + +clear_screen :: inline () { + write_string(Commands.ClearScreen); +} |
