analog-clock 0.1.6

A terminal analog clock.
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use crate::theme::THEMES;

use bresenham::Bresenham;
use chrono::{DateTime, Local, Timelike};
use colors_transform::Color;
use colors_transform::Rgb;
use crossterm::event::KeyEvent;
use crossterm::event::KeyModifiers;
use crossterm::{
    cursor,
    event::{poll, read, Event, KeyCode},
    style::{self, Stylize},
    terminal, ExecutableCommand, QueueableCommand, Result,
};
use image::{imageops::resize, ImageBuffer, Rgb as RgbPixel};
use line_drawing::BresenhamCircle;
use std::f32::consts::PI;
use std::io::{stdout, Write};
use std::process;
use std::time::Duration;

pub struct RunClockOptions {
    pub theme_index: usize,

    /// How often should the clock be redrawn.
    pub tick_interval: Duration,

    pub show_second_hand: bool,
    pub show_hour_labels: bool,
    pub show_minute_labels: bool,
}

fn new_error(message: String) -> std::io::Error {
    std::io::Error::new(std::io::ErrorKind::Other, message.as_str())
}

struct UiState {
    /// Aspect ratio  = character_height / character_width
    ///
    /// where:
    ///
    ///   character_height is the actual height of each character in the terminal,
    ///   character_width is the actual width of each character in terminal
    ///
    /// This is needed to circularize the clock, otherwise it will look like an ellipse.
    aspect_ratio: f32,
    theme_index: usize,
}

pub fn run_clock(options: RunClockOptions) -> Result<()> {
    terminal::enable_raw_mode()?;

    let mut stdout = stdout();
    stdout
        .execute(cursor::Hide)?
        .execute(terminal::Clear(terminal::ClearType::All))?;

    let mut state = UiState {
        aspect_ratio: 2.0,
        theme_index: options.theme_index,
    };

    let (width, height) = term_size::dimensions()
        .ok_or_else(|| new_error("Unable to get term size :(".to_string()))?;
    let mut current_matrix = Matrix::new(width, height);

    loop {
        // Read for user input in a non-blocking manner
        // Refer https://docs.rs/crossterm/latest/crossterm/event/index.html#examples
        if poll(options.tick_interval)? {
            match read()? {
                Event::Key(event) => {
                    // Increase width
                    if event.code == KeyCode::Char('+') || event.code == KeyCode::Char('=') {
                        state.aspect_ratio += 0.1;
                    }
                    // Decrease width
                    else if event.code == KeyCode::Char('-') {
                        if state.aspect_ratio > 1.0 {
                            state.aspect_ratio -= 0.1;
                        }
                    }
                    // Reset width
                    else if event.code == KeyCode::Char('0') {
                        state.aspect_ratio = 2.0;
                    }
                    // Quit
                    else if event.code == KeyCode::Char('q')
                        || event == KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)
                    {
                        stdout
                            .execute(terminal::Clear(terminal::ClearType::All))?
                            .execute(cursor::Show)?;
                        process::exit(0)
                    }
                    // Next theme
                    else if event.code == KeyCode::Char('j') {
                        state.theme_index = (state.theme_index + 1) % THEMES.len()
                    }
                    // Previous theme
                    else if event.code == KeyCode::Char('k') {
                        state.theme_index = if state.theme_index == 0 {
                            THEMES.len() - 1
                        } else {
                            state.theme_index - 1
                        }
                    }
                }
                Event::Mouse(event) => println!("{:?}", event),
                Event::Resize(width, height) => {
                    stdout.execute(terminal::Clear(terminal::ClearType::All))?;
                    current_matrix = Matrix::new(width as usize, height as usize)
                }
            }
        }
        let new_matrix = draw_clock(&state, &options);

        // Print based on diff, this is to improve rendering performance
        let diff = current_matrix.diff(&new_matrix);

        Matrix::print(diff)?;

        // Update current_matrix
        current_matrix = new_matrix;
    }
}

