lib: Use labels instead of cells and spans

This commit is contained in:
Erik Kundt
2024-01-12 15:51:08 +01:00
parent 74b7a16b7f
commit 2526dc4788
2 changed files with 134 additions and 53 deletions
+66 -47
View File
@@ -3,7 +3,7 @@ pub mod format;
use radicle_surf;
use tuirealm::props::{Color, Style};
use tuirealm::tui::text::{Span, Spans};
use tuirealm::tui::text::Spans;
use tuirealm::tui::widgets::Cell;
use radicle::node::{Alias, AliasStore};
@@ -20,7 +20,7 @@ use radicle::cob::{Label, Timestamp};
use crate::ui::theme::Theme;
use crate::ui::widget::list::{ListItem, TableItem};
use super::theme::style;
use super::widget::label;
/// An author item that can be used in tables, list or trees.
///
@@ -147,25 +147,31 @@ impl TableItem<8> for PatchItem {
fn row(&self, _theme: &Theme) -> [Cell; 8] {
let (icon, color) = format_patch_state(&self.state);
let state = Cell::from(icon).style(Style::default().fg(color));
let id = Cell::from(format::cob(&self.id)).style(style::cyan());
let title = Cell::from(self.title.clone()).style(style::reset());
let state = label::default(&icon)
.style(Style::default().fg(color))
.into();
let id = label::id(&format::cob(&self.id)).into();
let title = label::default(&self.title.clone()).into();
let author_style = match &self.author.alias {
Some(_) => style::magenta(),
None => style::magenta_dim(),
let author = match &self.author.alias {
Some(_) => label::alias(&format_author(
&self.author.did,
&self.author.alias,
self.author.is_you,
))
.into(),
None => label::did(&format_author(
&self.author.did,
&self.author.alias,
self.author.is_you,
))
.into(),
};
let author = Cell::from(format_author(
&self.author.did,
&self.author.alias,
self.author.is_you,
))
.style(author_style);
let head = Cell::from(format::oid(self.head)).style(style::lightblue());
let added = Cell::from(format!("+{}", self.added)).style(style::green());
let removed = Cell::from(format!("-{}", self.removed)).style(style::red());
let updated = Cell::from(format::timestamp(&self.timestamp)).style(style::gray());
let head = label::oid(&format::oid(self.head)).into();
let added = label::positive(&format!("+{}", self.added)).into();
let removed = label::negative(&format!("-{}", self.removed)).into();
let updated = label::timestamp(&format::timestamp(&self.timestamp)).into();
[state, id, title, author, head, added, removed, updated]
}
@@ -255,30 +261,35 @@ impl TableItem<7> for IssueItem {
fn row(&self, _theme: &Theme) -> [Cell; 7] {
let (icon, color) = format_issue_state(&self.state);
let state = Cell::from(icon).style(Style::default().fg(color));
let id = Cell::from(format::cob(&self.id)).style(style::cyan());
let title = Cell::from(self.title.clone()).style(style::reset());
let state = label::default(&icon)
.style(Style::default().fg(color))
.into();
let id = label::id(&format::cob(&self.id)).into();
let title = label::default(&self.title.clone()).into();
let author_style = match &self.author.alias {
Some(_) => style::magenta(),
None => style::magenta_dim(),
let author = match &self.author.alias {
Some(_) => label::alias(&format_author(
&self.author.did,
&self.author.alias,
self.author.is_you,
))
.into(),
None => label::did(&format_author(
&self.author.did,
&self.author.alias,
self.author.is_you,
))
.into(),
};
let author = Cell::from(format_author(
&self.author.did,
&self.author.alias,
self.author.is_you,
))
.style(author_style);
let labels = Cell::from(format_labels(&self.labels)).style(style::lightblue());
let labels = label::labels(&format_labels(&self.labels)).into();
let assignees = self
.assignees
.iter()
.map(|author| (author.did, author.alias.clone(), author.is_you))
.collect::<Vec<_>>();
let assignees = Cell::from(format_assignees(&assignees)).style(author_style);
let opened = Cell::from(format::timestamp(&self.timestamp)).style(style::gray());
let assignees = label::did(&format_assignees(&assignees)).into();
let opened = label::timestamp(&format::timestamp(&self.timestamp)).into();
[state, id, title, author, labels, assignees, opened]
}
@@ -287,24 +298,32 @@ impl TableItem<7> for IssueItem {
impl ListItem for IssueItem {
fn row(&self, theme: &Theme) -> tuirealm::tui::widgets::ListItem {
let (state, state_color) = format_issue_state(&self.state);
let author_style = match &self.author.alias {
Some(_) => style::magenta(),
None => style::magenta_dim(),
};
let lines = vec![
Spans::from(vec![
Span::styled(state, Style::default().fg(state_color)),
Span::styled(self.title.clone(), style::reset()),
label::default(&state)
.style(Style::default().fg(state_color))
.into(),
label::title(&self.title).into(),
]),
Spans::from(vec![
Span::raw(String::from(" ")),
Span::styled(
format_author(&self.author.did, &self.author.alias, self.author.is_you),
author_style,
),
Span::styled(format!(" {} ", theme.icons.property_divider), style::gray()),
Span::styled(format::timestamp(&self.timestamp), style::gray()),
label::default(" ").into(),
match &self.author.alias {
Some(_) => label::alias(&format_author(
&self.author.did,
&self.author.alias,
self.author.is_you,
))
.into(),
None => label::did(&format_author(
&self.author.did,
&self.author.alias,
self.author.is_you,
))
.into(),
},
label::property_divider(&format!(" {} ", theme.icons.property_divider)).into(),
label::timestamp(&format::timestamp(&self.timestamp)).into(),
]),
];
tuirealm::tui::widgets::ListItem::new(lines)
+68 -6
View File
@@ -49,10 +49,18 @@ pub fn property(content: &str) -> Widget<Label> {
default(content).style(style::cyan())
}
pub fn property_divider(content: &str) -> Widget<Label> {
default(content).style(style::gray())
}
pub fn badge(content: &str) -> Widget<Label> {
default(content).style(style::magenta_reversed())
}
pub fn title(content: &str) -> Widget<Label> {
default(content)
}
pub fn labels(content: &str) -> Widget<Label> {
default(content).style(style::lightblue())
}
@@ -65,6 +73,26 @@ pub fn did(content: &str) -> Widget<Label> {
default(content).style(style::magenta_dim())
}
pub fn id(content: &str) -> Widget<Label> {
default(content).style(style::cyan())
}
pub fn oid(content: &str) -> Widget<Label> {
default(content).style(style::lightblue())
}
pub fn timestamp(content: &str) -> Widget<Label> {
default(content).style(style::gray())
}
pub fn positive(content: &str) -> Widget<Label> {
default(content).style(style::green())
}
pub fn negative(content: &str) -> Widget<Label> {
default(content).style(style::red())
}
/// A label that can be styled using a foreground color and text modifiers.
/// Its height is fixed, its width depends on the length of the text it displays.
#[derive(Clone, Default)]
@@ -110,8 +138,27 @@ impl From<&Widget<Label>> for Span<'_> {
.query(Attribute::Content)
.unwrap_or(AttrValue::String(String::default()))
.unwrap_string();
let style = label
.query(Attribute::Style)
.unwrap_or(AttrValue::Style(Style::default()))
.unwrap_style();
Span::styled(content, Style::default())
Span::styled(content, style)
}
}
impl From<Widget<Label>> for Span<'_> {
fn from(label: Widget<Label>) -> Self {
let content = label
.query(Attribute::Content)
.unwrap_or(AttrValue::String(String::default()))
.unwrap_string();
let style = label
.query(Attribute::Style)
.unwrap_or(AttrValue::Style(Style::default()))
.unwrap_style();
Span::styled(content, style)
}
}
@@ -121,12 +168,27 @@ impl From<&Widget<Label>> for Text<'_> {
.query(Attribute::Content)
.unwrap_or(AttrValue::String(String::default()))
.unwrap_string();
let foreground = label
.query(Attribute::Foreground)
.unwrap_or(AttrValue::Color(Color::Reset))
.unwrap_color();
let style = label
.query(Attribute::Style)
.unwrap_or(AttrValue::Style(Style::default()))
.unwrap_style();
Text::styled(content, Style::default().fg(foreground))
Text::styled(content, style)
}
}
impl From<Widget<Label>> for Text<'_> {
fn from(label: Widget<Label>) -> Self {
let content = label
.query(Attribute::Content)
.unwrap_or(AttrValue::String(String::default()))
.unwrap_string();
let style = label
.query(Attribute::Style)
.unwrap_or(AttrValue::Style(Style::default()))
.unwrap_style();
Text::styled(content, style)
}
}