aboutsummaryrefslogtreecommitdiff
path: root/src/line/getter.cc
blob: 432ddca79cd221dd0b802ae0058f47458e45f626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

#include "line/line_editor.h"

#include <cstdio>
#include <string>
#include <termios.h>
#include <sys/ioctl.h>
#include <iostream>
#include <unistd.h>
#include <cmath>


using namespace std;

namespace ov4
{

int T_line_editor::get_terminal_pos_cursor()
{
    return prompt_pos + (buffer_index_cursor - get_buffer_index_display_begin()) + 1;
}

int T_line_editor::get_buffer_index_display_begin()
{
    return _buffer_index_display_begin;
}

int T_line_editor::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);
}

int T_line_editor::get_terminal_pos_line_end()
{
    int possible = prompt_pos + line_buffer.size();
    return min(possible, window_size_col);
}


T_position T_line_editor::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;
} 
    
}