fn draw_clock(state: &UiState, options: &RunClockOptions) -> Matrix {
    let (screen_width, height) = term_size::dimensions()
        .ok_or_else(|| new_error("Unable to get term size :(".to_string()))
        .unwrap();
    let clock_width = (screen_width as f32) / state.aspect_ratio;
    let matrix = Matrix::new(clock_width as usize, height);
    let datetime: DateTime<Local> = Local::now();

    let theme = THEMES[state.theme_index];
    let matrix = matrix.draw_circle(Rgb::from_hex_str(theme.clock_face).unwrap());

    // Draw clock face: hour labels
    let matrix = if options.show_hour_labels {
        (0..12).into_iter().fold(matrix, |matrix, n| {
            matrix.draw_hand(Hand {
                degree: (n as f32) / 12.0 * 360.0,
                thickness: HandThickness::Thin,
                length: 0.15,
                line_start: HandLineStart::FromCircumference,
                color: Rgb::from_hex_str(theme.clock_face).unwrap(),
            })
        })
    } else {
        matrix
    };

    // Draw clock face: minute/seconds labels
    let matrix = if options.show_minute_labels {
        (0..60).into_iter().fold(matrix, |matrix, n| {
            matrix.draw_hand(Hand {
                degree: (n as f32) / 60.0 * 360.0,
                thickness: HandThickness::Thin,
                length: 0.05,
                line_start: HandLineStart::FromCircumference,
                color: Rgb::from_hex_str("#4C566A").unwrap(),
            })
        })
    } else {
        matrix
    };

    let millisecond = datetime.timestamp_millis() % 1000;
    let second = datetime.second() as f32;
    let minute = datetime.minute() as f32;
    let hour = (datetime.hour() % 12) as f32;

    let second = if options.tick_interval.as_millis() < 1000 {
        second + (millisecond as f32) / 1000.0
    } else {
        second
    };
    let degree_second = second / 60.0 * 360.0;
    let degree_minute = (minute + second / 60.0) / 60.0 * 360.0;
    let degree_hour = (hour + minute / 60.0) / 12.0 * 360.0;

    // Firstly, draw minute hand
    let matrix = matrix.draw_hand(Hand {
        degree: degree_minute,
        thickness: HandThickness::Bold,
        length: 0.9,
        line_start: HandLineStart::FromCenter,
        color: Rgb::from_hex_str(theme.minute).unwrap(),
    });

    // Secondly, draw hour hand, as hour hand must be on top of minute hand
    let matrix = matrix.draw_hand(Hand {
        degree: degree_hour,
        thickness: HandThickness::Bold,
        length: 0.5,
        line_start: HandLineStart::FromCenter,
        color: Rgb::from_hex_str(theme.hour).unwrap(),
    });

    // Thirdly, draw second hand, which should be on top of hour hand & minute hand
    let matrix = if options.show_second_hand {
        matrix.draw_hand(Hand {
            degree: degree_second,
            thickness: HandThickness::Thin,
            length: 0.9,
            line_start: HandLineStart::FromCenter,
            color: Rgb::from_hex_str(theme.second).unwrap(),
        })
    } else {
        matrix
    };

    // After computing the final matrix, we have to resize it
    matrix.rescale(screen_width)
}

#[derive(Clone, Debug, PartialEq)]
struct Cell {
    color: Rgb,
}

struct Matrix {
    cells: Vec<Vec<Option<Cell>>>,
    width: usize,
    height: usize,
    midpoint_x: f32,
    midpoint_y: f32,
    circle_radius: f32,
}

impl Matrix {
    fn new(width: usize, height: usize) -> Matrix {
        let midpoint_x = (width as f32) / 2.0;
        let midpoint_y = (height as f32) / 2.0;
        Matrix {
            cells: vec![vec![None; width as usize]; height],
            width: width as usize,
            height,
            midpoint_x,
            midpoint_y,
            circle_radius: midpoint_x.min(midpoint_y) / 1.1,
        }
    }

    fn draw_circle(mut self, color: Rgb) -> Matrix {
        let points = BresenhamCircle::new(
            self.midpoint_x as i32,
            self.midpoint_y as i32,
            self.circle_radius as i32,
        );
        for (x, y) in points {
            self.cells[y as usize][x as usize] = Some(Cell { color })
        }
        self
    }

    /// Draw a line originated from the center.
    /// We will be using [Bresenham Line Algorithm](https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#History).
    fn draw_hand(self, hand: Hand) -> Matrix {
        let degree = hand.degree;
        let radian = PI / 2.0 - (degree).to_radians();
        let radius = self.circle_radius;

        let origins = {
            let x = self.midpoint_x;
            let y = self.midpoint_y;

            match hand.thickness {
                HandThickness::Thin => vec![(x, y)],
                HandThickness::Bold => vec![
                    (x - 1.0, y + 1.0), // top left
                    (x, y + 1.0),       // top
                    (x + 1.0, y + 1.0), // top right
                    (x - 1.0, y),       // left
                    (x, y),             // center
                    (x + 1.0, y),       // right
                    (x - 1.0, y - 1.0), // top left
                    (x, y - 1.0),       // top
                    (x + 1.0, y - 1.0), // top right
                ],
            }
        };

        origins
            .into_iter()
            .fold(self, |matrix, (midpoint_x, midpoint_y)| {
                // We treat radius as the hypotenuse
                // Trigonometry hints:
                // Adjacent = Hypotenuse * cos theta
                // Opposite = Hypotenuse * sin theta

                let get_point = |hypotenuse: f32| -> (isize, isize) {
                    let x = midpoint_x + hypotenuse * radian.cos();
                    let y = midpoint_y + hypotenuse * radian.sin();
                    (x as isize, y as isize)
                };

                // Calculate startpoint based on line_start
                let startpoint = match hand.line_start {
                    HandLineStart::FromCenter => get_point(0.0),
                    HandLineStart::FromCircumference => get_point(radius * (1.0 - hand.length)),
                };

                // Calculate endpoint based on line_start
                let endpoint = match hand.line_start {
                    HandLineStart::FromCenter => get_point(radius * hand.length),
                    HandLineStart::FromCircumference => get_point(radius),
                };

                let points = Bresenham::new(startpoint, endpoint)
                    .map(|(x, y)| Point {
                        x,

                        // We have to invert y because the result returned by Bresenham is based on Cartesian plane
                        // where (0, 0) is at the bottom left corner.
                        // However for our matrix, (0, 0) is at the top left corner, which is like the Cartesian
                        // plane flip around the x-axis.
                        y: matrix.height as isize - y,
                        color: hand.color,
                    })
                    .collect();

                matrix.draw_using_points(points)
            })
    }

