diff options
| author | _ov4 <ov4@ov4.org> | 2026-09-22 03:22:34 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-09-22 03:22:34 +0000 |
| commit | 6d7ff828389456a1e73b313afe2b732eb50a207c (patch) | |
| tree | d981e5ee825446701b5e5c98441bc04b5a37b5b0 | |
| parent | a1740da4d2e52c0999c36e82523234fb4f88885a (diff) | |
add cursor insert
| -rw-r--r-- | include/line/cursor.h | 2 | ||||
| -rw-r--r-- | module-test/line_test.cc | 1 | ||||
| -rw-r--r-- | src/line/cursor.cc | 23 | ||||
| -rw-r--r-- | 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); } } |
