-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvterm.cpp
More file actions
131 lines (103 loc) · 2.77 KB
/
vterm.cpp
File metadata and controls
131 lines (103 loc) · 2.77 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "vterm.hpp"
namespace app {
VTerm::VTerm(int _rows, int _cols)
: window(_rows, _cols)
{
cell.fg_col = 0xFFFFFFFF;
cell.bg_col = 0x000000FF;
}
void VTerm::resize(int _rows, int _cols) {
if(scroll_row_begin == 0 && scroll_row_end == rows) {
scroll_row_end = _rows;
}
rows = _rows;
cols = _cols;
window.resize_term(rows, cols);
window.redraw();
}
void VTerm::overwriteglyph(const char *input, size_t len) {
cell.glyph.assign(input, len);
window.set_cell(row, col, cell);
}
void VTerm::start_new_row() {
// If the next row has put us beyond the scroll region:
if (row == scroll_row_end) {
// scroll up and start the last line again.
window.scroll(scroll_row_begin, scroll_row_end, gfx::Direction::UP, 1);
row = scroll_row_end-1;
}
}
void VTerm::insert_lines(int num) {
scroll_down(num);
}
void VTerm::delete_lines(int num) {
scroll_up(num);
}
void VTerm::scroll_up(int num) {
// scroll [row, scroll_row_end) up num times
window.scroll(row, scroll_row_end, gfx::Direction::UP, num);
}
void VTerm::scroll_down(int num) {
// scroll [row, scroll_row_end) down num times
window.scroll(row, scroll_row_end, gfx::Direction::DOWN, num);
}
void VTerm::putglyph(const char *input, size_t len) {
// If we are of the rightmost column, we start the next row.
if (col >= cols) {
col = 0;
row++;
start_new_row();
}
overwriteglyph(input, len);
col++;
}
void clamp(int &v, int min, int max) {
if (v > max)
v = max;
else if (v < min)
v = min;
}
void curs_clamp(int &row, int &col, int rows, int cols) {
clamp(row, 0, rows-1);
clamp(col, 0, cols-1);
}
void VTerm::curs_newline() {
row++;
//col = 0;
start_new_row();
curs_clamp(row, col, rows, cols);
}
void VTerm::curs_to_col(int _col) {
col = _col;
curs_clamp(row, col, rows, cols);
}
void VTerm::curs_to_row(int _row) {
row = _row;
curs_clamp(row, col, rows, cols);
}
void VTerm::curs_backspace() {
col--;
curs_clamp(row, col, rows, cols);
}
bool &get_attr(gfx::TermCell &cell, gfx::TermCell::Attr attr) {
static bool b = false;
using Attr = gfx::TermCell::Attr;
// clang-format off
switch (attr) {
case Attr::BOLD: return cell.bold;
case Attr::ITALIC: return cell.italic;
case Attr::OVERLINE: return cell.overline;
case Attr::UNDERLINE: return cell.underline;
case Attr::DUNDERLINE: return cell.dunderline;
case Attr::STRIKE: return cell.strike;
case Attr::FEINT: return cell.feint;
case Attr::REVERSE: return cell.reverse;
case Attr::FG: case Attr::BG: default: return b;
}
// clang-format on
}
void VTerm::cell_set_(gfx::TermCell::Attr attr) { get_attr(cell, attr) = true; }
void VTerm::cell_reset_(gfx::TermCell::Attr attr) {
get_attr(cell, attr) = get_attr(reset, attr);
}
} // namespace app