diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-08 14:08:46 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-08 14:08:46 +0000 |
| commit | 01bf68b3bc34a685823a352eb09ed5717a3b4961 (patch) | |
| tree | 1835e8848cfe70ee52229c7349eefaf1a7296373 | |
| parent | 21bf679e4c5fcd72a0337d23cc3e0d15c18315ff (diff) | |
prepare for page
| -rw-r--r-- | include/line/cursor.h | 18 | ||||
| -rw-r--r-- | src/line/cursor.cc | 110 | ||||
| -rw-r--r-- | src/line/mode.cc | 13 |
3 files changed, 104 insertions, 37 deletions
diff --git a/include/line/cursor.h b/include/line/cursor.h index cdea934..7cae165 100644 --- a/include/line/cursor.h +++ b/include/line/cursor.h @@ -13,6 +13,12 @@ struct T_position int col; }; +enum class T_cursor_movement_direction +{ + LEFT, + RIGHT, +}; + // 1-based inline int cursor_row; inline int cursor_col; @@ -23,17 +29,23 @@ inline int window_size_row; inline int window_size_col; // 0-based index -inline int line_buffer_index; +inline int line_buffer_index_cursor; +// index [begin, end] is displayed +// NB: CLOSE interval +inline int line_buffer_index_begin; +inline int line_buffer_index_end; inline int buffer_len; +inline std::string line_buffer; + T_position get_cursor_position(); void print_override(const std::string &s); void move_cursor(char c); -void cursor_input(char c, std::string &buffer); +void cursor_input_char(char c); int get_cursor_real_pos(); @@ -41,5 +53,7 @@ void update_window_size(); void update_line_end_pos(); +void move_cursor(T_cursor_movement_direction d); + } diff --git a/src/line/cursor.cc b/src/line/cursor.cc index 64c3da8..1489d61 100644 --- a/src/line/cursor.cc +++ b/src/line/cursor.cc @@ -48,20 +48,21 @@ T_position get_cursor_position() void print_override(const std::string &s) { - + // save current cursor position print("\x1b[s"); fflush(stdout); - print("\r"); fflush(stdout); // move to the "start" of the zone // should skip prompt zone + print("\r"); fflush(stdout); print("\x1b[{}C", line_begin_pos-1); fflush(stdout); - // erase rest of it + // erase rest of the line print("\x1b[K"); fflush(stdout); - print("{}", s); - fflush(stdout); + print("{}", s.substr(line_buffer_index_begin, line_buffer_index_end-line_buffer_index_begin+1)); fflush(stdout); //line_end_pos = s.size()+1; + + // restore cursor position print("\x1b[u"); fflush(stdout); } @@ -80,50 +81,62 @@ void move_cursor(char c) // cursor_row++; // break; case 'C': - if (get_cursor_real_pos() < line_end_pos) - { - print("\x1b[C"); - fflush(stdout); - line_buffer_index++; - } + // if (get_cursor_real_pos() < line_end_pos) + // { + // // print("\x1b[C"); + // // fflush(stdout); + // // line_buffer_index_cursor++; + // move_cursor(T_cursor_movement_direction::RIGHT); + // } + move_cursor(T_cursor_movement_direction::RIGHT); break; case 'D': - if (get_cursor_real_pos() > line_begin_pos) - { - print("\x1b[D"); - fflush(stdout); - line_buffer_index--; - } + // if (get_cursor_real_pos() > line_begin_pos) + // { + // // print("\x1b[D"); + // // fflush(stdout); + // // line_buffer_index_cursor--; + // move_cursor(T_cursor_movement_direction::LEFT); + // } + move_cursor(T_cursor_movement_direction::LEFT); break; default: return; } } -void cursor_input(char c, std::string &buffer) +void cursor_input_char(char c) { // normal input if (c != '\b') { - buffer.insert(line_buffer_index, 1, c); - print("\x1b[C"); fflush(stdout); - line_buffer_index++; - buffer_len = buffer.size(); + line_buffer.insert(line_buffer_index_cursor, 1, c); + // print("\x1b[C"); fflush(stdout); // move cursor to the right + // line_buffer_index_cursor++; update_line_end_pos(); + int end_possible_buffer_index = window_size_col-line_begin_pos-1; + line_buffer_index_end = + (end_possible_buffer_index > (line_buffer_index_end)+1) + ? (line_buffer_index_end)+1 + : end_possible_buffer_index; + move_cursor(T_cursor_movement_direction::RIGHT); + return; } // BACKSPACE - buffer.erase(line_buffer_index, 1); - print("\x1b[D"); fflush(stdout); - line_buffer_index--; - buffer_len = buffer.size(); + line_buffer.erase(line_buffer_index_cursor, 1); + // print("\x1b[D"); fflush(stdout); // move cursor to the left + // line_buffer_index_cursor--; update_line_end_pos(); + // TODO: backspace page + move_cursor(T_cursor_movement_direction::LEFT); + } int get_cursor_real_pos() { - return line_begin_pos + line_buffer_index; + return line_begin_pos + line_buffer_index_cursor; } void update_window_size() @@ -138,8 +151,49 @@ void update_window_size() void update_line_end_pos() { - line_end_pos = line_begin_pos + buffer_len; + line_end_pos = line_begin_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 (line_begin_pos < (line_buffer_index_cursor+1)) + { + line_buffer_index_cursor--; + print("\x1b[D"); fflush(stdout); // move cursor to the left + } + // margin + else + { + line_buffer_index_cursor--; + line_buffer_index_begin--; + line_buffer_index_end--; + } + break; + + case T_cursor_movement_direction::RIGHT: + // if cursor is not at right margin + if (line_end_pos > (line_buffer_index_cursor+1)) + { + line_buffer_index_cursor++; + print("\x1b[C"); fflush(stdout); // move cursor to the right + } + // margin + else + { + line_buffer_index_cursor++; + line_buffer_index_begin++; + line_buffer_index_end++; + } + break; + + default: + exit(-1); // TODO: fix + } +} + } diff --git a/src/line/mode.cc b/src/line/mode.cc index 80e8531..7a91528 100644 --- a/src/line/mode.cc +++ b/src/line/mode.cc @@ -20,7 +20,7 @@ void disable_raw() void enable_raw() { - if (raw_enabled == false) return; + if (raw_enabled) return; raw_enabled = true; termios attr_raw; @@ -72,7 +72,6 @@ string readline() T_position pos = get_cursor_position(); line_begin_pos = pos.col; - string buffer; char c; while (read(STDIN_FILENO, &c, 1) == 1) { @@ -91,8 +90,8 @@ string readline() //fflush(stdout); if (c == 13) // \n { - //print("line: {}\r\n", buffer); - return buffer; + //print("line: {}\r\n", line_buffer); + return line_buffer; } // ESC sequence @@ -113,11 +112,11 @@ string readline() // normal char else { - cursor_input(c, buffer); - print_override(buffer); + cursor_input_char(c); + print_override(line_buffer); } } - return buffer; + return line_buffer; } |
