1use core::fmt::Debug;
2
3use embedded_graphics::{
4 draw_target::DrawTarget,
5 mono_font::{MonoTextStyle, ascii::FONT_6X10},
6 pixelcolor::BinaryColor,
7 prelude::*,
8};
9use embedded_text::{
10 TextBox,
11 alignment::HorizontalAlignment,
12 style::{HeightMode, TextBoxStyle, TextBoxStyleBuilder},
13};
14
15#[derive(Clone, PartialEq)]
16pub struct Console<'a>(MonoTextStyle<'a, BinaryColor>, TextBoxStyle);
17
18impl Default for Console<'_> {
19 fn default() -> Self {
20 Self::new()
21 }
22}
23
24impl<'a> Console<'a> {
25 pub fn new() -> Self {
26 Self(
27 MonoTextStyle::new(&FONT_6X10, BinaryColor::Off),
28 TextBoxStyleBuilder::new()
29 .height_mode(HeightMode::FitToText)
30 .alignment(HorizontalAlignment::Left)
31 .paragraph_spacing(6)
32 .build(),
33 )
34 }
35
36 pub fn draw<D: DrawTarget<Color = BinaryColor>>(&self, draw_target: &mut D, text: &'a str)
37 where
38 <D as DrawTarget>::Error: Debug,
39 {
40 draw_target.clear(BinaryColor::On).unwrap();
41 TextBox::with_textbox_style(text, draw_target.bounding_box(), self.0, self.1)
42 .draw(draw_target)
43 .unwrap();
44 }
45}