Skip to content

Commit

Permalink
refactor(config): use arc instead of box for config (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpsoane authored Jul 17, 2024
1 parent 73c109e commit 43d1d92
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 30 deletions.
6 changes: 4 additions & 2 deletions src/components/footer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use itertools::Itertools;
use ratatui::{
layout::Rect,
Expand All @@ -13,12 +15,12 @@ use super::version::VersionComponent;

#[derive(Debug)]
pub struct Footer {
config: Box<Config>,
config: Arc<Config>,
version: VersionComponent,
}

impl Footer {
pub async fn new(config: Box<Config>) -> Self {
pub async fn new(config: Arc<Config>) -> Self {
Self {
config: config.clone(),
version: VersionComponent::new(config).await,
Expand Down
6 changes: 4 additions & 2 deletions src/components/header.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use ratatui::{
layout::{self, Margin, Rect},
style::Style,
Expand All @@ -9,11 +11,11 @@ use crate::{config::Config, traits::Component};

#[derive(Debug)]
pub struct Header {
config: Box<Config>,
config: Arc<Config>,
}

impl Header {
pub fn new(config: Box<Config>) -> Self {
pub fn new(config: Arc<Config>) -> Self {
Self { config }
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/components/help.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use itertools::Itertools;
use ratatui::{
layout::{Constraint, Layout},
Expand All @@ -12,20 +14,20 @@ use crate::{config::Config, traits::Component};
#[derive(Debug, Clone)]
pub struct PageHelp {
name: String,
config: Box<Config>,
config: Arc<Config>,
displays: Vec<String>,
width: usize,
}

#[derive(Debug, Clone)]
pub struct PageHelpBuilder {
name: String,
config: Box<Config>,
config: Arc<Config>,
inputs: Vec<(String, String)>,
}

impl PageHelpBuilder {
pub fn new(name: String, config: Box<Config>) -> Self {
pub fn new(name: String, config: Arc<Config>) -> Self {
Self {
name,
config,
Expand Down
6 changes: 4 additions & 2 deletions src/components/resize_notice.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use ratatui::{
layout::{self},
style::Style,
Expand All @@ -15,11 +17,11 @@ const MIN_COLS: u16 = 100;
pub struct ResizeScreen {
pub min_height: u16,
pub min_width: u16,
config: Box<Config>,
config: Arc<Config>,
}

impl ResizeScreen {
pub fn new(config: Box<Config>) -> Self {
pub fn new(config: Arc<Config>) -> Self {
Self {
min_width: MIN_COLS,
min_height: MIN_ROWS,
Expand Down
6 changes: 4 additions & 2 deletions src/components/version.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use crate::{config::Config, traits::Component};
use color_eyre::eyre::Result;
use ratatui::{
Expand All @@ -12,13 +14,13 @@ const TAGS_URL: &str = "https://api.github.com/repos/robertpsoane/ducker/tags";

#[derive(Debug)]
pub struct VersionComponent {
config: Box<Config>,
config: Arc<Config>,
version: String,
update_to: Option<String>,
}

impl VersionComponent {
pub async fn new(config: Box<Config>) -> Self {
pub async fn new(config: Arc<Config>) -> Self {
let version = format!("v{VERSION}");

let update_to = if config.check_for_update {
Expand Down
6 changes: 3 additions & 3 deletions src/pages/attach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ enum ModalType {

#[derive(Debug)]
pub struct Attach {
config: Box<Config>,
config: Arc<Config>,
container: Option<DockerContainer>,
next: Option<Transition>,
tx: Sender<Message<Key, Transition>>,
Expand All @@ -43,7 +43,7 @@ pub struct Attach {
}

impl Attach {
pub fn new(tx: Sender<Message<Key, Transition>>, config: Box<Config>) -> Self {
pub fn new(tx: Sender<Message<Key, Transition>>, config: Arc<Config>) -> Self {
let page_help = Self::build_page_help(config.clone(), None);
Self {
config,
Expand All @@ -56,7 +56,7 @@ impl Attach {
}
}

pub fn build_page_help(config: Box<Config>, name: Option<String>) -> PageHelp {
pub fn build_page_help(config: Arc<Config>, name: Option<String>) -> PageHelp {
PageHelpBuilder::new(
match name {
Some(n) => n,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ enum ModalTypes {

#[derive(Debug)]
pub struct Containers {
config: Box<Config>,
config: Arc<Config>,
pub name: String,
tx: Sender<Message<Key, Transition>>,
page_help: Arc<Mutex<PageHelp>>,
Expand Down Expand Up @@ -180,7 +180,7 @@ impl Page for Containers {
impl Close for Containers {}

impl Containers {
pub fn new(docker: Docker, tx: Sender<Message<Key, Transition>>, config: Box<Config>) -> Self {
pub fn new(docker: Docker, tx: Sender<Message<Key, Transition>>, config: Arc<Config>) -> 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())
Expand Down
6 changes: 3 additions & 3 deletions src/pages/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const K_KEY: Key = Key::Char('k');
#[derive(Debug)]
pub struct DescribeContainer {
_docker: Docker,
config: Box<Config>,
config: Arc<Config>,
thing: Option<Box<dyn Describe>>,
thing_summary: Option<Vec<String>>,
tx: Sender<Message<Key, Transition>>,
Expand All @@ -41,7 +41,7 @@ pub struct DescribeContainer {
}

impl DescribeContainer {
pub fn new(docker: Docker, tx: Sender<Message<Key, Transition>>, config: Box<Config>) -> Self {
pub fn new(docker: Docker, tx: Sender<Message<Key, Transition>>, config: Arc<Config>) -> Self {
let page_help = Self::build_page_help(config.clone(), None);

Self {
Expand All @@ -56,7 +56,7 @@ impl DescribeContainer {
}
}

fn build_page_help(config: Box<Config>, name: Option<String>) -> PageHelp {
fn build_page_help(config: Arc<Config>, name: Option<String>) -> PageHelp {
let page_name = if let Some(name) = name {
name
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Page for Images {
impl Close for Images {}

impl Images {
pub fn new(docker: Docker, tx: Sender<Message<Key, Transition>>, config: Box<Config>) -> Self {
pub fn new(docker: Docker, tx: Sender<Message<Key, Transition>>, config: Arc<Config>) -> 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())
Expand Down
6 changes: 3 additions & 3 deletions src/pages/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const SHIFT_G_KEY: Key = Key::Char('G');

#[derive(Debug)]
pub struct Logs {
config: Box<Config>,
config: Arc<Config>,
docker: bollard::Docker,
tx: Sender<Message<Key, Transition>>,
logs: Option<DockerLogs>,
Expand All @@ -50,7 +50,7 @@ impl Logs {
pub fn new(
docker: bollard::Docker,
tx: Sender<Message<Key, Transition>>,
config: Box<Config>,
config: Arc<Config>,
) -> Self {
let page_help = Self::build_page_help(NAME, config.clone()).build();

Expand All @@ -69,7 +69,7 @@ impl Logs {
}
}

fn build_page_help(name: &str, config: Box<Config>) -> PageHelpBuilder {
fn build_page_help(name: &str, config: Arc<Config>) -> PageHelpBuilder {
PageHelpBuilder::new(format!("{} ({})", NAME, name), config)
.add_input(format!("{ESC_KEY}"), "back".into())
.add_input(format!("{G_KEY}"), "top".into())
Expand Down
2 changes: 1 addition & 1 deletion src/pages/networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Close for Network {}

impl Network {
#[must_use]
pub fn new(docker: Docker, tx: Sender<Message<Key, Transition>>, config: Box<Config>) -> Self {
pub fn new(docker: Docker, tx: Sender<Message<Key, Transition>>, config: Arc<Config>) -> 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())
Expand Down
2 changes: 1 addition & 1 deletion src/pages/volumes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl Close for Volume {}

impl Volume {
#[must_use]
pub fn new(docker: Docker, tx: Sender<Message<Key, Transition>>, config: Box<Config>) -> Self {
pub fn new(docker: Docker, tx: Sender<Message<Key, Transition>>, config: Arc<Config>) -> 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())
Expand Down
8 changes: 5 additions & 3 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use bollard::Docker;
use color_eyre::eyre::{Context, Result};
use ratatui::{
Expand Down Expand Up @@ -32,7 +34,7 @@ enum ModalType {
#[derive(Debug)]
pub struct App {
pub running: state::Running,
config: Box<Config>,
config: Arc<Config>,
mode: state::Mode,
blocked: bool,
resize_screen: ResizeScreen,
Expand All @@ -49,7 +51,7 @@ impl App {
docker: Docker,
config: Config,
) -> Result<Self> {
let config = Box::new(config);
let config = Arc::new(config);

let page = state::CurrentPage::default();

Expand All @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/ui/page_manager.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use bollard::Docker;
use color_eyre::eyre::{Context, Result};
use ratatui::{
Expand All @@ -21,7 +23,7 @@ use crate::{

#[derive(Debug)]
pub struct PageManager {
config: Box<Config>,
config: Arc<Config>,
current_page: state::CurrentPage,
page: Box<dyn Page>,
tx: Sender<Message<Key, Transition>>,
Expand All @@ -33,7 +35,7 @@ impl PageManager {
page: state::CurrentPage,
tx: Sender<Message<Key, Transition>>,
docker: Docker,
config: Box<Config>,
config: Arc<Config>,
) -> Result<Self> {
let containers = Box::new(Containers::new(docker.clone(), tx.clone(), config.clone()));

Expand Down

0 comments on commit 43d1d92

Please sign in to comment.