From be4aee6cece39f72d91c1b9646611d207aa8e1ef Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Mon, 22 Apr 2024 14:27:13 +0200 Subject: [PATCH] lib: Improve span builders --- src/ui/items.rs | 84 ++++++++++++++++++++++++------------------------ src/ui/span.rs | 42 ++++++++++++------------ src/ui/widget.rs | 2 +- 3 files changed, 64 insertions(+), 64 deletions(-) diff --git a/src/ui/items.rs b/src/ui/items.rs index d5060800..7fe65648 100644 --- a/src/ui/items.rs +++ b/src/ui/items.rs @@ -249,39 +249,39 @@ impl ToRow for NotificationItem { ), }; - let id = span::notification_id(format!(" {:-03}", &self.id)); + let id = span::notification_id(&format!(" {:-03}", &self.id)); let seen = if self.seen { span::blank() } else { - span::primary(" ● ".into()) + span::primary(" ● ") }; - let kind_id = span::primary(kind_id); - let summary = span::default(summary.to_string()); - let type_name = span::notification_type(type_name); - let name = span::default(self.project.clone()).style(style::gray().dim()); + let kind_id = span::primary(&kind_id); + let summary = span::default(&summary); + let type_name = span::notification_type(&type_name); + let name = span::default(&self.project.clone()).style(style::gray().dim()); let status = match status.as_str() { - "archived" => span::default(status.to_string()).yellow(), - "draft" => span::default(status.to_string()).gray().dim(), - "updated" => span::primary(status.to_string()), - "open" | "created" => span::positive(status.to_string()), - "closed" | "merged" => span::ternary(status.to_string()), - _ => span::default(status.to_string()), + "archived" => span::default(&status).yellow(), + "draft" => span::default(&status).gray().dim(), + "updated" => span::primary(&status), + "open" | "created" => span::positive(&status), + "closed" | "merged" => span::ternary(&status), + _ => span::default(&status), }; let author = match &self.author.alias { Some(alias) => { if self.author.you { - span::alias(format!("{} (you)", alias)) + span::alias(&format!("{} (you)", alias)) } else { - span::alias(alias.to_string()) + span::alias(alias) } } None => match self.author.nid { - Some(nid) => span::alias(format::did(&Did::from(nid))).dim(), - None => span::alias("".to_string()), + Some(nid) => span::alias(&format::did(&Did::from(nid))).dim(), + None => span::blank(), }, }; - let timestamp = span::timestamp(format::timestamp(&self.timestamp)); + let timestamp = span::timestamp(&format::timestamp(&self.timestamp)); [ id.into(), @@ -492,35 +492,35 @@ impl ToRow for IssueItem { fn to_row(&self) -> Vec { let (state, state_color) = format::issue_state(&self.state); - let state = span::default(state).style(Style::default().fg(state_color)); - let id = span::primary(format::cob(&self.id)); - let title = span::default(self.title.clone()); + let state = span::default(&state).style(Style::default().fg(state_color)); + let id = span::primary(&format::cob(&self.id)); + let title = span::default(&self.title.clone()); let author = match &self.author.alias { Some(alias) => { if self.author.you { - span::alias(format!("{} (you)", alias)) + span::alias(&format!("{} (you)", alias)) } else { - span::alias(alias.to_string()) + span::alias(alias) } } None => match self.author.nid { - Some(nid) => span::alias(format::did(&Did::from(nid))).dim(), - None => span::alias("".to_string()), + Some(nid) => span::alias(&format::did(&Did::from(nid))).dim(), + None => span::alias(""), }, }; let did = match self.author.nid { - Some(nid) => span::alias(format::did(&Did::from(nid))).dim(), - None => span::alias("".to_string()), + Some(nid) => span::alias(&format::did(&Did::from(nid))).dim(), + None => span::alias(""), }; - let labels = span::labels(format::labels(&self.labels)); + let labels = span::labels(&format::labels(&self.labels)); let assignees = self .assignees .iter() .map(|author| (author.nid, author.alias.clone(), author.you)) .collect::>(); - let assignees = span::alias(format::assignees(&assignees)); - let opened = span::timestamp(format::timestamp(&self.timestamp)); + let assignees = span::alias(&format::assignees(&assignees)); + let opened = span::timestamp(&format::timestamp(&self.timestamp)); [ state.into(), @@ -744,32 +744,32 @@ impl ToRow for PatchItem { fn to_row(&self) -> Vec { let (state, color) = format::patch_state(&self.state); - let state = span::default(state).style(Style::default().fg(color)); - let id = span::primary(format::cob(&self.id)); - let title = span::default(self.title.clone()); + let state = span::default(&state).style(Style::default().fg(color)); + let id = span::primary(&format::cob(&self.id)); + let title = span::default(&self.title.clone()); let author = match &self.author.alias { Some(alias) => { if self.author.you { - span::alias(format!("{} (you)", alias)) + span::alias(&format!("{} (you)", alias)) } else { - span::alias(alias.to_string()) + span::alias(alias) } } None => match self.author.nid { - Some(nid) => span::alias(format::did(&Did::from(nid))).dim(), - None => span::alias("".to_string()), + Some(nid) => span::alias(&format::did(&Did::from(nid))).dim(), + None => span::blank(), }, }; let did = match self.author.nid { - Some(nid) => span::alias(format::did(&Did::from(nid))).dim(), - None => span::alias("".to_string()), + Some(nid) => span::alias(&format::did(&Did::from(nid))).dim(), + None => span::blank(), }; - let head = span::ternary(format::oid(self.head)); - let added = span::positive(format!("+{}", self.added)); - let removed = span::negative(format!("-{}", self.removed)); - let updated = span::timestamp(format::timestamp(&self.timestamp)); + let head = span::ternary(&format::oid(self.head)); + let added = span::positive(&format!("+{}", self.added)); + let removed = span::negative(&format!("-{}", self.removed)); + let updated = span::timestamp(&format::timestamp(&self.timestamp)); [ state.into(), diff --git a/src/ui/span.rs b/src/ui/span.rs index 48fdbf36..29a80655 100644 --- a/src/ui/span.rs +++ b/src/ui/span.rs @@ -7,75 +7,75 @@ pub fn blank() -> Span<'static> { Span::styled("", Style::default()) } -pub fn default(content: String) -> Span<'static> { - Span::styled(content, Style::default()) +pub fn default(content: &str) -> Span<'static> { + Span::styled(content.to_string(), Style::default()) } -pub fn primary(content: String) -> Span<'static> { +pub fn primary(content: &str) -> Span<'static> { default(content).style(style::cyan()) } -pub fn secondary(content: String) -> Span<'static> { +pub fn secondary(content: &str) -> Span<'static> { default(content).style(style::magenta()) } -pub fn ternary(content: String) -> Span<'static> { +pub fn ternary(content: &str) -> Span<'static> { default(content).style(style::blue()) } -pub fn positive(content: String) -> Span<'static> { +pub fn positive(content: &str) -> Span<'static> { default(content).style(style::green()) } -pub fn negative(content: String) -> Span<'static> { +pub fn negative(content: &str) -> Span<'static> { default(content).style(style::red()) } -pub fn badge(content: String) -> Span<'static> { +pub fn badge(content: &str) -> Span<'static> { let content = &format!(" {content} "); - default(content.to_string()).magenta().reversed() + default(content).magenta().reversed() } -pub fn alias(content: String) -> Span<'static> { +pub fn alias(content: &str) -> Span<'static> { secondary(content) } -pub fn labels(content: String) -> Span<'static> { +pub fn labels(content: &str) -> Span<'static> { ternary(content) } -pub fn timestamp(content: String) -> Span<'static> { +pub fn timestamp(content: &str) -> Span<'static> { default(content).style(style::gray().dim()) } -pub fn notification_id(content: String) -> Span<'static> { +pub fn notification_id(content: &str) -> Span<'static> { default(content).style(style::gray().dim()) } -pub fn notification_type(content: String) -> Span<'static> { +pub fn notification_type(content: &str) -> Span<'static> { default(content).style(style::gray().dim()) } pub fn step(step: usize, len: usize, fill_zeros: bool) -> Span<'static> { if fill_zeros { if len > 10 { - badge(format!("{:-02}/{:-02}", step, len)) + badge(&format!("{:-02}/{:-02}", step, len)) } else if len > 100 { - badge(format!("{:-03}/{:-03}", step, len)) + badge(&format!("{:-03}/{:-03}", step, len)) } else if len > 1000 { - badge(format!("{:-04}/{:-04}", step, len)) + badge(&format!("{:-04}/{:-04}", step, len)) } else if len > 10000 { - badge(format!("{:-05}/{:-05}", step, len)) + badge(&format!("{:-05}/{:-05}", step, len)) } else { - badge(format!("{}/{}", step, len)) + badge(&format!("{}/{}", step, len)) } } else { - badge(format!("{}/{}", step, len)) + badge(&format!("{}/{}", step, len)) } } pub fn progress(step: usize, len: usize) -> Span<'static> { let progress = step as f32 / len as f32 * 100_f32; let progress = progress as usize; - default(format!("{}%", progress)).dim() + default(&format!("{}%", progress)).dim() } diff --git a/src/ui/widget.rs b/src/ui/widget.rs index de66dc7a..0f331414 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -660,7 +660,7 @@ where frame.render_stateful_widget(rows, area, &mut self.state.clone()); } else { let center = layout::centered_rect(area, 50, 10); - let hint = Text::from(span::default("Nothing to show".to_string())) + let hint = Text::from(span::default("Nothing to show")) .centered() .light_magenta() .dim();