relax Send + Sync constraint on Renderables; remove 3 format calls from render loop

maybe render should have mutable access after all?
This commit is contained in:
🪞👃🪞 2025-01-20 18:53:15 +01:00
parent 209f35440a
commit 680a841e3f
5 changed files with 58 additions and 55 deletions

View file

@ -20,6 +20,15 @@ impl Content<TuiOut> for String {
}
}
impl Content<TuiOut> for std::sync::RwLockReadGuard<'_, String> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
Content::<TuiOut>::layout(&**self, to)
}
fn render (&self, to: &mut TuiOut) {
Content::<TuiOut>::render(&**self, to)
}
}
impl Content<TuiOut> for Arc<str> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to.center_xy([self.chars().count() as u16, 1])
@ -29,43 +38,32 @@ impl Content<TuiOut> for Arc<str> {
}
}
pub struct Field<T, U>(pub ItemPalette, pub T, pub U)
where T: AsRef<str> + Send + Sync, U: AsRef<str> + Send + Sync;
impl<T, U> Content<TuiOut> for Field<T, U>
where T: AsRef<str> + Send + Sync, U: AsRef<str> + Send + Sync
{
pub struct FieldH<T, U>(pub ItemPalette, pub T, pub U);
impl<T: Content<TuiOut>, U: Content<TuiOut>> Content<TuiOut> for FieldH<T, U> {
fn content (&self) -> impl Render<TuiOut> {
let ItemPalette { darkest, dark, lighter, lightest, .. } = self.0;
let Self(ItemPalette { darkest, dark, lighter, lightest, .. }, title, value) = self;
row!(
Tui::fg_bg(dark.rgb, darkest.rgb, ""),
Tui::fg_bg(lighter.rgb, dark.rgb, Tui::bold(true, format!("{}", self.1.as_ref()))),
Tui::fg_bg(lighter.rgb, dark.rgb, Tui::bold(true, title)),
Tui::fg_bg(dark.rgb, darkest.rgb, ""),
Tui::fg_bg(lightest.rgb, darkest.rgb, format!("{} ", self.2.as_ref()))
Tui::fg_bg(lightest.rgb, darkest.rgb, value),
)
}
}
pub struct FieldV<T, U>(pub ItemPalette, pub T, pub U)
where T: AsRef<str> + Send + Sync, U: AsRef<str> + Send + Sync;
impl<T, U> Content<TuiOut> for FieldV<T, U>
where T: AsRef<str> + Send + Sync, U: AsRef<str> + Send + Sync
{
pub struct FieldV<T, U>(pub ItemPalette, pub T, pub U);
impl<T: Content<TuiOut>, U: Content<TuiOut>> Content<TuiOut> for FieldV<T, U> {
fn content (&self) -> impl Render<TuiOut> {
let ItemPalette { darkest, dark, lighter, lightest, .. } = self.0;
let sep1 = Tui::bg(darkest.rgb, Tui::fg(dark.rgb, ""));
let sep2 = Tui::bg(darkest.rgb, Tui::fg(dark.rgb, ""));
let name = Tui::bg(dark.rgb, Tui::fg(lighter.rgb,
Tui::bold(true, format!("{}", self.1.as_ref()))));
let value = Tui::bg(darkest.rgb, Tui::fg(lightest.rgb,
format!(" {} ", self.2.as_ref())));
Bsp::e(Bsp::s(row!(sep1, name, sep2), value), " ")
let Self(ItemPalette { darkest, dark, lighter, lightest, .. }, title, value) = self;
let sep1 = Tui::bg(darkest.rgb, Tui::fg(dark.rgb, ""));
let sep2 = Tui::bg(darkest.rgb, Tui::fg(dark.rgb, ""));
let title = Tui::bg(dark.rgb, Tui::fg(lighter.rgb, Tui::bold(true, title)));
let value = Tui::bg(darkest.rgb, Tui::fg(lightest.rgb, value));
Bsp::e(Bsp::s(row!(sep1, title, sep2), value), " ")
}
}
pub struct Repeat<'a>(pub &'a str);
impl Content<TuiOut> for Repeat<'_> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to
@ -85,7 +83,6 @@ impl Content<TuiOut> for Repeat<'_> {
}
pub struct RepeatV<'a>(pub &'a str);
impl Content<TuiOut> for RepeatV<'_> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to
@ -101,7 +98,6 @@ impl Content<TuiOut> for RepeatV<'_> {
}
pub struct RepeatH<'a>(pub &'a str);
impl Content<TuiOut> for RepeatH<'_> {
fn layout (&self, to: [u16;4]) -> [u16;4] {
to

View file

@ -21,7 +21,7 @@ impl Output for TuiOut {
}
impl TuiOut {
/// Spawn the output thread.
pub fn run_output <T: Render<TuiOut> + 'static> (
pub fn run_output <T: Render<TuiOut> + Send + Sync + 'static> (
engine: &Arc<RwLock<Tui>>,
state: &Arc<RwLock<T>>,
timer: Duration