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 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())
}
}

View file

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