diff options
Diffstat (limited to 'src/line/getter.cc')
| -rw-r--r-- | src/line/getter.cc | 69 |
1 files changed, 69 insertions, 0 deletions
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 |
