diff options
| author | _ov4 <ov4@ov4.org> | 2026-09-24 14:34:57 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-09-24 14:34:57 +0000 |
| commit | d813a851ac9e642eff718882702d383bae2134b2 (patch) | |
| tree | 1ccbe59640633aa5e379adb6fb1f40fd2f50696c /src | |
| parent | 75270bdeaa993c3088667d5b3604fe9259bccadc (diff) | |
add index in buffer
Diffstat (limited to 'src')
| -rw-r--r-- | src/line/cursor.cc | 24 | ||||
| -rw-r--r-- | src/line/mode.cc | 2 |
2 files changed, 24 insertions, 2 deletions
diff --git a/src/line/cursor.cc b/src/line/cursor.cc index 9630d34..64c3da8 100644 --- a/src/line/cursor.cc +++ b/src/line/cursor.cc @@ -7,6 +7,7 @@ #include <cstdio> #include <cstdlib> #include <termios.h> +#include <sys/ioctl.h> using namespace std; @@ -101,12 +102,14 @@ void move_cursor(char c) void cursor_input(char c, std::string &buffer) { + // normal input if (c != '\b') { buffer.insert(line_buffer_index, 1, c); print("\x1b[C"); fflush(stdout); line_buffer_index++; - line_end_pos = line_begin_pos + buffer.size(); + buffer_len = buffer.size(); + update_line_end_pos(); return; } @@ -114,7 +117,8 @@ void cursor_input(char c, std::string &buffer) buffer.erase(line_buffer_index, 1); print("\x1b[D"); fflush(stdout); line_buffer_index--; - line_end_pos = line_begin_pos + buffer.size(); + buffer_len = buffer.size(); + update_line_end_pos(); } int get_cursor_real_pos() @@ -122,4 +126,20 @@ int get_cursor_real_pos() return line_begin_pos + line_buffer_index; } +void update_window_size() +{ + winsize ws; + if (ioctl(1, TIOCGWINSZ, &ws) == -1) + exit(-1); // TODO: fix + + window_size_row = ws.ws_row; + window_size_col = ws.ws_col; +} + +void update_line_end_pos() +{ + line_end_pos = line_begin_pos + buffer_len; + line_end_pos = (line_end_pos > window_size_col) ? window_size_col : line_end_pos; +} + } diff --git a/src/line/mode.cc b/src/line/mode.cc index 4122411..b61965d 100644 --- a/src/line/mode.cc +++ b/src/line/mode.cc @@ -68,6 +68,8 @@ void clear_below() string readline() { + update_window_size(); + T_position pos = get_cursor_position(); line_begin_pos = pos.col; string buffer; |
