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
#![cfg_attr(not(any(test, feature = "use-std")), no_std)]

use embedded_graphics::{
    mono_font::MonoTextStyle,
    prelude::{DrawTarget, Drawable, PixelColor, Point, Size},
    primitives::{PrimitiveStyleBuilder, Rectangle, StyledDrawable},
    text::Text,
};
use input_mgr::{RingLine, Source};

#[derive(Clone)]
pub struct ColorStyle<'font, ColorKind: PixelColor> {
    pub background: ColorKind,
    pub local_editing_font: MonoTextStyle<'font, ColorKind>,
    pub remote_editing_font: MonoTextStyle<'font, ColorKind>,
    pub local_history_font: MonoTextStyle<'font, ColorKind>,
    pub remote_history_font: MonoTextStyle<'font, ColorKind>,
    pub local_editing_background: ColorKind,
    pub remote_editing_background: ColorKind,
    pub local_history_background: ColorKind,
    pub remote_history_background: ColorKind,
    pub margin_chars: u32,
}

pub fn drawer_color<'font, ColorKind, Display, const WIDTH: usize, const HEIGHT: usize>(
    disp: &mut Display,
    rline: &RingLine<HEIGHT, WIDTH>,
    style: ColorStyle<'font, ColorKind>,
) -> Result<(), <Display as DrawTarget>::Error>
where
    ColorKind: PixelColor,
    Display: DrawTarget<Color = ColorKind>,
{
    let full_display = disp.bounding_box();

    let local_edit_char_pixels_y = style.local_editing_font.font.character_size.height;
    let remote_edit_char_pixels_y = style.remote_editing_font.font.character_size.height;
    let local_hist_char_pixels_y = style.local_history_font.font.character_size.height;
    let remote_hist_char_pixels_y = style.remote_history_font.font.character_size.height;

    // Blank the background
    let mut y_idx: u32 = full_display.size.height;
    let x_width = full_display.size.width;
    disp.fill_solid(&full_display, style.background)?;

    let left_margin_px = if style.margin_chars != 0 {
        let local_edit_char_pixels_x = style.local_editing_font.font.character_size.width
            + style.local_editing_font.font.character_spacing;
        let remote_edit_char_pixels_x = style.remote_editing_font.font.character_size.width
            + style.remote_editing_font.font.character_spacing;
        let local_hist_char_pixels_x = style.local_history_font.font.character_size.width
            + style.local_history_font.font.character_spacing;
        let remote_hist_char_pixels_x = style.remote_history_font.font.character_size.width
            + style.remote_history_font.font.character_spacing;
        let largest_width = [
            local_edit_char_pixels_x,
            remote_edit_char_pixels_x,
            local_hist_char_pixels_x,
            remote_hist_char_pixels_x,
        ]
        .into_iter()
        .max()
        .unwrap_or(0);

        largest_width * style.margin_chars
    } else {
        0
    };

    let width_margin = if style.margin_chars != 0 {
        x_width - (2 * left_margin_px)
    } else {
        x_width
    };

    let local_edit_bkgd_style = PrimitiveStyleBuilder::new()
        .fill_color(style.local_editing_background)
        .build();
    for line in rline.iter_local_editing() {
        y_idx -= local_edit_char_pixels_y;
        let bar = Rectangle::new(
            Point {
                x: left_margin_px as i32,
                y: y_idx as i32,
            },
            Size {
                width: width_margin,
                height: local_edit_char_pixels_y,
            },
        );
        bar.draw_styled(&local_edit_bkgd_style, disp)?;

        Text::new(
            line.as_str(),
            Point {
                x: left_margin_px as i32,
                y: (y_idx + style.local_editing_font.font.baseline) as i32,
            },
            style.local_editing_font,
        )
        .draw(disp)?;
    }

    let remote_edit_bkgd_style = PrimitiveStyleBuilder::new()
        .fill_color(style.remote_editing_background)
        .build();
    for line in rline.iter_remote_editing() {
        y_idx -= remote_edit_char_pixels_y;
        let bar = Rectangle::new(
            Point {
                x: left_margin_px as i32,
                y: y_idx as i32,
            },
            Size {
                width: width_margin,
                height: remote_edit_char_pixels_y,
            },
        );
        bar.draw_styled(&remote_edit_bkgd_style, disp)?;

        Text::new(
            line.as_str(),
            Point {
                x: left_margin_px as i32,
                y: (y_idx + style.remote_editing_font.font.baseline) as i32,
            },
            style.remote_editing_font,
        )
        .draw(disp)?;
    }

    let local_hist_bkgd_style = PrimitiveStyleBuilder::new()
        .fill_color(style.local_history_background)
        .build();
    let remote_hist_bkgd_style = PrimitiveStyleBuilder::new()
        .fill_color(style.remote_history_background)
        .build();
    for line in rline.iter_history() {
        let (line_y, font, bkgd) = match line.status() {
            Source::Local => (
                local_hist_char_pixels_y,
                style.local_history_font,
                local_hist_bkgd_style,
            ),
            Source::Remote => (
                remote_hist_char_pixels_y,
                style.remote_history_font,
                remote_hist_bkgd_style,
            ),
        };

        y_idx -= line_y;
        let bar = Rectangle::new(
            Point {
                x: left_margin_px as i32,
                y: y_idx as i32,
            },
            Size {
                width: width_margin,
                height: line_y,
            },
        );
        bar.draw_styled(&bkgd, disp)?;

        Text::new(
            line.as_str(),
            Point {
                x: left_margin_px as i32,
                y: (y_idx + font.font.baseline) as i32,
            },
            font,
        )
        .draw(disp)?;
    }

    Ok(())
}

