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
use crate::{
color::{ApproxBrightness, Color, Color2},
string::TermGrapheme,
};
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Tile {
pub grapheme: TermGrapheme,
pub colors: Color2,
}
pub trait Updater {
fn update(self, tile: &mut Tile);
}
impl Updater for Tile {
fn update(self, tile: &mut Tile) {
*tile = self;
}
}
impl<F> Updater for F
where
F: FnOnce(&mut Tile),
{
fn update(self, tile: &mut Tile) {
self(tile)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Foreground {
pub grapheme: TermGrapheme,
pub color: Color,
}
impl Updater for Foreground {
fn update(self, tile: &mut Tile) {
tile.grapheme = self.grapheme;
tile.colors.foreground = self
.color
.with_approx_brightness(!tile.colors.background.approx_brightness())
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Background {
pub color: Color,
}
impl Updater for Background {
fn update(self, tile: &mut Tile) {
tile.colors.background = self.color;
tile.colors.foreground = tile
.colors
.foreground
.with_approx_brightness(!tile.colors.background.approx_brightness())
}
}