From 6d7ff828389456a1e73b313afe2b732eb50a207c Mon Sep 17 00:00:00 2001 From: _ov4 Date: Tue, 22 Sep 2026 03:22:34 +0000 Subject: add cursor insert --- include/line/cursor.h | 2 ++ module-test/line_test.cc | 1 + src/line/cursor.cc | 23 +++++++++++++++++++++-- src/line/mode.cc | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/include/line/cursor.h b/include/line/cursor.h index 7417201..39160dc 100644 --- a/include/line/cursor.h +++ b/include/line/cursor.h @@ -24,5 +24,7 @@ void print_override(const std::string &s); void move_cursor(char c); +void cursor_input(char c, std::string &buffer); + } diff --git a/module-test/line_test.cc b/module-test/line_test.cc index a608a16..e2784d9 100644 --- a/module-test/line_test.cc +++ b/module-test/line_test.cc @@ -29,6 +29,7 @@ int main() cursor_col = pos.col; string s = readline(); disable_raw(); + cout << "\n\n\n\n" << endl; cout << s << endl; return 0; } diff --git a/src/line/cursor.cc b/src/line/cursor.cc index ce01466..9ff8bbc 100644 --- a/src/line/cursor.cc +++ b/src/line/cursor.cc @@ -47,12 +47,13 @@ T_position get_cursor_position() void print_override(const std::string &s) { + print("\x1b[s"); fflush(stdout); print("\r"); fflush(stdout); print("{}", s); fflush(stdout); - cursor_col = s.size()+1; - line_end_pos = cursor_col; + //line_end_pos = s.size()+1; + print("\x1b[u"); fflush(stdout); } void move_cursor(char c) @@ -90,4 +91,22 @@ void move_cursor(char c) } } +void cursor_input(char c, std::string &buffer) +{ + if (c != '\b') + { + buffer.insert(cursor_col-1, 1, c); + print("\x1b[C"); fflush(stdout); + cursor_col++; + line_end_pos = buffer.size()+1; + return; + } + + // BACKSPACE + buffer.erase(cursor_col-1, 1); + print("\x1b[D"); fflush(stdout); + cursor_col--; + line_end_pos = buffer.size()+1; +} + } diff --git a/src/line/mode.cc b/src/line/mode.cc index a2236e2..18c7347 100644 --- a/src/line/mode.cc +++ b/src/line/mode.cc @@ -105,7 +105,7 @@ string readline() // normal char else { - buffer += c; + cursor_input(c, buffer); print_override(buffer); } } -- cgit v1.3.1-4-g156e