    /// Apply vertical/horizontal scaling to the given matrix,
    /// such that the clock will look like a circle instead of an ellipse.
    /// This is because each "pixel" (or character) on a terminal is not square-ish, but a
    /// vertical rectangle instead.
    fn rescale(self, screen_width: usize) -> Matrix {
        let img = matrix_to_luma_image_buffer(&self);
        let img = resize(
            &img,
            screen_width as u32,
            self.height as u32,
            image::imageops::FilterType::Nearest,
        );
        Matrix {
            cells: luma_image_buffer_to_matrix(img),
            width: screen_width,
            ..self
        }
    }

    /// Compute the diff between two matrices.
    /// This is for reducing unnecessary re-renders.
    fn diff(&self, new: &Matrix) -> Vec<DiffUpdate> {
        let old = self;
        if old.width != new.width {
            panic!(
                "Diff error: old width != new width, old.width = {}, new.width = {}",
                old.width, new.width
            )
        }
        if old.height != new.height {
            panic!(
                "Diff error: old height != new height, old.height = {}, new.height = {}",
                old.height, new.height
            )
        }
        let points = generate_points(old.width, old.height);

        points
            .into_iter()
            .filter_map(|(x, y)| {
                let old_cell = &old.cells[y][x];
                let new_cell = &new.cells[y][x];
                if old_cell == new_cell {
                    None
                } else {
                    Some(DiffUpdate {
                        x,
                        y,
                        cell: new_cell.clone(),
                    })
                }
            })
            .collect()
    }

    fn print(updates: Vec<DiffUpdate>) -> Result<()> {
        let mut stdout = stdout();

        for update in updates {
            let x = update.x as u16;
            let y = update.y as u16;
            let character = match update.cell {
                Some(cell) => "".with(style::Color::Rgb {
                    r: cell.color.get_red() as u8,
                    g: cell.color.get_green() as u8,
                    b: cell.color.get_blue() as u8,
                }),
                None => " ".stylize(),
            };

            stdout
                .queue(cursor::MoveTo(x, y))?
                .queue(style::PrintStyledContent(character))?;
        }

        stdout.flush()?;

        Ok(())
    }

    fn draw_using_points(mut self, points: Vec<Point>) -> Matrix {
        for point in points {
            self.cells[point.y as usize][point.x as usize] = Some(Cell { color: point.color })
        }
        self
    }
}

fn generate_points(width: usize, height: usize) -> Vec<(usize, usize)> {
    (0..height)
        .into_iter()
        .flat_map(|y| (0..width).into_iter().map(move |x| (x, y)))
        .collect()
}

struct DiffUpdate {
    x: usize,
    y: usize,
    cell: Option<Cell>,
}

fn matrix_to_luma_image_buffer(matrix: &Matrix) -> ImageBuffer<RgbPixel<u8>, Vec<u8>> {
    ImageBuffer::from_fn(
        matrix.width as u32,
        matrix.height as u32,
        |x, y| match &matrix.cells[y as usize][x as usize] {
            Some(cell) => RgbPixel([
                cell.color.get_red() as u8,
                cell.color.get_green() as u8,
                cell.color.get_blue() as u8,
            ]),
            None => RgbPixel([255, 255, 255]),
        },
    )
}

fn luma_image_buffer_to_matrix(img: ImageBuffer<RgbPixel<u8>, Vec<u8>>) -> Vec<Vec<Option<Cell>>> {
    let width = img.width() as usize;
    let mut cells = vec![vec![None; width]; img.height() as usize];
    img.pixels().enumerate().for_each(|(index, pixel)| {
        let y = (index as f32 / width as f32).floor() as usize;
        let x = index % width;
        cells[y][x] = if pixel != &RgbPixel([255, 255, 255]) {
            Some(Cell {
                color: Rgb::from(pixel.0[0] as f32, pixel.0[1] as f32, pixel.0[2] as f32),
            })
        } else {
            None
        }
    });
    cells
}
struct Point {
    x: isize,
    y: isize,
    color: Rgb,
}
struct Hand {
    /// 0 to 360, where:
    /// 0 = North,
    /// 90 = East,
    /// 180 = South,
    /// 270 = West.
    degree: f32,
    thickness: HandThickness,
    /// In terms of percentage. 0 is shortest, 1 is longest.
    length: f32,
    line_start: HandLineStart,
    color: Rgb,
}
enum HandThickness {
    Thin,
    Bold,
}
enum HandLineStart {
    FromCenter,
    FromCircumference,
}