From 75270bdeaa993c3088667d5b3604fe9259bccadc Mon Sep 17 00:00:00 2001 From: _ov4 Date: Wed, 23 Sep 2026 11:57:31 +0000 Subject: add append input for non-empty line --- src/line/cursor.cc | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) (limited to 'src/line/cursor.cc') diff --git a/src/line/cursor.cc b/src/line/cursor.cc index 9ff8bbc..9630d34 100644 --- a/src/line/cursor.cc +++ b/src/line/cursor.cc @@ -40,16 +40,24 @@ T_position get_cursor_position() } pos.row = row; pos.col = col; - print("row {}, col {}\r\n", row, col); - fflush(stdout); + // print("row {}, col {}\r\n", row, col); + // fflush(stdout); return pos; } void print_override(const std::string &s) { + print("\x1b[s"); fflush(stdout); - print("\r"); - fflush(stdout); + print("\r"); fflush(stdout); + + // move to the "start" of the zone + // should skip prompt zone + print("\x1b[{}C", line_begin_pos-1); fflush(stdout); + + // erase rest of it + print("\x1b[K"); fflush(stdout); + print("{}", s); fflush(stdout); //line_end_pos = s.size()+1; @@ -71,19 +79,19 @@ void move_cursor(char c) // cursor_row++; // break; case 'C': - if (cursor_col < line_end_pos) + if (get_cursor_real_pos() < line_end_pos) { print("\x1b[C"); fflush(stdout); - cursor_col++; + line_buffer_index++; } break; case 'D': - if (cursor_col > 1) + if (get_cursor_real_pos() > line_begin_pos) { print("\x1b[D"); fflush(stdout); - cursor_col--; + line_buffer_index--; } break; default: @@ -95,18 +103,23 @@ void cursor_input(char c, std::string &buffer) { if (c != '\b') { - buffer.insert(cursor_col-1, 1, c); + buffer.insert(line_buffer_index, 1, c); print("\x1b[C"); fflush(stdout); - cursor_col++; - line_end_pos = buffer.size()+1; + line_buffer_index++; + line_end_pos = line_begin_pos + buffer.size(); return; } // BACKSPACE - buffer.erase(cursor_col-1, 1); + buffer.erase(line_buffer_index, 1); print("\x1b[D"); fflush(stdout); - cursor_col--; - line_end_pos = buffer.size()+1; + line_buffer_index--; + line_end_pos = line_begin_pos + buffer.size(); +} + +int get_cursor_real_pos() +{ + return line_begin_pos + line_buffer_index; } } -- cgit v1.3.1-4-g156e