From 43d1d927eabcb00220b2658496e48cfb8e4ef690 Mon Sep 17 00:00:00 2001 From: Robert Soane <64409800+robertpsoane@users.noreply.github.com> Date: Wed, 17 Jul 2024 18:45:24 +0100 Subject: [PATCH] refactor(config): use arc instead of box for config (#52) --- src/components/footer.rs | 6 ++++-- src/components/header.rs | 6 ++++-- src/components/help.rs | 8 +++++--- src/components/resize_notice.rs | 6 ++++-- src/components/version.rs | 6 ++++-- src/pages/attach.rs | 6 +++--- src/pages/containers.rs | 4 ++-- src/pages/describe.rs | 6 +++--- src/pages/images.rs | 2 +- src/pages/logs.rs | 6 +++--- src/pages/networks.rs | 2 +- src/pages/volumes.rs | 2 +- src/ui/app.rs | 8 +++++--- src/ui/page_manager.rs | 6 ++++-- 14 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/components/footer.rs b/src/components/footer.rs index ddd15e6..a00e264 100644 --- a/src/components/footer.rs +++ b/src/components/footer.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use itertools::Itertools; use ratatui::{ layout::Rect, @@ -13,12 +15,12 @@ use super::version::VersionComponent; #[derive(Debug)] pub struct Footer { - config: Box, + config: Arc, version: VersionComponent, } impl Footer { - pub async fn new(config: Box) -> Self { + pub async fn new(config: Arc) -> Self { Self { config: config.clone(), version: VersionComponent::new(config).await, diff --git a/src/components/header.rs b/src/components/header.rs index ad2e61b..00a6419 100644 --- a/src/components/header.rs +++ b/src/components/header.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use ratatui::{ layout::{self, Margin, Rect}, style::Style, @@ -9,11 +11,11 @@ use crate::{config::Config, traits::Component}; #[derive(Debug)] pub struct Header { - config: Box, + config: Arc, } impl Header { - pub fn new(config: Box) -> Self { + pub fn new(config: Arc) -> Self { Self { config } } } diff --git a/src/components/help.rs b/src/components/help.rs index 8627bae..753be3b 100644 --- a/src/components/help.rs +++ b/src/components/help.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use itertools::Itertools; use ratatui::{ layout::{Constraint, Layout}, @@ -12,7 +14,7 @@ use crate::{config::Config, traits::Component}; #[derive(Debug, Clone)] pub struct PageHelp { name: String, - config: Box, + config: Arc, displays: Vec, width: usize, } @@ -20,12 +22,12 @@ pub struct PageHelp { #[derive(Debug, Clone)] pub struct PageHelpBuilder { name: String, - config: Box, + config: Arc, inputs: Vec<(String, String)>, } impl PageHelpBuilder { - pub fn new(name: String, config: Box) -> Self { + pub fn new(name: String, config: Arc) -> Self { Self { name, config, diff --git a/src/components/resize_notice.rs b/src/components/resize_notice.rs index 2074f24..8b4ac4b 100644 --- a/src/components/resize_notice.rs +++ b/src/components/resize_notice.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use ratatui::{ layout::{self}, style::Style, @@ -15,11 +17,11 @@ const MIN_COLS: u16 = 100; pub struct ResizeScreen { pub min_height: u16, pub min_width: u16, - config: Box, + config: Arc, } impl ResizeScreen { - pub fn new(config: Box) -> Self { + pub fn new(config: Arc) -> Self { Self { min_width: MIN_COLS, min_height: MIN_ROWS, diff --git a/src/components/version.rs b/src/components/version.rs index f821867..da1bd88 100644 --- a/src/components/version.rs +++ b/src/components/version.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use crate::{config::Config, traits::Component}; use color_eyre::eyre::Result; use ratatui::{ @@ -12,13 +14,13 @@ const TAGS_URL: &str = "https://api.github.com/repos/robertpsoane/ducker/tags"; #[derive(Debug)] pub struct VersionComponent { - config: Box, + config: Arc, version: String, update_to: Option, } impl VersionComponent { - pub async fn new(config: Box) -> Self { + pub async fn new(config: Arc) -> Self { let version = format!("v{VERSION}"); let update_to = if config.check_for_update { diff --git a/src/pages/attach.rs b/src/pages/attach.rs index 7c0263a..4f6e616 100644 --- a/src/pages/attach.rs +++ b/src/pages/attach.rs @@ -33,7 +33,7 @@ enum ModalType { #[derive(Debug)] pub struct Attach { - config: Box, + config: Arc, container: Option, next: Option, tx: Sender>, @@ -43,7 +43,7 @@ pub struct Attach { } impl Attach { - pub fn new(tx: Sender>, config: Box) -> Self { + pub fn new(tx: Sender>, config: Arc) -> Self { let page_help = Self::build_page_help(config.clone(), None); Self { config, @@ -56,7 +56,7 @@ impl Attach { } } - pub fn build_page_help(config: Box, name: Option) -> PageHelp { + pub fn build_page_help(config: Arc, name: Option) -> PageHelp { PageHelpBuilder::new( match name { Some(n) => n, diff --git a/src/pages/containers.rs b/src/pages/containers.rs index 312fe3c..55e7275 100644 --- a/src/pages/containers.rs +++ b/src/pages/containers.rs @@ -51,7 +51,7 @@ enum ModalTypes { #[derive(Debug)] pub struct Containers { - config: Box, + config: Arc, pub name: String, tx: Sender>, page_help: Arc>, @@ -180,7 +180,7 @@ impl Page for Containers { impl Close for Containers {} impl Containers { - pub fn new(docker: Docker, tx: Sender>, config: Box) -> Self { + pub fn new(docker: Docker, tx: Sender>, config: Arc) -> Self { let page_help = PageHelpBuilder::new(NAME.into(), config.clone()) .add_input(format!("{}", A_KEY), "exec".into()) .add_input(format!("{CTRL_D_KEY}"), "delete".into()) diff --git a/src/pages/describe.rs b/src/pages/describe.rs index 057bbb4..7045f20 100644 --- a/src/pages/describe.rs +++ b/src/pages/describe.rs @@ -31,7 +31,7 @@ const K_KEY: Key = Key::Char('k'); #[derive(Debug)] pub struct DescribeContainer { _docker: Docker, - config: Box, + config: Arc, thing: Option>, thing_summary: Option>, tx: Sender>, @@ -41,7 +41,7 @@ pub struct DescribeContainer { } impl DescribeContainer { - pub fn new(docker: Docker, tx: Sender>, config: Box) -> Self { + pub fn new(docker: Docker, tx: Sender>, config: Arc) -> Self { let page_help = Self::build_page_help(config.clone(), None); Self { @@ -56,7 +56,7 @@ impl DescribeContainer { } } - fn build_page_help(config: Box, name: Option) -> PageHelp { + fn build_page_help(config: Arc, name: Option) -> PageHelp { let page_name = if let Some(name) = name { name } else { diff --git a/src/pages/images.rs b/src/pages/images.rs index bb7dbd9..38e984b 100644 --- a/src/pages/images.rs +++ b/src/pages/images.rs @@ -145,7 +145,7 @@ impl Page for Images { impl Close for Images {} impl Images { - pub fn new(docker: Docker, tx: Sender>, config: Box) -> Self { + pub fn new(docker: Docker, tx: Sender>, config: Arc) -> Self { let page_help = PageHelpBuilder::new(NAME.into(), config.clone()) .add_input(format!("{CTRL_D_KEY}"), "delete".into()) .add_input(format!("{G_KEY}"), "top".into()) diff --git a/src/pages/logs.rs b/src/pages/logs.rs index 2a28cd3..870c7be 100644 --- a/src/pages/logs.rs +++ b/src/pages/logs.rs @@ -33,7 +33,7 @@ const SHIFT_G_KEY: Key = Key::Char('G'); #[derive(Debug)] pub struct Logs { - config: Box, + config: Arc, docker: bollard::Docker, tx: Sender>, logs: Option, @@ -50,7 +50,7 @@ impl Logs { pub fn new( docker: bollard::Docker, tx: Sender>, - config: Box, + config: Arc, ) -> Self { let page_help = Self::build_page_help(NAME, config.clone()).build(); @@ -69,7 +69,7 @@ impl Logs { } } - fn build_page_help(name: &str, config: Box) -> PageHelpBuilder { + fn build_page_help(name: &str, config: Arc) -> PageHelpBuilder { PageHelpBuilder::new(format!("{} ({})", NAME, name), config) .add_input(format!("{ESC_KEY}"), "back".into()) .add_input(format!("{G_KEY}"), "top".into()) diff --git a/src/pages/networks.rs b/src/pages/networks.rs index ef3229e..bb0640f 100644 --- a/src/pages/networks.rs +++ b/src/pages/networks.rs @@ -143,7 +143,7 @@ impl Close for Network {} impl Network { #[must_use] - pub fn new(docker: Docker, tx: Sender>, config: Box) -> Self { + pub fn new(docker: Docker, tx: Sender>, config: Arc) -> Self { let page_help = PageHelpBuilder::new(NAME.into(), config.clone()) .add_input(format!("{CTRL_D_KEY}"), "delete".into()) .add_input(format!("{G_KEY}"), "top".into()) diff --git a/src/pages/volumes.rs b/src/pages/volumes.rs index 7bfaf50..bd00f35 100644 --- a/src/pages/volumes.rs +++ b/src/pages/volumes.rs @@ -142,7 +142,7 @@ impl Close for Volume {} impl Volume { #[must_use] - pub fn new(docker: Docker, tx: Sender>, config: Box) -> Self { + pub fn new(docker: Docker, tx: Sender>, config: Arc) -> Self { let page_help = PageHelpBuilder::new(NAME.into(), config.clone()) .add_input(format!("{CTRL_D_KEY}"), "delete".into()) .add_input(format!("{G_KEY}"), "top".into()) diff --git a/src/ui/app.rs b/src/ui/app.rs index 013e81d..9cc1b06 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use bollard::Docker; use color_eyre::eyre::{Context, Result}; use ratatui::{ @@ -32,7 +34,7 @@ enum ModalType { #[derive(Debug)] pub struct App { pub running: state::Running, - config: Box, + config: Arc, mode: state::Mode, blocked: bool, resize_screen: ResizeScreen, @@ -49,7 +51,7 @@ impl App { docker: Docker, config: Config, ) -> Result { - let config = Box::new(config); + let config = Arc::new(config); let page = state::CurrentPage::default(); @@ -66,7 +68,7 @@ impl App { title: Header::new(config.clone()), page_manager: body, footer: Footer::new(config.clone()).await, - input_field: CommandInput::new(tx, config.prompt), + input_field: CommandInput::new(tx, config.prompt.clone()), modal: None, }; Ok(app) diff --git a/src/ui/page_manager.rs b/src/ui/page_manager.rs index b380d00..92dccb1 100644 --- a/src/ui/page_manager.rs +++ b/src/ui/page_manager.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use bollard::Docker; use color_eyre::eyre::{Context, Result}; use ratatui::{ @@ -21,7 +23,7 @@ use crate::{ #[derive(Debug)] pub struct PageManager { - config: Box, + config: Arc, current_page: state::CurrentPage, page: Box, tx: Sender>, @@ -33,7 +35,7 @@ impl PageManager { page: state::CurrentPage, tx: Sender>, docker: Docker, - config: Box, + config: Arc, ) -> Result { let containers = Box::new(Containers::new(docker.clone(), tx.clone(), config.clone()));