Set cursor types

This commit is contained in:
2025-12-13 15:35:42 +00:00
parent d9ebf83588
commit d8c281d7d7
5 changed files with 43 additions and 16 deletions

View File

@@ -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)

View File

@@ -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();

View File

@@ -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,

View File

@@ -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;

View File

@@ -2,7 +2,7 @@
#include "../include/ui.h"
uint32_t rows, cols;
int show_cursor = 0;
bool show_cursor = 0;
std::vector<ScreenCell> screen;
std::vector<ScreenCell> 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);
}