Set cursor types
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user