Skip to content

Commit 9534e4c

Browse files
authored
persist current tab as options (#1339)
1 parent cb01fda commit 9534e4c

File tree

9 files changed

+126
-31
lines changed

9 files changed

+126
-31
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Added
1515
* submodules support ([#1087](https://github.com/extrawurst/gitui/issues/1087))
16+
* remember tab between app starts ([#1338](https://github.com/extrawurst/gitui/issues/1338))
1617
* customizable `cmdbar_bg` theme color & screen spanning selected line bg [[@gigitsu](https://github.com/gigitsu)] ([#1299](https://github.com/extrawurst/gitui/pull/1299))
17-
* use filewatcher instead of polling updates ([#1](https://github.com/extrawurst/gitui/issues/1))
1818
* word motions to text input [[@Rodrigodd](https://github.com/Rodrigodd)] ([#1256](https://github.com/extrawurst/gitui/issues/1256))
1919
* file blame at right revision from commit-details [[@heiskane](https://github.com/heiskane)] ([#1122](https://github.com/extrawurst/gitui/issues/1122))
2020
* 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)]
2121
* add `vendor-openssl` feature to allow building without vendored openssl [[@jirutka](https://github.com/jirutka)]
2222

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

2627
## [0.21.0] - 2021-08-17
2728

src/app.rs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ use crate::{
1010
FileRevlogComponent, HelpComponent, InspectCommitComponent,
1111
MsgComponent, OptionsPopupComponent, PullComponent,
1212
PushComponent, PushTagsComponent, RenameBranchComponent,
13-
RevisionFilesPopup, SharedOptions, StashMsgComponent,
13+
RevisionFilesPopup, StashMsgComponent,
1414
SubmodulesListComponent, TagCommitComponent,
1515
TagListComponent,
1616
},
1717
input::{Input, InputEvent, InputState},
1818
keys::{key_match, KeyConfig, SharedKeyConfig},
19+
options::{Options, SharedOptions},
1920
popup_stack::PopupStack,
2021
queue::{
2122
Action, InternalEvent, NeedsUpdate, Queue, StackablePopupOpen,
@@ -92,6 +93,7 @@ pub struct App {
9293
key_config: SharedKeyConfig,
9394
input: Input,
9495
popup_stack: PopupStack,
96+
options: SharedOptions,
9597

9698
// "Flags"
9799
requires_redraw: Cell<bool>,
@@ -109,15 +111,17 @@ impl App {
109111
input: Input,
110112
theme: Theme,
111113
key_config: KeyConfig,
112-
) -> Self {
114+
) -> Result<Self> {
113115
log::trace!("open repo at: {:?}", &repo);
114116

115117
let queue = Queue::new();
116118
let theme = Rc::new(theme);
117119
let key_config = Rc::new(key_config);
118-
let options = SharedOptions::default();
120+
let options = Options::new(repo.clone());
119121

120-
Self {
122+
let tab = options.borrow().current_tab();
123+
124+
let mut app = Self {
121125
input,
122126
reset: ConfirmComponent::new(
123127
queue.clone(),
@@ -263,7 +267,6 @@ impl App {
263267
key_config.clone(),
264268
),
265269
msg: MsgComponent::new(theme.clone(), key_config.clone()),
266-
tab: 0,
267270
revlog: Revlog::new(
268271
&repo,
269272
&queue,
@@ -277,7 +280,7 @@ impl App {
277280
sender,
278281
theme.clone(),
279282
key_config.clone(),
280-
options,
283+
options.clone(),
281284
),
282285
stashing_tab: Stashing::new(
283286
&repo,
@@ -299,14 +302,20 @@ impl App {
299302
theme.clone(),
300303
key_config.clone(),
301304
),
305+
tab: 0,
302306
queue,
303307
theme,
308+
options,
304309
key_config,
305310
requires_redraw: Cell::new(false),
306311
file_to_open: None,
307312
repo,
308313
popup_stack: PopupStack::default(),
309-
}
314+
};
315+
316+
app.set_tab(tab)?;
317+
318+
Ok(app)
310319
}
311320

312321
///
@@ -678,6 +687,7 @@ impl App {
678687
}
679688

680689
self.tab = tab;
690+
self.options.borrow_mut().set_current_tab(tab);
681691

682692
Ok(())
683693
}

src/components/changes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use super::{
22
status_tree::StatusTreeComponent,
33
utils::filetree::{FileTreeItem, FileTreeItemKind},
4-
CommandBlocking, DrawableComponent, SharedOptions,
4+
CommandBlocking, DrawableComponent,
55
};
66
use crate::{
77
components::{CommandInfo, Component, EventState},
88
keys::{key_match, SharedKeyConfig},
9+
options::SharedOptions,
910
queue::{Action, InternalEvent, NeedsUpdate, Queue, ResetItem},
1011
strings, try_or_popup,
1112
ui::style::SharedTheme,

src/components/file_revlog.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use super::{utils::logitems::ItemBatch, SharedOptions};
1+
use super::utils::logitems::ItemBatch;
22
use super::{visibility_blocking, BlameFileOpen, InspectCommitOpen};
33
use crate::keys::key_match;
4+
use crate::options::SharedOptions;
45
use crate::queue::StackablePopupOpen;
56
use crate::{
67
components::{
@@ -191,7 +192,7 @@ impl FileRevlogComponent {
191192
let diff_params = DiffParams {
192193
path: open_request.file_path.clone(),
193194
diff_type: DiffType::Commit(commit_id),
194-
options: self.options.borrow().diff,
195+
options: self.options.borrow().diff_options(),
195196
};
196197

197198
if let Some((params, last)) =

src/components/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ pub use file_revlog::{FileRevOpen, FileRevlogComponent};
5151
pub use help::HelpComponent;
5252
pub use inspect_commit::{InspectCommitComponent, InspectCommitOpen};
5353
pub use msg::MsgComponent;
54-
pub use options_popup::{
55-
AppOption, OptionsPopupComponent, SharedOptions,
56-
};
54+
pub use options_popup::{AppOption, OptionsPopupComponent};
5755
pub use pull::PullComponent;
5856
pub use push::PushComponent;
5957
pub use push_tags::PushTagsComponent;

src/components/options_popup.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
use std::{cell::RefCell, rc::Rc};
2-
31
use super::{
42
visibility_blocking, CommandBlocking, CommandInfo, Component,
53
DrawableComponent, EventState,
64
};
75
use crate::{
86
components::utils::string_width_align,
97
keys::{key_match, SharedKeyConfig},
8+
options::SharedOptions,
109
queue::{InternalEvent, Queue},
1110
strings::{self},
1211
ui::{self, style::SharedTheme},
1312
};
1413
use anyhow::Result;
15-
use asyncgit::sync::{diff::DiffOptions, ShowUntrackedFilesConfig};
14+
use asyncgit::sync::ShowUntrackedFilesConfig;
1615
use crossterm::event::Event;
1716
use tui::{
1817
backend::Backend,
@@ -31,14 +30,6 @@ pub enum AppOption {
3130
DiffInterhunkLines,
3231
}
3332

34-
#[derive(Default, Copy, Clone)]
35-
pub struct Options {
36-
pub status_show_untracked: Option<ShowUntrackedFilesConfig>,
37-
pub diff: DiffOptions,
38-
}
39-
40-
pub type SharedOptions = Rc<RefCell<Options>>;
41-
4233
pub struct OptionsPopupComponent {
4334
selection: AppOption,
4435
queue: Queue,
@@ -91,26 +82,27 @@ impl OptionsPopupComponent {
9182
);
9283
Self::add_header(txt, "");
9384

85+
let diff = self.options.borrow().diff_options();
9486
Self::add_header(txt, "Diff");
9587
self.add_entry(
9688
txt,
9789
width,
9890
"Ignore whitespaces",
99-
&self.options.borrow().diff.ignore_whitespace.to_string(),
91+
&diff.ignore_whitespace.to_string(),
10092
self.is_select(AppOption::DiffIgnoreWhitespaces),
10193
);
10294
self.add_entry(
10395
txt,
10496
width,
10597
"Context lines",
106-
&self.options.borrow().diff.context.to_string(),
98+
&diff.context.to_string(),
10799
self.is_select(AppOption::DiffContextLines),
108100
);
109101
self.add_entry(
110102
txt,
111103
width,
112104
"Inter hunk lines",
113-
&self.options.borrow().diff.interhunk_lines.to_string(),
105+
&diff.interhunk_lines.to_string(),
114106
self.is_select(AppOption::DiffInterhunkLines),
115107
);
116108
}

src/main.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ mod components;
2727
mod input;
2828
mod keys;
2929
mod notify_mutex;
30+
mod options;
3031
mod popup_stack;
3132
mod profiler;
3233
mod queue;
@@ -175,7 +176,7 @@ fn run_app(
175176
input.clone(),
176177
theme,
177178
key_config,
178-
);
179+
)?;
179180

180181
let mut spinner = Spinner::default();
181182
let mut first_update = true;

src/options.rs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
use anyhow::Result;
2+
use asyncgit::sync::{
3+
diff::DiffOptions, repo_dir, RepoPathRef,
4+
ShowUntrackedFilesConfig,
5+
};
6+
use ron::{
7+
de::from_bytes,
8+
ser::{to_string_pretty, PrettyConfig},
9+
};
10+
use serde::{Deserialize, Serialize};
11+
use std::{
12+
cell::RefCell,
13+
fs::File,
14+
io::{Read, Write},
15+
path::PathBuf,
16+
rc::Rc,
17+
};
18+
19+
#[derive(Default, Copy, Clone, Serialize, Deserialize)]
20+
struct OptionsData {
21+
pub tab: usize,
22+
}
23+
24+
#[derive(Clone)]
25+
pub struct Options {
26+
//TODO: un-pub and use getters/setters and move into persisted data
27+
pub status_show_untracked: Option<ShowUntrackedFilesConfig>,
28+
pub diff: DiffOptions,
29+
30+
repo: RepoPathRef,
31+
data: OptionsData,
32+
}
33+
34+
pub type SharedOptions = Rc<RefCell<Options>>;
35+
36+
impl Options {
37+
pub fn new(repo: RepoPathRef) -> SharedOptions {
38+
Rc::new(RefCell::new(Self {
39+
data: Self::read(&repo).unwrap_or_default(),
40+
diff: DiffOptions::default(),
41+
status_show_untracked: None,
42+
repo,
43+
}))
44+
}
45+
46+
pub fn set_current_tab(&mut self, tab: usize) {
47+
self.data.tab = tab;
48+
self.save();
49+
}
50+
51+
pub const fn current_tab(&self) -> usize {
52+
self.data.tab
53+
}
54+
55+
pub const fn diff_options(&self) -> DiffOptions {
56+
self.diff
57+
}
58+
59+
fn save(&self) {
60+
if let Err(e) = self.save_failable() {
61+
log::error!("options save error: {}", e);
62+
}
63+
}
64+
65+
fn read(repo: &RepoPathRef) -> Result<OptionsData> {
66+
let dir = Self::options_file(repo)?;
67+
68+
let mut f = File::open(dir)?;
69+
let mut buffer = Vec::new();
70+
f.read_to_end(&mut buffer)?;
71+
Ok(from_bytes(&buffer)?)
72+
}
73+
74+
fn save_failable(&self) -> Result<()> {
75+
let dir = Self::options_file(&self.repo)?;
76+
77+
let mut file = File::create(&dir)?;
78+
let data =
79+
to_string_pretty(&self.data, PrettyConfig::default())?;
80+
file.write_all(data.as_bytes())?;
81+
82+
Ok(())
83+
}
84+
85+
fn options_file(repo: &RepoPathRef) -> Result<PathBuf> {
86+
let dir = repo_dir(&repo.borrow())?;
87+
let dir = dir.join("gitui");
88+
Ok(dir)
89+
}
90+
}

src/tabs/status.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use crate::{
44
command_pump, event_pump, visibility_blocking,
55
ChangesComponent, CommandBlocking, CommandInfo, Component,
66
DiffComponent, DrawableComponent, EventState,
7-
FileTreeItemKind, SharedOptions,
7+
FileTreeItemKind,
88
},
99
keys::{key_match, SharedKeyConfig},
10+
options::SharedOptions,
1011
queue::{Action, InternalEvent, NeedsUpdate, Queue, ResetItem},
1112
strings, try_or_popup,
1213
ui::style::SharedTheme,
@@ -490,7 +491,7 @@ impl Status {
490491
let diff_params = DiffParams {
491492
path: path.clone(),
492493
diff_type,
493-
options: self.options.borrow().diff,
494+
options: self.options.borrow().diff_options(),
494495
};
495496

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

0 commit comments

Comments
 (0)