Skip to content

Commit

Permalink
persist current tab as options (#1339)
Browse files Browse the repository at this point in the history
  • Loading branch information
extrawurst authored Sep 18, 2022
1 parent cb01fda commit 9534e4c
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 31 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
* submodules support ([#1087](https://github.com/extrawurst/gitui/issues/1087))
* remember tab between app starts ([#1338](https://github.com/extrawurst/gitui/issues/1338))
* customizable `cmdbar_bg` theme color & screen spanning selected line bg [[@gigitsu](https://github.com/gigitsu)] ([#1299](https://github.com/extrawurst/gitui/pull/1299))
* use filewatcher instead of polling updates ([#1](https://github.com/extrawurst/gitui/issues/1))
* word motions to text input [[@Rodrigodd](https://github.com/Rodrigodd)] ([#1256](https://github.com/extrawurst/gitui/issues/1256))
* file blame at right revision from commit-details [[@heiskane](https://github.com/heiskane)] ([#1122](https://github.com/extrawurst/gitui/issues/1122))
* add `regex-fancy` and `regex-onig` features to allow building Syntect with Onigumara regex engine instead of the default engine based on fancy-regex [[@jirutka](https://github.com/jirutka)]
* add `vendor-openssl` feature to allow building without vendored openssl [[@jirutka](https://github.com/jirutka)]

### Fixes
* remove insecure dependency `ansi_term` ([#1290](https://github.com/extrawurst/gitui/issues/1290))
* use filewatcher instead of polling updates ([#1](https://github.com/extrawurst/gitui/issues/1))

## [0.21.0] - 2021-08-17

Expand Down
24 changes: 17 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ use crate::{
FileRevlogComponent, HelpComponent, InspectCommitComponent,
MsgComponent, OptionsPopupComponent, PullComponent,
PushComponent, PushTagsComponent, RenameBranchComponent,
RevisionFilesPopup, SharedOptions, StashMsgComponent,
RevisionFilesPopup, StashMsgComponent,
SubmodulesListComponent, TagCommitComponent,
TagListComponent,
},
input::{Input, InputEvent, InputState},
keys::{key_match, KeyConfig, SharedKeyConfig},
options::{Options, SharedOptions},
popup_stack::PopupStack,
queue::{
Action, InternalEvent, NeedsUpdate, Queue, StackablePopupOpen,
Expand Down Expand Up @@ -92,6 +93,7 @@ pub struct App {
key_config: SharedKeyConfig,
input: Input,
popup_stack: PopupStack,
options: SharedOptions,

// "Flags"
requires_redraw: Cell<bool>,
Expand All @@ -109,15 +111,17 @@ impl App {
input: Input,
theme: Theme,
key_config: KeyConfig,
) -> Self {
) -> Result<Self> {
log::trace!("open repo at: {:?}", &repo);

let queue = Queue::new();
let theme = Rc::new(theme);
let key_config = Rc::new(key_config);
let options = SharedOptions::default();
let options = Options::new(repo.clone());

Self {
let tab = options.borrow().current_tab();

let mut app = Self {
input,
reset: ConfirmComponent::new(
queue.clone(),
Expand Down Expand Up @@ -263,7 +267,6 @@ impl App {
key_config.clone(),
),
msg: MsgComponent::new(theme.clone(), key_config.clone()),
tab: 0,
revlog: Revlog::new(
&repo,
&queue,
Expand All @@ -277,7 +280,7 @@ impl App {
sender,
theme.clone(),
key_config.clone(),
options,
options.clone(),
),
stashing_tab: Stashing::new(
&repo,
Expand All @@ -299,14 +302,20 @@ impl App {
theme.clone(),
key_config.clone(),
),
tab: 0,
queue,
theme,
options,
key_config,
requires_redraw: Cell::new(false),
file_to_open: None,
repo,
popup_stack: PopupStack::default(),
}
};

app.set_tab(tab)?;

Ok(app)
}

///
Expand Down Expand Up @@ -678,6 +687,7 @@ impl App {
}

self.tab = tab;
self.options.borrow_mut().set_current_tab(tab);

Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion src/components/changes.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::{
status_tree::StatusTreeComponent,
utils::filetree::{FileTreeItem, FileTreeItemKind},
CommandBlocking, DrawableComponent, SharedOptions,
CommandBlocking, DrawableComponent,
};
use crate::{
components::{CommandInfo, Component, EventState},
keys::{key_match, SharedKeyConfig},
options::SharedOptions,
queue::{Action, InternalEvent, NeedsUpdate, Queue, ResetItem},
strings, try_or_popup,
ui::style::SharedTheme,
Expand Down
5 changes: 3 additions & 2 deletions src/components/file_revlog.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{utils::logitems::ItemBatch, SharedOptions};
use super::utils::logitems::ItemBatch;
use super::{visibility_blocking, BlameFileOpen, InspectCommitOpen};
use crate::keys::key_match;
use crate::options::SharedOptions;
use crate::queue::StackablePopupOpen;
use crate::{
components::{
Expand Down Expand Up @@ -191,7 +192,7 @@ impl FileRevlogComponent {
let diff_params = DiffParams {
path: open_request.file_path.clone(),
diff_type: DiffType::Commit(commit_id),
options: self.options.borrow().diff,
options: self.options.borrow().diff_options(),
};

if let Some((params, last)) =
Expand Down
4 changes: 1 addition & 3 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ pub use file_revlog::{FileRevOpen, FileRevlogComponent};
pub use help::HelpComponent;
pub use inspect_commit::{InspectCommitComponent, InspectCommitOpen};
pub use msg::MsgComponent;
pub use options_popup::{
AppOption, OptionsPopupComponent, SharedOptions,
};
pub use options_popup::{AppOption, OptionsPopupComponent};
pub use pull::PullComponent;
pub use push::PushComponent;
pub use push_tags::PushTagsComponent;
Expand Down
20 changes: 6 additions & 14 deletions src/components/options_popup.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
use std::{cell::RefCell, rc::Rc};

use super::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent, EventState,
};
use crate::{
components::utils::string_width_align,
keys::{key_match, SharedKeyConfig},
options::SharedOptions,
queue::{InternalEvent, Queue},
strings::{self},
ui::{self, style::SharedTheme},
};
use anyhow::Result;
use asyncgit::sync::{diff::DiffOptions, ShowUntrackedFilesConfig};
use asyncgit::sync::ShowUntrackedFilesConfig;
use crossterm::event::Event;
use tui::{
backend::Backend,
Expand All @@ -31,14 +30,6 @@ pub enum AppOption {
DiffInterhunkLines,
}

#[derive(Default, Copy, Clone)]
pub struct Options {
pub status_show_untracked: Option<ShowUntrackedFilesConfig>,
pub diff: DiffOptions,
}

pub type SharedOptions = Rc<RefCell<Options>>;

pub struct OptionsPopupComponent {
selection: AppOption,
queue: Queue,
Expand Down Expand Up @@ -91,26 +82,27 @@ impl OptionsPopupComponent {
);
Self::add_header(txt, "");

let diff = self.options.borrow().diff_options();
Self::add_header(txt, "Diff");
self.add_entry(
txt,
width,
"Ignore whitespaces",
&self.options.borrow().diff.ignore_whitespace.to_string(),
&diff.ignore_whitespace.to_string(),
self.is_select(AppOption::DiffIgnoreWhitespaces),
);
self.add_entry(
txt,
width,
"Context lines",
&self.options.borrow().diff.context.to_string(),
&diff.context.to_string(),
self.is_select(AppOption::DiffContextLines),
);
self.add_entry(
txt,
width,
"Inter hunk lines",
&self.options.borrow().diff.interhunk_lines.to_string(),
&diff.interhunk_lines.to_string(),
self.is_select(AppOption::DiffInterhunkLines),
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod components;
mod input;
mod keys;
mod notify_mutex;
mod options;
mod popup_stack;
mod profiler;
mod queue;
Expand Down Expand Up @@ -175,7 +176,7 @@ fn run_app(
input.clone(),
theme,
key_config,
);
)?;

let mut spinner = Spinner::default();
let mut first_update = true;
Expand Down
90 changes: 90 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use anyhow::Result;
use asyncgit::sync::{
diff::DiffOptions, repo_dir, RepoPathRef,
ShowUntrackedFilesConfig,
};
use ron::{
de::from_bytes,
ser::{to_string_pretty, PrettyConfig},
};
use serde::{Deserialize, Serialize};
use std::{
cell::RefCell,
fs::File,
io::{Read, Write},
path::PathBuf,
rc::Rc,
};

#[derive(Default, Copy, Clone, Serialize, Deserialize)]
struct OptionsData {
pub tab: usize,
}

#[derive(Clone)]
pub struct Options {
//TODO: un-pub and use getters/setters and move into persisted data
pub status_show_untracked: Option<ShowUntrackedFilesConfig>,
pub diff: DiffOptions,

repo: RepoPathRef,
data: OptionsData,
}

pub type SharedOptions = Rc<RefCell<Options>>;

impl Options {
pub fn new(repo: RepoPathRef) -> SharedOptions {
Rc::new(RefCell::new(Self {
data: Self::read(&repo).unwrap_or_default(),
diff: DiffOptions::default(),
status_show_untracked: None,
repo,
}))
}

pub fn set_current_tab(&mut self, tab: usize) {
self.data.tab = tab;
self.save();
}

pub const fn current_tab(&self) -> usize {
self.data.tab
}

pub const fn diff_options(&self) -> DiffOptions {
self.diff
}

fn save(&self) {
if let Err(e) = self.save_failable() {
log::error!("options save error: {}", e);
}
}

fn read(repo: &RepoPathRef) -> Result<OptionsData> {
let dir = Self::options_file(repo)?;

let mut f = File::open(dir)?;
let mut buffer = Vec::new();
f.read_to_end(&mut buffer)?;
Ok(from_bytes(&buffer)?)
}

fn save_failable(&self) -> Result<()> {
let dir = Self::options_file(&self.repo)?;

let mut file = File::create(&dir)?;
let data =
to_string_pretty(&self.data, PrettyConfig::default())?;
file.write_all(data.as_bytes())?;

Ok(())
}

fn options_file(repo: &RepoPathRef) -> Result<PathBuf> {
let dir = repo_dir(&repo.borrow())?;
let dir = dir.join("gitui");
Ok(dir)
}
}
5 changes: 3 additions & 2 deletions src/tabs/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use crate::{
command_pump, event_pump, visibility_blocking,
ChangesComponent, CommandBlocking, CommandInfo, Component,
DiffComponent, DrawableComponent, EventState,
FileTreeItemKind, SharedOptions,
FileTreeItemKind,
},
keys::{key_match, SharedKeyConfig},
options::SharedOptions,
queue::{Action, InternalEvent, NeedsUpdate, Queue, ResetItem},
strings, try_or_popup,
ui::style::SharedTheme,
Expand Down Expand Up @@ -490,7 +491,7 @@ impl Status {
let diff_params = DiffParams {
path: path.clone(),
diff_type,
options: self.options.borrow().diff,
options: self.options.borrow().diff_options(),
};

if self.diff.current() == (path.clone(), is_stage) {
Expand Down

0 comments on commit 9534e4c

Please sign in to comment.