diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-08 14:48:25 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-08 14:48:25 +0000 |
| commit | 0e1493d536c564f5d75a1345fe149cfdf0daee61 (patch) | |
| tree | 871b8753506bd32a20c812621476646fd9aea2ee | |
| parent | cff54544705a99fd526f75418c10fb78503f2243 (diff) | |
fix cursor moving left
| -rw-r--r-- | include/line/cursor.h | 2 | ||||
| -rw-r--r-- | include/line/getter.h | 2 | ||||
| -rw-r--r-- | module-test/line_test.cc | 4 | ||||
| -rw-r--r-- | src/line/cursor.cc | 31 | ||||
| -rw-r--r-- | src/line/getter.cc | 6 | ||||
| -rw-r--r-- | src/line/mode.cc | 1 |
6 files changed, 26 insertions, 20 deletions
diff --git a/include/line/cursor.h b/include/line/cursor.h index 6e62bce..46b8423 100644 --- a/include/line/cursor.h +++ b/include/line/cursor.h @@ -36,8 +36,6 @@ void cursor_input_char(char c); void update_window_size(); -void update_line_end_pos(); - void move_cursor(T_cursor_movement_direction d); } diff --git a/include/line/getter.h b/include/line/getter.h index a1c3765..a572c38 100644 --- a/include/line/getter.h +++ b/include/line/getter.h @@ -9,6 +9,8 @@ int get_terminal_pos_cursor(); int get_buffer_index_display_begin(); int get_buffer_index_display_end(); +void update_line_end_pos(); + T_position get_cursor_position(); }
\ No newline at end of file diff --git a/module-test/line_test.cc b/module-test/line_test.cc index 874b5cf..ea149ff 100644 --- a/module-test/line_test.cc +++ b/module-test/line_test.cc @@ -4,6 +4,7 @@ module-test/line_test.cc \ src/line/mode.cc \ src/line/cursor.cc \ + src/line/getter.cc \ -std=c++26 -Iinclude -Ithird_party/magic_enum/include \ -O0 -ggdb3 -fno-omit-frame-pointer -fno-inline -D_GLIBCXX_ASSERTIONS \ -o module-test/line_test.out @@ -17,6 +18,8 @@ #include "line/mode.h" #include "line/cursor.h" +#include "line/getter.h" +#include "line/type.h" using namespace std; using namespace ov4; @@ -28,6 +31,7 @@ int main() T_position pos = get_cursor_position(); cursor_row = pos.row; cursor_col = pos.col; + prompt_pos = cursor_col; string s = readline(); disable_raw(); cout << "\n\n\n\n" << endl; diff --git a/src/line/cursor.cc b/src/line/cursor.cc index 05311ad..3c701e5 100644 --- a/src/line/cursor.cc +++ b/src/line/cursor.cc @@ -9,6 +9,8 @@ #include <termios.h> #include <sys/ioctl.h> +#include "line/getter.h" + using namespace std; namespace ov4 @@ -27,7 +29,8 @@ void print_override(const std::string &s) // erase rest of the line print("\x1b[K"); fflush(stdout); - print("{}", s.substr(buffer_index_display_begin, buffer_index_display_end-buffer_index_display_begin+1)); fflush(stdout); + print("{}", s.substr(get_buffer_index_display_begin(), + get_buffer_index_display_end() - get_buffer_index_display_begin() + 1)); fflush(stdout); //line_end_pos = s.size()+1; // restore cursor position @@ -82,11 +85,11 @@ void cursor_input_char(char c) // print("\x1b[C"); fflush(stdout); // move cursor to the right // buffer_index_cursor++; update_line_end_pos(); - int end_possible_buffer_index = window_size_col-prompt_pos-1; - buffer_index_display_end = - (end_possible_buffer_index > (buffer_index_display_end)+1) - ? (buffer_index_display_end)+1 - : end_possible_buffer_index; + // int end_possible_buffer_index = window_size_col-prompt_pos-1; + // buffer_index_display_end = + // (end_possible_buffer_index > (buffer_index_display_end)+1) + // ? (buffer_index_display_end)+1 + // : end_possible_buffer_index; move_cursor(T_cursor_movement_direction::RIGHT); return; @@ -114,19 +117,13 @@ void update_window_size() window_size_col = ws.ws_col; } -void update_line_end_pos() -{ - line_end_pos = prompt_pos + line_buffer.size(); - line_end_pos = (line_end_pos > window_size_col) ? window_size_col : line_end_pos; -} - void move_cursor(T_cursor_movement_direction d) { switch (d) { case T_cursor_movement_direction::LEFT: // if cursor is not at left margin - if (prompt_pos < (buffer_index_cursor+1)) + if (prompt_pos < get_terminal_pos_cursor()) { buffer_index_cursor--; print("\x1b[D"); fflush(stdout); // move cursor to the left @@ -135,14 +132,13 @@ void move_cursor(T_cursor_movement_direction d) else { buffer_index_cursor--; - buffer_index_display_begin--; - buffer_index_display_end--; + _buffer_index_display_begin--; } break; case T_cursor_movement_direction::RIGHT: // if cursor is not at right margin - if (line_end_pos > (buffer_index_cursor+1)) + if (line_end_pos > get_terminal_pos_cursor()) { buffer_index_cursor++; print("\x1b[C"); fflush(stdout); // move cursor to the right @@ -151,8 +147,7 @@ void move_cursor(T_cursor_movement_direction d) else { buffer_index_cursor++; - buffer_index_display_begin++; - buffer_index_display_end++; + _buffer_index_display_begin++; } break; diff --git a/src/line/getter.cc b/src/line/getter.cc index 2ef9723..50456d8 100644 --- a/src/line/getter.cc +++ b/src/line/getter.cc @@ -33,6 +33,12 @@ int get_buffer_index_display_end() return min(max_possible_end, window_possible_end); } +void update_line_end_pos() +{ + line_end_pos = prompt_pos + line_buffer.size(); + line_end_pos = (line_end_pos > window_size_col) ? window_size_col : line_end_pos; +} + T_position get_cursor_position() { diff --git a/src/line/mode.cc b/src/line/mode.cc index e3f071c..e8024fa 100644 --- a/src/line/mode.cc +++ b/src/line/mode.cc @@ -6,6 +6,7 @@ #include <print> #include "line/cursor.h" +#include "line/getter.h" using namespace std; |
