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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
use crate::cell::{unicode_column_width, AttributeChange, CellAttributes};
use crate::color::ColorAttribute;
pub use crate::image::{ImageData, TextureCoordinate};
use crate::surface::{CursorShape, Position};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use unicode_segmentation::UnicodeSegmentation;
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Change {
Attribute(AttributeChange),
AllAttributes(CellAttributes),
Text(String),
ClearScreen(ColorAttribute),
ClearToEndOfLine(ColorAttribute),
ClearToEndOfScreen(ColorAttribute),
CursorPosition { x: Position, y: Position },
CursorColor(ColorAttribute),
CursorShape(CursorShape),
Image(Image),
ScrollRegionUp {
first_row: usize,
region_size: usize,
scroll_count: usize,
},
ScrollRegionDown {
first_row: usize,
region_size: usize,
scroll_count: usize,
},
Title(String),
}
impl Change {
pub fn is_text(&self) -> bool {
match self {
Change::Text(_) => true,
_ => false,
}
}
pub fn text(&self) -> &str {
match self {
Change::Text(text) => text,
_ => panic!("you must use Change::is_text() to guard calls to Change::text()"),
}
}
}
impl<S: Into<String>> From<S> for Change {
fn from(s: S) -> Self {
Change::Text(s.into())
}
}
impl From<AttributeChange> for Change {
fn from(c: AttributeChange) -> Self {
Change::Attribute(c)
}
}
pub struct ChangeSequence {
changes: Vec<Change>,
screen_rows: usize,
screen_cols: usize,
pub(crate) cursor_x: usize,
pub(crate) cursor_y: isize,
render_y_max: isize,
render_y_min: isize,
}
impl ChangeSequence {
pub fn new(rows: usize, cols: usize) -> Self {
Self {
changes: vec![],
screen_rows: rows,
screen_cols: cols,
cursor_x: 0,
cursor_y: 0,
render_y_max: 0,
render_y_min: 0,
}
}
pub fn consume(self) -> Vec<Change> {
self.changes
}
pub fn current_cursor_position(&self) -> (usize, isize) {
(self.cursor_x, self.cursor_y)
}
pub fn move_to(&mut self, (cursor_x, cursor_y): (usize, isize)) {
self.add(Change::CursorPosition {
x: Position::Relative(cursor_x as isize - self.cursor_x as isize),
y: Position::Relative(cursor_y - self.cursor_y),
});
}
pub fn render_height(&self) -> usize {
(self.render_y_max - self.render_y_min).max(0).abs() as usize
}
fn update_render_height(&mut self) {
self.render_y_max = self.render_y_max.max(self.cursor_y);
self.render_y_min = self.render_y_min.min(self.cursor_y);
}
pub fn add_changes(&mut self, changes: Vec<Change>) {
for change in changes {
self.add(change);
}
}
pub fn add<C: Into<Change>>(&mut self, change: C) {
let change = change.into();
match &change {
Change::AllAttributes(_)
| Change::Attribute(_)
| Change::CursorColor(_)
| Change::CursorShape(_)
| Change::ClearToEndOfLine(_)
| Change::Title(_)
| Change::ClearToEndOfScreen(_) => {}
Change::Text(t) => {
for g in t.as_str().graphemes(true) {
if self.cursor_x == self.screen_cols {
self.cursor_y += 1;
self.cursor_x = 0;
}
if g == "\n" {
self.cursor_y += 1;
} else if g == "\r" {
self.cursor_x = 0;
} else if g == "\r\n" {
self.cursor_y += 1;
self.cursor_x = 0;
} else {
let len = unicode_column_width(g);
self.cursor_x += len;
}
}
self.update_render_height();
}
Change::Image(im) => {
self.cursor_x += im.width;
self.render_y_max = self.render_y_max.max(self.cursor_y + im.height as isize);
}
Change::ClearScreen(_) => {
self.cursor_x = 0;
self.cursor_y = 0;
}
Change::CursorPosition { x, y } => {
self.cursor_x = match x {
Position::Relative(x) => {
((self.cursor_x as isize + x) % self.screen_cols as isize) as usize
}
Position::Absolute(x) => x % self.screen_cols,
Position::EndRelative(x) => (self.screen_cols - x) % self.screen_cols,
};
self.cursor_y = match y {
Position::Relative(y) => {
(self.cursor_y as isize + y) % self.screen_rows as isize
}
Position::Absolute(y) => (y % self.screen_rows) as isize,
Position::EndRelative(y) => {
((self.screen_rows - y) % self.screen_rows) as isize
}
};
self.update_render_height();
}
Change::ScrollRegionUp { .. } | Change::ScrollRegionDown { .. } => {
self.cursor_x = 0;
self.cursor_y = 0;
}
}
self.changes.push(change);
}
}
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Image {
pub width: usize,
pub height: usize,
pub top_left: TextureCoordinate,
pub bottom_right: TextureCoordinate,
pub image: Arc<ImageData>,
}