#[derive(Clone)]
pub struct BwStyle<'font, ColorKind: PixelColor> {
    pub background: ColorKind,
    pub font: MonoTextStyle<'font, ColorKind>,
}

pub fn drawer_bw<'font, ColorKind, Display, const WIDTH: usize, const HEIGHT: usize>(
    disp: &mut Display,
    rline: &RingLine<HEIGHT, WIDTH>,
    style: BwStyle<'font, ColorKind>,
) -> Result<(), <Display as DrawTarget>::Error>
where
    ColorKind: PixelColor,
    Display: DrawTarget<Color = ColorKind>,
{
    let full_display = disp.bounding_box();
    let char_pixels_y = style.font.font.character_size.height;
    let char_pixels_x = style.font.font.character_size.width + style.font.font.character_spacing;

    // Blank the background
    let mut y_idx: u32 = full_display.size.height;
    let x_width = full_display.size.width;
    let l_gutter = 2 * char_pixels_x;
    let r_gutter = x_width - (2 * char_pixels_x);
    disp.fill_solid(&full_display, style.background)?;

    for line in rline.iter_local_editing() {
        // Bail once we run out of screen
        y_idx = match y_idx.checked_sub(char_pixels_y) {
            Some(y) => y,
            None => return Ok(()),
        };

        let font_y = (y_idx + style.font.font.baseline) as i32;

        // Left gutter
        let lgpt = Point { x: 0, y: font_y };
        Text::new("> ", lgpt, style.font).draw(disp)?;

        // Text
        let ltpt = Point {
            x: l_gutter as i32,
            y: font_y,
        };
        Text::new(line.as_str(), ltpt, style.font).draw(disp)?;

        // Right gutter
        let rgpt = Point {
            x: r_gutter as i32,
            y: font_y,
        };
        Text::new(" #", rgpt, style.font).draw(disp)?;
    }

    for line in rline.iter_remote_editing() {
        // Bail once we run out of screen
        y_idx = match y_idx.checked_sub(char_pixels_y) {
            Some(y) => y,
            None => return Ok(()),
        };

        let font_y = (y_idx + style.font.font.baseline) as i32;

        // Left gutter
        let lgpt = Point { x: 0, y: font_y };
        Text::new("< ", lgpt, style.font).draw(disp)?;

        // Text
        let ltpt = Point {
            x: l_gutter as i32,
            y: font_y,
        };
        Text::new(line.as_str(), ltpt, style.font).draw(disp)?;

        // Right gutter
        let rgpt = Point {
            x: r_gutter as i32,
            y: font_y,
        };
        Text::new(" #", rgpt, style.font).draw(disp)?;
    }

    // let local_hist_bkgd_style = PrimitiveStyleBuilder::new().fill_color(style.local_history_background).build();
    // let remote_hist_bkgd_style = PrimitiveStyleBuilder::new().fill_color(style.remote_history_background).build();
    for line in rline.iter_history() {
        // Bail once we run out of screen
        y_idx = match y_idx.checked_sub(char_pixels_y) {
            Some(y) => y,
            None => return Ok(()),
        };
        let (lgutter, rgutter) = match line.status() {
            Source::Local => (">|", "|>"),
            Source::Remote => ("<|", "|<"),
        } ;

        let font_y = (y_idx + style.font.font.baseline) as i32;

        // Left gutter
        let lgpt = Point { x: 0, y: font_y };
        Text::new(lgutter, lgpt, style.font).draw(disp)?;

        // Text
        let ltpt = Point {
            x: l_gutter as i32,
            y: font_y,
        };
        Text::new(line.as_str(), ltpt, style.font).draw(disp)?;

        // Right gutter
        let rgpt = Point {
            x: r_gutter as i32,
            y: font_y,
        };
        Text::new(rgutter, rgpt, style.font).draw(disp)?;
    }

    Ok(())
}