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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! 2D screen structures and arithmetics
use std::{borrow::Borrow, fmt, ops};
/// Represents a 2D rectangle in a terminal screen
#[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Hash)]
pub struct Window {
/// screen cell left position, zero based
pub col: u16,
/// screen cell top position, zero based
pub row: u16,
/// screen cell width
pub width: u16,
/// screen cell height
pub height: u16,
}
/// Represents 2D coordinates in a terminal screen
#[derive(Default, Debug, Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Hash)]
pub struct Point {
/// screen cell left position or width, zero based
pub col: u16,
/// screen cell top position or height, zero based
pub row: u16,
}
impl From<(u16, u16)> for Point {
fn from(p: (u16, u16)) -> Self {
point(p.0, p.1)
}
}
/// New 2D coordinates for a terminal screen
pub fn point(col: u16, row: u16) -> Point {
Point { col, row }
}
/// New 2D rectangle for a terminal screen
pub fn window(position: Point, size: Point) -> Window {
Window {
col: position.col,
row: position.row,
width: size.col,
height: size.row,
}
}
impl Point {
/// create a window the size of the point position
pub fn window(&self) -> Window {
Window {
col: 0,
row: 0,
width: self.col,
height: self.row,
}
}
/// The total number of discrete points on the terminal screen
pub fn area(&self) -> u32 {
self.col as u32 * self.row as u32
}
/// Is the given coordinate smaller or equal to self?
pub fn contains(&self, o: &Point) -> bool {
self.col > o.col && self.row > o.row
}
/// Check if the point appears withing the given window
pub fn is_in<T: Borrow<Window>>(&self, w: T) -> bool {
let w = w.borrow();
if w.col <= self.col
&& w.row <= self.row
&& w.col.saturating_add(w.width) > self.col
&& w.row.saturating_add(w.height) > self.row
{
true
} else {
false
}
}
/// What is the col/row coordinate of the nth point within the bounds of self?
pub fn point_at_idx(&self, idx: u32) -> Option<Point> {
if idx >= self.area() {
None
} else {
let col = (idx % self.col as u32) as u16;
let row = (idx / self.col as u32) as u16;
Some(point(col, row))
}
}
/// What is the order index of the given coordinates within the bounds of self?
pub fn idx_at_point(&self, p: Point) -> Option<u32> {
if p.row >= self.row || p.col >= self.col {
None
} else {
let w = self.col as u32;
let x = p.col as u32;
let y = p.row as u32;
Some(y * w + x)
}
}
}
impl Window {
/// Crop to a relative overlapping subset of the window.
///
/// ```diagram
/// my row
/// |
/// v
/// |--------------------|
/// | + |
/// | self +row |
/// | + |
/// my col ----> | v |
/// | {================}
/// |++col++++> {XXXXXXXX| }
/// | {XresultX| }
/// | {XXXXXXXX| }
/// |-----------{--------| }
/// { }
/// { relative other }
/// {================}
/// ```
pub fn relative_crop(&self, w: impl Borrow<Window>) -> Window {
let w = w.borrow() + self.position();
self.intersection(w)
}
/// Crop to an absolute overlapping subset of the window.
///
/// ```diagram
///
/// my row other row
/// | +
/// v +
/// |--------------+-----|
/// | + |
/// | self + |
/// | + |
/// my col ----> | v |
/// | {================}
/// other col +++++++++++++> {XXXXXXXX| }
/// | {XresultX| }
/// | {XXXXXXXX| }
/// |-----------{--------| }
/// { }
/// { absolute other }
/// {================}
/// ```
pub fn intersection(&self, w: impl Borrow<Window>) -> Window {
let o = w.borrow();
let pos = point(u16::max(self.col, o.col), u16::max(self.row, o.row));
window(
pos,
point(
u16::min(
self.col.saturating_add(self.width).saturating_sub(pos.col),
o.col.saturating_add(o.width).saturating_sub(pos.col),
),
u16::min(
self.row.saturating_add(self.height).saturating_sub(pos.row),
o.row.saturating_add(o.height).saturating_sub(pos.row),
),
),
)
}
/// Check if the given point appears withing the window
/// and if it does, return same, otherwise none
pub fn contains_point<T: Borrow<Point>>(&self, p: T) -> Option<T> {
if p.borrow().is_in(self) {
Some(p)
} else {
None
}
}
/// Restrict the window to given space
///
/// ```diagram
///
/// my row trim col
/// | +
/// v +
/// XXXXXXXXXXXXXX+------
/// XXXXXXXXXXXXXX+ |
/// XXXXXXXXXXXXXX+ |
/// XXXXresultXXXX+ |
/// my col ----> XXXXXXXXXXXXXX+ |
/// XXXXXXXXXXXXXX+ |
/// trim row +++++++++++++++++++ |
/// | |
/// | self |
/// ---------------------
///
/// ```
pub fn trim(&self, p: impl Borrow<Point>) -> Window {
self.intersection(p.borrow().window())
}
/// The col/row coordinates of the bottom right corner
pub fn bottom_right(&self) -> Point {
point(
self.col.saturating_add(self.width),
self.row.saturating_add(self.height),
)
}
/// The col/row coordinates of the top left corner
pub fn position(&self) -> Point {
point(self.col, self.row)
}
/// The width/height coordinates of the rectangle
pub fn size(&self) -> Point {
point(self.width, self.height)
}
/// If the window has zero area
pub fn is_empty(&self) -> bool {
self.width == 0 || self.height == 0
}
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "c{} r{} ", self.col, self.row)
}
}
impl fmt::Display for Window {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"c{} r{} w{} h{}",
self.col, self.row, self.width, self.height
)
}
}
impl<RHS: Borrow<Window>> ops::BitAnd<RHS> for Window {
type Output = Window;
/// get the intersection of the two windows
fn bitand(self, rhs: RHS) -> Self::Output {
(&self).bitand(rhs)
}
}
impl<RHS: Borrow<Window>> ops::BitAnd<RHS> for &Window {
type Output = Window;
/// get the intersection of the two windows
fn bitand(self, o: RHS) -> Self::Output {
self.intersection(o)
}
}
impl ops::SubAssign for Point {
/// move point closer by given coordinates
fn sub_assign(&mut self, o: Self) {
*self = &*self - o
}
}
impl ops::AddAssign for Point {
/// move point away by given coordinates
fn add_assign(&mut self, o: Self) {
*self = &*self + o
}
}
impl<RHS: Borrow<Point>> ops::Sub<RHS> for Point {
type Output = Point;
/// move point closer by given coordinates
fn sub(self, o: RHS) -> Self::Output {
(&self).sub(o)
}
}
impl<RHS: Borrow<Point>> ops::Sub<RHS> for &Point {
type Output = Point;
/// move point closer by given coordinates
fn sub(self, o: RHS) -> Self::Output {
let o = o.borrow();
point(
self.col.saturating_sub(o.col),
self.row.saturating_sub(o.row),
)
}
}
impl<RHS: Borrow<Point>> ops::Add<RHS> for Point {
type Output = Point;
/// move point away by given coordinates
fn add(self, o: RHS) -> Self::Output {
(&self).add(o)
}
}
impl<RHS: Borrow<Point>> ops::Add<RHS> for &Point {
type Output = Point;
/// move point away by given coordinates
fn add(self, o: RHS) -> Self::Output {
let o = o.borrow();
point(
self.col.saturating_add(o.col),
self.row.saturating_add(o.row),
)
}
}
impl ops::AddAssign<Point> for Window {
/// move window away by given coordinates
fn add_assign(&mut self, o: Point) {
*self = &*self + o
}
}
impl ops::SubAssign<Point> for Window {
/// move window closer by given coordinates
fn sub_assign(&mut self, o: Point) {
*self = &*self - o
}
}
impl<RHS: Borrow<Point>> ops::Sub<RHS> for Window {
type Output = Window;
/// move window closer by given coordinates
fn sub(self, o: RHS) -> Self::Output {
(&self).sub(o)
}
}
impl<RHS: Borrow<Point>> ops::Sub<RHS> for &Window {
type Output = Window;
/// move window closer by given coordinates
fn sub(self, o: RHS) -> Self::Output {
let o = o.borrow();
window(
point(
self.col.saturating_sub(o.col),
self.row.saturating_sub(o.row),
),
self.size(),
)
}
}
impl<RHS: Borrow<Point>> ops::Add<RHS> for Window {
type Output = Window;
/// move window away by given coordinates
fn add(self, o: RHS) -> Self::Output {
(&self).add(o)
}
}
impl<RHS: Borrow<Point>> ops::Add<RHS> for &Window {
type Output = Window;
/// move window away by given coordinates
fn add(self, o: RHS) -> Self::Output {
let o = o.borrow();
window(
point(
self.col.saturating_add(o.col),
self.row.saturating_add(o.row),
),
self.size(),
)
}
}
#[test]
fn test_window_relative_crop() {
let op = window(point(0, 0), point(10_000, 30_000));
let crop = window(point(10, 3), point(100, 300)).relative_crop(&op);
assert_eq!(crop.col, (10));
assert_eq!(crop.row, (3));
assert_eq!(crop.width, (100));
assert_eq!(crop.height, (300));
let op = window(point(10, 3), point(100, 300));
let crop = window(point(0, 0), point(10_000, 30_000)).relative_crop(&op);
assert_eq!(crop.col, (10));
assert_eq!(crop.row, (3));
assert_eq!(crop.width, (100));
assert_eq!(crop.height, (300));
}
#[test]
fn test_window_intersection() {
let a = window(point(50, 30), point(5, 3));
let b = window(point(51, 31), point(100, 300));
let crop = a.intersection(b);
assert_eq!(crop.col, (51));
assert_eq!(crop.row, (31));
assert_eq!(crop.width, (4));
assert_eq!(crop.height, (2));
let c = window(point(1, 1), point(1, 1));
let crop = a & c;
assert_eq!(crop.col, (50));
assert_eq!(crop.row, (30));
assert_eq!(crop.width, (0));
assert_eq!(crop.height, (0));
let crop_commutative = c & a;
assert_eq!(crop_commutative, crop)
}
#[test]
fn test_size_contains() {
let p = point(3, 2);
assert_eq!(p.contains(&point(0, 0)), true);
assert_eq!(p.contains(&point(2, 1)), true);
assert_eq!(p.contains(&point(3, 1)), false);
assert_eq!(p.contains(&point(2, 2)), false);
}
#[test]
fn test_idx_at_point() {
let p = point(3, 2);
assert_eq!(p.idx_at_point(point(0, 0)), Some(0));
assert_eq!(p.idx_at_point(point(1, 0)), Some(1));
assert_eq!(p.idx_at_point(point(1, 1)), Some(4));
assert_eq!(p.idx_at_point(point(2, 1)), Some(5));
assert_eq!(p.idx_at_point(point(3, 1)), None);
assert_eq!(p.idx_at_point(point(1, 2)), None);
assert_eq!(p.point_at_idx(0), Some(point(0, 0)));
assert_eq!(p.point_at_idx(5), Some(point(2, 1)));
assert_eq!(p.point_at_idx(6), None);
}