Add WidgetExt and AsyncWidgetExt traits

This commit is contained in:
Joscha 2023-02-16 14:43:48 +01:00
parent dbafc40700
commit bcc07dc9ba
2 changed files with 30 additions and 4 deletions

View file

@ -2,18 +2,19 @@ use std::convert::Infallible;
use crossterm::event::Event;
use crossterm::style::{ContentStyle, Stylize};
use toss::widgets::{Border, BorderLook, Text};
use toss::{Styled, Terminal, Widget};
use toss::widgets::{BorderLook, Text};
use toss::{Styled, Terminal, Widget, WidgetExt};
fn widget() -> impl Widget<Infallible> {
Border::new(Text::new(
Text::new(
Styled::new("Hello world!", ContentStyle::default().green())
.then_plain("\n")
.then(
"Press any key to exit",
ContentStyle::default().on_dark_blue(),
),
))
)
.border()
.look(BorderLook::LINE_DOUBLE)
.style(ContentStyle::default().dark_red())
}

View file

@ -1,5 +1,6 @@
use async_trait::async_trait;
use crate::widgets::{Border, Padding};
use crate::{Frame, Size};
// TODO Feature-gate these traits
@ -26,3 +27,27 @@ pub trait AsyncWidget<E> {
async fn draw(self, frame: &mut Frame) -> Result<(), E>;
}
pub trait WidgetExt: Sized {
fn border(self) -> Border<Self> {
Border::new(self)
}
fn padding(self) -> Padding<Self> {
Padding::new(self)
}
}
impl<W> WidgetExt for W {}
pub trait AsyncWidgetExt: Sized {
fn border(self) -> Border<Self> {
Border::new(self)
}
fn padding(self) -> Padding<Self> {
Padding::new(self)
}
}
impl<W> AsyncWidgetExt for W {}