Skip to content

Commit

Permalink
(refactor) shorten component creation by grouping common items in an …
Browse files Browse the repository at this point in the history
…Environment
  • Loading branch information
alexmaco committed Oct 26, 2022
1 parent 282e578 commit c6e4713
Show file tree
Hide file tree
Showing 41 changed files with 382 additions and 728 deletions.
244 changes: 66 additions & 178 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ pub struct App {
file_to_open: Option<String>,
}

pub struct Environment {
pub queue: Queue,
pub theme: SharedTheme,
pub key_config: SharedKeyConfig,
pub repo: RepoPathRef,
pub options: SharedOptions,
}

/// The need to construct a "whatever" environment only arises in testing right now
#[cfg(test)]
impl Default for Environment {
fn default() -> Self {
Self {
queue: Queue::new(),
theme: Default::default(),
key_config: Default::default(),
repo: RefCell::new(RepoPath::Path(Default::default())),
options: Default::default(),
}
}
}

// public interface
impl App {
///
Expand All @@ -121,203 +143,69 @@ impl App {
let repo_path_text =
repo_work_dir(&repo.borrow()).unwrap_or_default();

let queue = Queue::new();
let theme = Rc::new(theme);
let key_config = Rc::new(key_config);
let options = Options::new(repo.clone());
let env = Environment {
queue: Queue::new(),
theme: Rc::new(theme),
key_config: Rc::new(key_config),
options: Options::new(repo.clone()),
repo,
};

let tab = options.borrow().current_tab();
let tab = env.options.borrow().current_tab();

let mut app = Self {
input,
reset: ConfirmComponent::new(
queue.clone(),
theme.clone(),
key_config.clone(),
),
commit: CommitComponent::new(
repo.clone(),
queue.clone(),
theme.clone(),
key_config.clone(),
options.clone(),
),
reset: ConfirmComponent::new(&env),
commit: CommitComponent::new(&env),
blame_file_popup: BlameFileComponent::new(
&repo,
&queue,
sender,
&strings::blame_title(&key_config),
theme.clone(),
key_config.clone(),
),
file_revlog_popup: FileRevlogComponent::new(
&repo,
&queue,
&env,
sender,
theme.clone(),
key_config.clone(),
options.clone(),
&strings::blame_title(&env.key_config),
),
file_revlog_popup: FileRevlogComponent::new(&env, sender),
revision_files_popup: RevisionFilesPopup::new(
repo.clone(),
&queue,
sender_app,
theme.clone(),
key_config.clone(),
),
stashmsg_popup: StashMsgComponent::new(
repo.clone(),
queue.clone(),
theme.clone(),
key_config.clone(),
&env, sender_app,
),
stashmsg_popup: StashMsgComponent::new(&env),
inspect_commit_popup: InspectCommitComponent::new(
&repo,
&queue,
sender,
theme.clone(),
key_config.clone(),
&env, sender,
),
compare_commits_popup: CompareCommitsComponent::new(
&repo,
&queue,
sender,
theme.clone(),
key_config.clone(),
),
external_editor_popup: ExternalEditorComponent::new(
theme.clone(),
key_config.clone(),
),
push_popup: PushComponent::new(
&repo,
&queue,
sender,
theme.clone(),
key_config.clone(),
),
push_tags_popup: PushTagsComponent::new(
&repo,
&queue,
sender,
theme.clone(),
key_config.clone(),
),
pull_popup: PullComponent::new(
&repo,
&queue,
sender,
theme.clone(),
key_config.clone(),
),
fetch_popup: FetchComponent::new(
repo.clone(),
&queue,
sender,
theme.clone(),
key_config.clone(),
),
tag_commit_popup: TagCommitComponent::new(
repo.clone(),
queue.clone(),
theme.clone(),
key_config.clone(),
),
create_branch_popup: CreateBranchComponent::new(
repo.clone(),
queue.clone(),
theme.clone(),
key_config.clone(),
),
rename_branch_popup: RenameBranchComponent::new(
repo.clone(),
queue.clone(),
theme.clone(),
key_config.clone(),
),
select_branch_popup: BranchListComponent::new(
repo.clone(),
queue.clone(),
theme.clone(),
key_config.clone(),
),
tags_popup: TagListComponent::new(
repo.clone(),
&queue,
sender,
theme.clone(),
key_config.clone(),
),
options_popup: OptionsPopupComponent::new(
&queue,
theme.clone(),
key_config.clone(),
options.clone(),
),
submodule_popup: SubmodulesListComponent::new(
repo.clone(),
&queue,
theme.clone(),
key_config.clone(),
),
find_file_popup: FileFindPopup::new(
&queue,
theme.clone(),
key_config.clone(),
&env, sender,
),
external_editor_popup: ExternalEditorComponent::new(&env),
push_popup: PushComponent::new(&env, sender),
push_tags_popup: PushTagsComponent::new(&env, sender),
pull_popup: PullComponent::new(&env, sender),
fetch_popup: FetchComponent::new(&env, sender),
tag_commit_popup: TagCommitComponent::new(&env),
create_branch_popup: CreateBranchComponent::new(&env),
rename_branch_popup: RenameBranchComponent::new(&env),
select_branch_popup: BranchListComponent::new(&env),
tags_popup: TagListComponent::new(&env, sender),
options_popup: OptionsPopupComponent::new(&env),
submodule_popup: SubmodulesListComponent::new(&env),
find_file_popup: FileFindPopup::new(&env),
do_quit: QuitState::None,
cmdbar: RefCell::new(CommandBar::new(
theme.clone(),
key_config.clone(),
env.theme.clone(),
env.key_config.clone(),
)),
help: HelpComponent::new(
theme.clone(),
key_config.clone(),
),
msg: MsgComponent::new(theme.clone(), key_config.clone()),
revlog: Revlog::new(
&repo,
&queue,
sender,
theme.clone(),
key_config.clone(),
),
status_tab: Status::new(
repo.clone(),
&queue,
sender,
theme.clone(),
key_config.clone(),
options.clone(),
),
stashing_tab: Stashing::new(
&repo,
sender,
&queue,
theme.clone(),
key_config.clone(),
),
stashlist_tab: StashList::new(
repo.clone(),
&queue,
theme.clone(),
key_config.clone(),
),
files_tab: FilesTab::new(
repo.clone(),
sender_app,
&queue,
theme.clone(),
key_config.clone(),
),
help: HelpComponent::new(&env),
msg: MsgComponent::new(&env),
revlog: Revlog::new(&env, sender),
status_tab: Status::new(&env, sender),
stashing_tab: Stashing::new(&env, sender),
stashlist_tab: StashList::new(&env),
files_tab: FilesTab::new(&env, sender_app),
tab: 0,
queue,
theme,
options,
key_config,
queue: env.queue,
theme: env.theme,
options: env.options,
key_config: env.key_config,
requires_redraw: Cell::new(false),
file_to_open: None,
repo,
repo: env.repo,
repo_path_text,
popup_stack: PopupStack::default(),
};
Expand Down
16 changes: 7 additions & 9 deletions src/components/blame_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{
InspectCommitOpen,
};
use crate::{
app::Environment,
components::{utils::string_width_align, ScrollType},
keys::{key_match, SharedKeyConfig},
queue::{InternalEvent, Queue, StackablePopupOpen},
Expand All @@ -13,7 +14,7 @@ use crate::{
};
use anyhow::Result;
use asyncgit::{
sync::{BlameHunk, CommitId, FileBlame, RepoPathRef},
sync::{BlameHunk, CommitId, FileBlame},
AsyncBlame, AsyncGitNotification, BlameParams,
};
use crossbeam_channel::Sender;
Expand Down Expand Up @@ -272,27 +273,24 @@ impl Component for BlameFileComponent {
impl BlameFileComponent {
///
pub fn new(
repo: &RepoPathRef,
queue: &Queue,
env: &Environment,
sender: &Sender<AsyncGitNotification>,
title: &str,
theme: SharedTheme,
key_config: SharedKeyConfig,
) -> Self {
Self {
title: String::from(title),
theme,
theme: env.theme.clone(),
async_blame: AsyncBlame::new(
repo.borrow().clone(),
env.repo.borrow().clone(),
sender,
),
queue: queue.clone(),
queue: env.queue.clone(),
visible: false,
params: None,
file_blame: None,
open_request: None,
table_state: std::cell::Cell::new(TableState::default()),
key_config,
key_config: env.key_config.clone(),
current_height: std::cell::Cell::new(0),
}
}
Expand Down
16 changes: 6 additions & 10 deletions src/components/branchlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use super::{
EventState, InspectCommitOpen,
};
use crate::{
app::Environment,
components::ScrollType,
keys::{key_match, SharedKeyConfig},
queue::{
Expand Down Expand Up @@ -334,24 +335,19 @@ impl Component for BranchListComponent {
}

impl BranchListComponent {
pub fn new(
repo: RepoPathRef,
queue: Queue,
theme: SharedTheme,
key_config: SharedKeyConfig,
) -> Self {
pub fn new(env: &Environment) -> Self {
Self {
branches: Vec::new(),
local: true,
has_remotes: false,
visible: false,
selection: 0,
scroll: VerticalScroll::new(),
queue,
theme,
key_config,
queue: env.queue.clone(),
theme: env.theme.clone(),
key_config: env.key_config.clone(),
current_height: Cell::new(0),
repo,
repo: env.repo.clone(),
}
}

Expand Down
Loading

0 comments on commit c6e4713

Please sign in to comment.