diff --git a/pulse-ui/src/widget/center.rs b/pulse-ui/src/widget/center.rs new file mode 100644 index 0000000..33ac06e --- /dev/null +++ b/pulse-ui/src/widget/center.rs @@ -0,0 +1,20 @@ +use crate::widget::Widget; + +pub struct Center(pub String); + +impl Widget for Center { + fn render(&self, scope: &mut crate::render::RenderScope) { + let mut width = 0; + let mut height = 0; + + for line in self.0.lines() { + height += 1; + width = width.max(line.len() as u16); + } + + let x = (scope.rect.width - width) / 2; + let y = (scope.rect.height - height) / 2; + + scope.draw_text((x, y), &self.0); + } +} diff --git a/pulse-ui/src/widget/mod.rs b/pulse-ui/src/widget/mod.rs index 819165e..d75c0e6 100644 --- a/pulse-ui/src/widget/mod.rs +++ b/pulse-ui/src/widget/mod.rs @@ -1,3 +1,4 @@ +pub mod center; pub mod outline; pub mod spaced; diff --git a/src/main.rs b/src/main.rs index 21980b4..cddf97d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,10 @@ impl App for PulseTradeApp { (LayoutItem::Widget(Size::Flex(1)), Box::new("one")), (LayoutItem::Widget(Size::Flex(1)), Box::new("two")), (LayoutItem::Widget(Size::Flex(1)), Box::new("three")), - (LayoutItem::Widget(Size::Flex(1)), Box::new(self.count.display().await)), + ( + LayoutItem::Widget(Size::Flex(1)), + Box::new(self.count.display().await), + ), ]), ); }