diff options
Diffstat (limited to 'src/line')
| -rw-r--r-- | src/line/cursor.cc | 85 | ||||
| -rw-r--r-- | src/line/getter.cc | 69 | ||||
| -rw-r--r-- | src/line/mode.cc | 2 |
3 files changed, 95 insertions, 61 deletions
diff --git a/src/line/cursor.cc b/src/line/cursor.cc index 1489d61..05311ad 100644 --- a/src/line/cursor.cc +++ b/src/line/cursor.cc @@ -14,38 +14,6 @@ using namespace std; namespace ov4 { -T_position get_cursor_position() -{ - char c; - string buffer; - T_position pos; - int row, col; - tcflush(STDIN_FILENO, TCIFLUSH); - cout << "\x1b[6n" << flush; - while (read(STDIN_FILENO, &c, 1) == 1) - { - if (c == 'R') break; - buffer += c; - } - if (buffer.size() <= 2 || buffer[0] != '\x1b' || buffer[1] != '[') - { - pos.row = -1; - pos.col = -1; - return pos; - } - if ((sscanf(buffer.c_str(), "\x1b[%d;%d", &row, &col)) != 2) - { - pos.row = -1; - pos.col = -1; - return pos; - } - pos.row = row; - pos.col = col; - // print("row {}, col {}\r\n", row, col); - // fflush(stdout); - return pos; -} - void print_override(const std::string &s) { // save current cursor position @@ -54,12 +22,12 @@ void print_override(const std::string &s) // move to the "start" of the zone // should skip prompt zone print("\r"); fflush(stdout); - print("\x1b[{}C", line_begin_pos-1); fflush(stdout); + print("\x1b[{}C", prompt_pos-1); fflush(stdout); // erase rest of the line print("\x1b[K"); fflush(stdout); - print("{}", s.substr(line_buffer_index_begin, line_buffer_index_end-line_buffer_index_begin+1)); fflush(stdout); + print("{}", s.substr(buffer_index_display_begin, buffer_index_display_end-buffer_index_display_begin+1)); fflush(stdout); //line_end_pos = s.size()+1; // restore cursor position @@ -85,17 +53,17 @@ void move_cursor(char c) // { // // print("\x1b[C"); // // fflush(stdout); - // // line_buffer_index_cursor++; + // // 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) + // if (get_cursor_real_pos() > prompt_pos) // { // // print("\x1b[D"); // // fflush(stdout); - // // line_buffer_index_cursor--; + // // buffer_index_cursor--; // move_cursor(T_cursor_movement_direction::LEFT); // } move_cursor(T_cursor_movement_direction::LEFT); @@ -110,14 +78,14 @@ void cursor_input_char(char c) // normal input if (c != '\b') { - line_buffer.insert(line_buffer_index_cursor, 1, c); + line_buffer.insert(buffer_index_cursor, 1, c); // print("\x1b[C"); fflush(stdout); // move cursor to the right - // line_buffer_index_cursor++; + // 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 + 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); @@ -125,19 +93,16 @@ void cursor_input_char(char c) } // BACKSPACE - line_buffer.erase(line_buffer_index_cursor, 1); + line_buffer.erase(buffer_index_cursor, 1); // print("\x1b[D"); fflush(stdout); // move cursor to the left - // line_buffer_index_cursor--; + // 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_cursor; -} + void update_window_size() { @@ -151,7 +116,7 @@ void update_window_size() void update_line_end_pos() { - line_end_pos = line_begin_pos + line_buffer.size(); + line_end_pos = prompt_pos + line_buffer.size(); line_end_pos = (line_end_pos > window_size_col) ? window_size_col : line_end_pos; } @@ -161,33 +126,33 @@ void move_cursor(T_cursor_movement_direction d) { case T_cursor_movement_direction::LEFT: // if cursor is not at left margin - if (line_begin_pos < (line_buffer_index_cursor+1)) + if (prompt_pos < (buffer_index_cursor+1)) { - line_buffer_index_cursor--; + 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--; + buffer_index_cursor--; + buffer_index_display_begin--; + buffer_index_display_end--; } break; case T_cursor_movement_direction::RIGHT: // if cursor is not at right margin - if (line_end_pos > (line_buffer_index_cursor+1)) + if (line_end_pos > (buffer_index_cursor+1)) { - line_buffer_index_cursor++; + 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++; + buffer_index_cursor++; + buffer_index_display_begin++; + buffer_index_display_end++; } break; diff --git a/src/line/getter.cc b/src/line/getter.cc new file mode 100644 index 0000000..2ef9723 --- /dev/null +++ b/src/line/getter.cc @@ -0,0 +1,69 @@ +#include "line/getter.h" + +#include <cstdio> +#include <string> +#include <termios.h> +#include <sys/ioctl.h> +#include <iostream> +#include <unistd.h> +#include <cmath> + +#include "line/cursor.h" + + +using namespace std; + +namespace ov4 +{ + +int get_terminal_pos_cursor() +{ + return prompt_pos + buffer_index_cursor; +} + +int get_buffer_index_display_begin() +{ + return _buffer_index_display_begin; +} + +int get_buffer_index_display_end() +{ + int max_possible_end = get_buffer_index_display_begin() + line_buffer.size() - 1; + int window_possible_end = get_buffer_index_display_begin() + (window_size_col - prompt_pos) -1; + return min(max_possible_end, window_possible_end); +} + + +T_position get_cursor_position() +{ + char c; + string buffer; + T_position pos; + int row, col; + tcflush(STDIN_FILENO, TCIFLUSH); + cout << "\x1b[6n" << flush; + while (read(STDIN_FILENO, &c, 1) == 1) + { + if (c == 'R') break; + buffer += c; + } + if (buffer.size() <= 2 || buffer[0] != '\x1b' || buffer[1] != '[') + { + pos.row = -1; + pos.col = -1; + return pos; + } + if ((sscanf(buffer.c_str(), "\x1b[%d;%d", &row, &col)) != 2) + { + pos.row = -1; + pos.col = -1; + return pos; + } + pos.row = row; + pos.col = col; + // print("row {}, col {}\r\n", row, col); + // fflush(stdout); + return pos; +} + +}
\ No newline at end of file diff --git a/src/line/mode.cc b/src/line/mode.cc index 7a91528..e3f071c 100644 --- a/src/line/mode.cc +++ b/src/line/mode.cc @@ -71,7 +71,7 @@ string readline() update_window_size(); T_position pos = get_cursor_position(); - line_begin_pos = pos.col; + prompt_pos = pos.col; char c; while (read(STDIN_FILENO, &c, 1) == 1) { |
