From d8c281d7d7c2c2fe0bc10f6328fca084967c9e63 Mon Sep 17 00:00:00 2001 From: Syed Daanish Date: Sat, 13 Dec 2025 15:35:42 +0000 Subject: [PATCH] Set cursor types --- README.md | 7 ------- include/ui.h | 7 ++++++- src/editor.cc | 18 ++++++++++++++++-- src/editor_ctrl.cc | 20 +++++++++++++++++--- src/renderer.cc | 7 ++++--- 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 25b2524..670591a 100644 --- a/README.md +++ b/README.md @@ -6,13 +6,6 @@ A TUI IDE. # TODO -- [ ] Add struct as union of structs that can take input. or similar - - then send input to them. - - Also background thread will run worker function for whatever struct is selected. - - And maybe even a list of active editors etc? -- [ ] Add ui api for setting cursor types. -- [ ] Use that api to set cursor for selection/insertion/normal etc properly. - - Sorta unrelated but check kutuwm + kitty problem not keeping cursor mode properly. - [ ] Add line numbers. - [ ] Add bg highlight for current line. - [ ] Make function to get selected text. (selection itself is done) diff --git a/include/ui.h b/include/ui.h index b8ed3ef..d3131a8 100644 --- a/include/ui.h +++ b/include/ui.h @@ -46,6 +46,11 @@ #define CNTRL_ALT 3 #define SHIFT 4 +#define DEFAULT 0 +#define BLOCK 2 +#define UNDERLINE 4 +#define CURSOR 6 + enum CellFlags : uint8_t { CF_NONE = 0, CF_ITALIC = 1 << 0, @@ -86,7 +91,7 @@ Coord start_screen(); void end_screen(); void update(uint32_t row, uint32_t col, const char *utf8, uint32_t fg, uint32_t bg, uint8_t flags); -void set_cursor(int row, int col, int show_cursor_param); +void set_cursor(int row, int col, int type, bool show_cursor_param); void render(); Coord get_size(); diff --git a/src/editor.cc b/src/editor.cc index 0a2533a..50f2d52 100644 --- a/src/editor.cc +++ b/src/editor.cc @@ -2,6 +2,7 @@ extern "C" { #include "../libs/libgrapheme/grapheme.h" } #include "../include/editor.h" +#include "../include/main.h" #include "../include/ts.h" #include "../include/utils.h" @@ -197,8 +198,21 @@ void render_editor(Editor *editor) { line_index++; free(line); } - if (cursor.row != UINT32_MAX && cursor.col != UINT32_MAX) - set_cursor(cursor.row, cursor.col, 1); + if (cursor.row != UINT32_MAX && cursor.col != UINT32_MAX) { + int type = 0; + switch (mode) { + case NORMAL: + type = BLOCK; + break; + case INSERT: + type = CURSOR; + break; + case SELECT: + type = UNDERLINE; + break; + } + set_cursor(cursor.row, cursor.col, type, true); + } while (rendered_rows < editor->size.row) { for (uint32_t col = 0; col < editor->size.col; col++) update(editor->position.row + rendered_rows, editor->position.col + col, diff --git a/src/editor_ctrl.cc b/src/editor_ctrl.cc index f876306..8c2db30 100644 --- a/src/editor_ctrl.cc +++ b/src/editor_ctrl.cc @@ -42,6 +42,10 @@ void handle_editor_event(Editor *editor, KeyEvent event) { editor->cursor = p; editor->cursor_preffered = UINT32_MAX; editor->selection = p; + if (mode == SELECT) { + mode = NORMAL; + editor->selection_active = false; + } } break; case DRAG: @@ -49,14 +53,17 @@ void handle_editor_event(Editor *editor, KeyEvent event) { Coord p = editor_hit_test(editor, event.mouse_x, event.mouse_y); editor->cursor = p; editor->cursor_preffered = UINT32_MAX; + mode = SELECT; editor->selection_active = true; } break; case RELEASE: if (event.mouse_button == LEFT_BTN) if (editor->cursor.row == editor->selection.row && - editor->cursor.col == editor->selection.col) + editor->cursor.col == editor->selection.col) { + mode = NORMAL; editor->selection_active = false; + } break; } } @@ -65,6 +72,9 @@ void handle_editor_event(Editor *editor, KeyEvent event) { if (event.key_type == KEY_CHAR && event.len == 1) { switch (event.c[0]) { case 'a': + mode = INSERT; + cursor_right(editor, 1); + break; case 'i': mode = INSERT; break; @@ -119,14 +129,16 @@ void handle_editor_event(Editor *editor, KeyEvent event) { cursor_right(editor, 1); } else if (event.c[0] == 0x1B) { mode = NORMAL; + cursor_left(editor, 1); } } else if (event.len > 1) { edit_insert(editor, editor->cursor, event.c, event.len); cursor_right(editor, 1); } - } - if (event.key_type == KEY_SPECIAL && event.special_key == KEY_DELETE) + } else if (event.key_type == KEY_SPECIAL && + event.special_key == KEY_DELETE) { edit_erase(editor, editor->cursor, 1); + } break; case SELECT: if (event.key_type == KEY_CHAR && event.len == 1) { @@ -156,6 +168,8 @@ void handle_editor_event(Editor *editor, KeyEvent event) { } Coord editor_hit_test(Editor *editor, uint32_t x, uint32_t y) { + if (mode != INSERT) + x--; uint32_t target_visual_row = y; uint32_t visual_row = 0; uint32_t line_index = editor->scroll.row; diff --git a/src/renderer.cc b/src/renderer.cc index dda10de..6ed5e89 100644 --- a/src/renderer.cc +++ b/src/renderer.cc @@ -2,7 +2,7 @@ #include "../include/ui.h" uint32_t rows, cols; -int show_cursor = 0; +bool show_cursor = 0; std::vector screen; std::vector old_screen; std::mutex screen_mutex; @@ -187,9 +187,10 @@ void render() { } } -void set_cursor(int row, int col, int show_cursor_param) { +void set_cursor(int row, int col, int type, bool show_cursor_param) { char buf[32]; - int n = snprintf(buf, sizeof(buf), "\x1b[%d;%dH\x1b[5 q", row + 1, col + 1); + int n = snprintf(buf, sizeof(buf), "\x1b[%d;%dH\x1b[%d q", row + 1, col + 1, + type); show_cursor = show_cursor_param; write(STDOUT_FILENO, buf, n); }