From e489fb36e9ba4ee732e4237a2349280fa194b623 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 26 Aug 2024 08:03:52 +0100 Subject: [PATCH] refactor(tui): merge repo widgets into one --- .../cli/src/tui/components/repo/configured.rs | 43 ---------- .../cli/src/tui/components/repo/identified.rs | 34 -------- .../cli/src/tui/components/repo/identity.rs | 26 ++++-- crates/cli/src/tui/components/repo/mod.rs | 83 +++++++++++++++---- crates/cli/src/tui/components/repo/ready.rs | 49 ----------- 5 files changed, 85 insertions(+), 150 deletions(-) delete mode 100644 crates/cli/src/tui/components/repo/configured.rs delete mode 100644 crates/cli/src/tui/components/repo/identified.rs delete mode 100644 crates/cli/src/tui/components/repo/ready.rs diff --git a/crates/cli/src/tui/components/repo/configured.rs b/crates/cli/src/tui/components/repo/configured.rs deleted file mode 100644 index ae342ce..0000000 --- a/crates/cli/src/tui/components/repo/configured.rs +++ /dev/null @@ -1,43 +0,0 @@ -// -use git_next_core::{git, RepoAlias, RepoBranches}; - -use ratatui::{ - buffer::Buffer, - layout::{Constraint, Direction, Layout, Rect}, - widgets::{Block, Borders, Widget}, -}; - -use crate::tui::components::CommitLog; - -use super::{identity::Identity, messages::Messages}; - -pub struct ConfiguredRepoWidget<'a> { - pub repo_alias: &'a RepoAlias, - pub message: &'a str, - pub messages: &'a Vec, - pub alert: Option<&'a str>, - pub branches: &'a RepoBranches, - pub log: &'a git::graph::Log, -} -impl<'a> Widget for ConfiguredRepoWidget<'a> { - fn render(self, area: Rect, buf: &mut Buffer) - where - Self: Sized, - { - let block = Block::default() - .title(Identity::new( - self.repo_alias, - self.alert, - self.message, - Some(self.branches), - )) - .borders(Borders::ALL); - let layout = Layout::default() - .direction(Direction::Vertical) - .constraints(vec![Constraint::Fill(1), Constraint::Fill(1)]) - .split(block.inner(area)); - block.render(area, buf); - Messages::new(self.messages).render(layout[0], buf); - CommitLog { log: self.log }.render(layout[1], buf); - } -} diff --git a/crates/cli/src/tui/components/repo/identified.rs b/crates/cli/src/tui/components/repo/identified.rs deleted file mode 100644 index 53eefb8..0000000 --- a/crates/cli/src/tui/components/repo/identified.rs +++ /dev/null @@ -1,34 +0,0 @@ -// -use git_next_core::RepoAlias; - -use ratatui::{ - buffer::Buffer, - layout::Rect, - widgets::{Block, Borders, Widget}, -}; - -use super::{identity::Identity, messages::Messages}; - -pub struct IdentifiedRepoWidget<'a> { - pub repo_alias: &'a RepoAlias, - pub message: &'a str, - pub messages: &'a Vec, - pub alert: Option<&'a str>, -} -impl<'a> Widget for IdentifiedRepoWidget<'a> { - fn render(self, area: Rect, buf: &mut Buffer) - where - Self: Sized, - { - let block = Block::default() - .title(Identity::new( - self.repo_alias, - self.alert, - self.message, - None, - )) - .borders(Borders::ALL); - Messages::new(self.messages).render(block.inner(area), buf); - block.render(area, buf); - } -} diff --git a/crates/cli/src/tui/components/repo/identity.rs b/crates/cli/src/tui/components/repo/identity.rs index 28e0917..5e88dc2 100644 --- a/crates/cli/src/tui/components/repo/identity.rs +++ b/crates/cli/src/tui/components/repo/identity.rs @@ -9,6 +9,7 @@ use ratatui::{ }; pub struct Identity<'a> { + pub label: &'a str, pub repo_alias: &'a RepoAlias, pub alert: Option<&'a str>, pub message: &'a str, @@ -16,12 +17,14 @@ pub struct Identity<'a> { } impl<'a> Identity<'a> { pub const fn new( + label: &'a str, repo_alias: &'a RepoAlias, alert: Option<&'a str>, message: &'a str, repo_branches: Option<&'a RepoBranches>, ) -> Self { Self { + label, repo_alias, alert, message, @@ -39,18 +42,23 @@ impl<'a> Widget for Identity<'a> { } impl<'a> Display for Identity<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let label = self.label; let repo_alias = self.repo_alias; let alert = self.alert.unwrap_or(""); let message = self.message; - let text = self.repo_branches.map_or_else( - || format!("{repo_alias} {alert} (_/_/_) [{message}]"), - |branches| { - let main = branches.main(); - let next = branches.next(); - let dev = branches.dev(); - format!("{repo_alias} {alert} ({main}/{next}/{dev}) [{message}]") - }, - ); + let main = self + .repo_branches + .map(RepoBranches::main) + .map_or_else(|| "_".to_string(), |b| b.to_string()); + let next = self + .repo_branches + .map(RepoBranches::next) + .map_or_else(|| "_".to_string(), |b| b.to_string()); + let dev = self + .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()) } } diff --git a/crates/cli/src/tui/components/repo/mod.rs b/crates/cli/src/tui/components/repo/mod.rs index fa7a8e6..4379438 100644 --- a/crates/cli/src/tui/components/repo/mod.rs +++ b/crates/cli/src/tui/components/repo/mod.rs @@ -1,18 +1,24 @@ // -mod configured; -mod identified; mod identity; mod messages; -mod ready; use std::string::String; -use configured::ConfiguredRepoWidget; -use identified::IdentifiedRepoWidget; -use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget}; -use ready::ReadyRepoWidget; +use git_next_core::{RepoAlias, RepoBranches}; -use crate::tui::actor::RepoState; +use crate::{ + git, + tui::{actor::RepoState, components::CommitLog}, +}; + +use identity::Identity; +use messages::Messages; + +use ratatui::{ + buffer::Buffer, + layout::{Constraint, Direction, Layout, Rect}, + widgets::{Block, Borders, Widget}, +}; pub struct RepoWidget<'a> { pub repo_state: &'a RepoState, @@ -29,11 +35,14 @@ impl<'a> Widget for RepoWidget<'a> { messages, alert, } => { - IdentifiedRepoWidget { + InnerRepoWidget { + label: "identified", repo_alias, message, messages, alert: alert.as_ref().map(String::as_str), + branches: None, + log: None, } .render(area, buf); } @@ -46,13 +55,14 @@ impl<'a> Widget for RepoWidget<'a> { branches, log, .. - } => ConfiguredRepoWidget { + } => InnerRepoWidget { + label: "configured", repo_alias, message, messages, alert: alert.as_ref().map(String::as_str), - branches, - log, + branches: Some(branches), + log: Some(log), } .render(area, buf), @@ -68,13 +78,14 @@ impl<'a> Widget for RepoWidget<'a> { // next, // dev, .. - } => ReadyRepoWidget { + } => InnerRepoWidget { + label: "ready", repo_alias, message, messages, alert: alert.as_ref().map(String::as_str), - branches, - log, + branches: Some(branches), + log: Some(log), // view_state, // main, // next, @@ -84,3 +95,45 @@ impl<'a> Widget for RepoWidget<'a> { }; } } +// + +struct InnerRepoWidget<'a> { + pub label: &'a str, + pub repo_alias: &'a RepoAlias, + pub message: &'a str, + pub messages: &'a Vec, + pub alert: Option<&'a str>, + pub branches: Option<&'a RepoBranches>, + pub log: Option<&'a git::graph::Log>, + // pub view_state: &'a actor::ViewState, + // pub main: &'a Commit, + // pub next: &'a Commit, + // pub dev: &'a Commit, +} +impl<'a> Widget for InnerRepoWidget<'a> { + fn render(self, area: Rect, buf: &mut Buffer) + where + Self: Sized, + { + let block = Block::default() + .title(Identity::new( + self.label, + self.repo_alias, + self.alert, + self.message, + self.branches, + )) + .borders(Borders::ALL); + let layout_repo = Layout::default() + .direction(Direction::Vertical) + .constraints(vec![Constraint::Fill(1), Constraint::Fill(1)]) + .split(block.inner(area)); + block.render(area, buf); + + Messages::new(self.messages).render(layout_repo[0], buf); + + if let Some(log) = self.log { + CommitLog { log }.render(layout_repo[1], buf); + } + } +} diff --git a/crates/cli/src/tui/components/repo/ready.rs b/crates/cli/src/tui/components/repo/ready.rs deleted file mode 100644 index d1349c7..0000000 --- a/crates/cli/src/tui/components/repo/ready.rs +++ /dev/null @@ -1,49 +0,0 @@ -// -use git_next_core::{RepoAlias, RepoBranches}; - -use ratatui::{ - buffer::Buffer, - layout::{Constraint, Direction, Layout, Rect}, - widgets::{Block, Borders, Widget}, -}; - -use crate::{git, tui::components::CommitLog}; - -use super::{identity::Identity, messages::Messages}; - -pub struct ReadyRepoWidget<'a> { - pub repo_alias: &'a RepoAlias, - pub message: &'a str, - pub messages: &'a Vec, - pub alert: Option<&'a str>, - pub branches: &'a RepoBranches, - pub log: &'a git::graph::Log, - // pub view_state: &'a actor::ViewState, - // pub main: &'a Commit, - // pub next: &'a Commit, - // pub dev: &'a Commit, -} -impl<'a> Widget for ReadyRepoWidget<'a> { - fn render(self, area: Rect, buf: &mut Buffer) - where - Self: Sized, - { - let block = Block::default() - .title(Identity::new( - self.repo_alias, - self.alert, - self.message, - Some(self.branches), - )) - .borders(Borders::ALL); - let layout_repo = Layout::default() - .direction(Direction::Vertical) - .constraints(vec![Constraint::Fill(1), Constraint::Fill(1)]) - .split(block.inner(area)); - block.render(area, buf); - - Messages::new(self.messages).render(layout_repo[0], buf); - - CommitLog { log: self.log }.render(layout_repo[1], buf); - } -}