Skip to content

Commit 0948bd0

Browse files
committed
Define Display as Shape
This lets us draw it deterministically on the cell grid
1 parent 7fc0a09 commit 0948bd0

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/cmd/display.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use ratatui::{
1313
style::Color,
1414
symbols::Marker,
1515
widgets::{
16-
canvas::{Canvas, Rectangle},
16+
canvas::{Canvas, Painter, Rectangle, Shape},
1717
Block, BorderType, Padding, Widget,
1818
},
1919
Terminal,
@@ -64,7 +64,7 @@ pub async fn invoke(args: &[String], ctx: &mut Context) -> Result<String> {
6464
None | Some(Err(_)) => break,
6565
Some(Ok(msg)) => if let Model::Frame(lh_frame) = msg.payload {
6666
terminal.draw(|frame| {
67-
let canvas = lh_frame_canvas(
67+
let canvas = display_canvas(
6868
lh_frame,
6969
format!("{} ({}: quit)", path, QUIT_KEY),
7070
);
@@ -83,7 +83,7 @@ pub async fn invoke(args: &[String], ctx: &mut Context) -> Result<String> {
8383
Ok(String::new())
8484
}
8585

86-
fn lh_frame_canvas(lh_frame: Frame, title: String) -> impl Widget {
86+
fn display_canvas(lh_frame: Frame, title: String) -> impl Widget {
8787
Canvas::default()
8888
.block(
8989
Block::bordered()
@@ -92,24 +92,24 @@ fn lh_frame_canvas(lh_frame: Frame, title: String) -> impl Widget {
9292
.padding(Padding::new(1, 1, 0, 0)),
9393
)
9494
.marker(Marker::Block)
95-
.paint(move |ctx| {
96-
for y in 0..LIGHTHOUSE_ROWS {
97-
for x in 0..LIGHTHOUSE_COLS {
98-
let c = lh_frame.get(x, y);
99-
ctx.draw(&Rectangle {
100-
x: x as f64,
101-
y: (LIGHTHOUSE_ROWS - 1 - y) as f64,
102-
width: 1.0,
103-
height: 1.0,
104-
color: Color::from_u32(
105-
((c.red as u32) << 16) | (c.green as u32) << 8 | c.blue as u32,
106-
),
107-
})
108-
}
95+
.paint(move |ctx| ctx.draw(&Display { lh_frame }))
96+
}
97+
98+
struct Display {
99+
lh_frame: Frame
100+
}
101+
102+
impl Shape for Display {
103+
fn draw(&self, painter: &mut Painter) {
104+
for y in 0..LIGHTHOUSE_ROWS {
105+
for x in 0..LIGHTHOUSE_COLS {
106+
let c = self.lh_frame.get(x, y);
107+
painter.paint(x, y, Color::from_u32(
108+
((c.red as u32) << 16) | (c.green as u32) << 8 | c.blue as u32,
109+
));
109110
}
110-
})
111-
.x_bounds([0.0, LIGHTHOUSE_COLS as f64])
112-
.y_bounds([0.0, LIGHTHOUSE_ROWS as f64])
111+
}
112+
}
113113
}
114114

115115
fn key_code_to_js(key_code: KeyCode) -> Option<i32> {

0 commit comments

Comments
 (0)