refactor(tui): merge repo widgets into one
All checks were successful
Rust / build (push) Successful in 7m12s
ci/woodpecker/push/cron-docker-builder Pipeline was successful
ci/woodpecker/push/push-next Pipeline was successful
ci/woodpecker/push/tag-created Pipeline was successful
Release Please / Release-plz (push) Successful in 51s

This commit is contained in:
Paul Campbell 2024-08-26 08:03:52 +01:00
parent 09ff4c3a54
commit e489fb36e9
5 changed files with 85 additions and 150 deletions

View file

@ -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<String>,
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);
}
}

View file

@ -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<String>,
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);
}
}

View file

@ -9,6 +9,7 @@ use ratatui::{
}; };
pub struct Identity<'a> { pub struct Identity<'a> {
pub label: &'a str,
pub repo_alias: &'a RepoAlias, pub repo_alias: &'a RepoAlias,
pub alert: Option<&'a str>, pub alert: Option<&'a str>,
pub message: &'a str, pub message: &'a str,
@ -16,12 +17,14 @@ pub struct Identity<'a> {
} }
impl<'a> Identity<'a> { impl<'a> Identity<'a> {
pub const fn new( pub const fn new(
label: &'a str,
repo_alias: &'a RepoAlias, repo_alias: &'a RepoAlias,
alert: Option<&'a str>, alert: Option<&'a str>,
message: &'a str, message: &'a str,
repo_branches: Option<&'a RepoBranches>, repo_branches: Option<&'a RepoBranches>,
) -> Self { ) -> Self {
Self { Self {
label,
repo_alias, repo_alias,
alert, alert,
message, message,
@ -39,18 +42,23 @@ impl<'a> Widget for Identity<'a> {
} }
impl<'a> Display for Identity<'a> { impl<'a> Display for Identity<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let label = self.label;
let repo_alias = self.repo_alias; let repo_alias = self.repo_alias;
let alert = self.alert.unwrap_or(""); let alert = self.alert.unwrap_or("");
let message = self.message; let message = self.message;
let text = self.repo_branches.map_or_else( let main = self
|| format!("{repo_alias} {alert} (_/_/_) [{message}]"), .repo_branches
|branches| { .map(RepoBranches::main)
let main = branches.main(); .map_or_else(|| "_".to_string(), |b| b.to_string());
let next = branches.next(); let next = self
let dev = branches.dev(); .repo_branches
format!("{repo_alias} {alert} ({main}/{next}/{dev}) [{message}]") .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()) f.write_str(text.as_str())
} }
} }

View file

@ -1,18 +1,24 @@
// //
mod configured;
mod identified;
mod identity; mod identity;
mod messages; mod messages;
mod ready;
use std::string::String; use std::string::String;
use configured::ConfiguredRepoWidget; use git_next_core::{RepoAlias, RepoBranches};
use identified::IdentifiedRepoWidget;
use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
use ready::ReadyRepoWidget;
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 struct RepoWidget<'a> {
pub repo_state: &'a RepoState, pub repo_state: &'a RepoState,
@ -29,11 +35,14 @@ impl<'a> Widget for RepoWidget<'a> {
messages, messages,
alert, alert,
} => { } => {
IdentifiedRepoWidget { InnerRepoWidget {
label: "identified",
repo_alias, repo_alias,
message, message,
messages, messages,
alert: alert.as_ref().map(String::as_str), alert: alert.as_ref().map(String::as_str),
branches: None,
log: None,
} }
.render(area, buf); .render(area, buf);
} }
@ -46,13 +55,14 @@ impl<'a> Widget for RepoWidget<'a> {
branches, branches,
log, log,
.. ..
} => ConfiguredRepoWidget { } => InnerRepoWidget {
label: "configured",
repo_alias, repo_alias,
message, message,
messages, messages,
alert: alert.as_ref().map(String::as_str), alert: alert.as_ref().map(String::as_str),
branches, branches: Some(branches),
log, log: Some(log),
} }
.render(area, buf), .render(area, buf),
@ -68,13 +78,14 @@ impl<'a> Widget for RepoWidget<'a> {
// next, // next,
// dev, // dev,
.. ..
} => ReadyRepoWidget { } => InnerRepoWidget {
label: "ready",
repo_alias, repo_alias,
message, message,
messages, messages,
alert: alert.as_ref().map(String::as_str), alert: alert.as_ref().map(String::as_str),
branches, branches: Some(branches),
log, log: Some(log),
// view_state, // view_state,
// main, // main,
// next, // 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<String>,
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);
}
}
}

View file

@ -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<String>,
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);
}
}