From d1a685ae34e6563b4011f4f39464e54541efddac Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 26 Aug 2024 08:21:18 +0100 Subject: [PATCH] feat(tui): highlight user interventions in red --- .../cli/src/tui/components/repo/identity.rs | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/crates/cli/src/tui/components/repo/identity.rs b/crates/cli/src/tui/components/repo/identity.rs index 5e88dc2..b9e6c13 100644 --- a/crates/cli/src/tui/components/repo/identity.rs +++ b/crates/cli/src/tui/components/repo/identity.rs @@ -4,7 +4,8 @@ use std::fmt::Display; use git_next_core::{RepoAlias, RepoBranches}; use ratatui::{ layout::Alignment, - text::Text, + style::{Color, Style, Styled, Stylize as _}, + text::{Line, Span, Text}, widgets::{block::Title, Widget}, }; @@ -32,19 +33,14 @@ impl<'a> Identity<'a> { } } } -impl<'a> Widget for Identity<'a> { - fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) - where - Self: Sized, - { - Text::from(self.to_string()).render(area, buf); - } -} -impl<'a> Display for Identity<'a> { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl<'a> Identity<'a> { + fn spans(self) -> Vec> { let label = self.label; let repo_alias = self.repo_alias; - let alert = self.alert.unwrap_or(""); + let alert = self + .alert + .map_or(String::new(), |a| format!("{a} ")) + .fg(Color::Red); let message = self.message; let main = self .repo_branches @@ -58,12 +54,23 @@ impl<'a> Display for Identity<'a> { .repo_branches .map(RepoBranches::dev) .map_or_else(|| "_".to_string(), |b| b.to_string()); - let text = format!("{repo_alias} ({label}) {alert} ({main}/{next}/{dev}) [{message}]"); - f.write_str(text.as_str()) + vec![ + Span::from(format!(" {repo_alias} ({label}) ")), + alert, + Span::from(format!("({main}/{next}/{dev}) [{message}] ")), + ] } } impl<'a> From> for Title<'a> { fn from(identity: Identity<'a>) -> Self { - Self::from(identity.to_string()).alignment(Alignment::Left) + Self { + content: Line { + spans: identity.spans(), + style: Style::reset(), + alignment: None, + }, + alignment: Some(Alignment::Left), + position: None, + } } }