Skip to content

Commit

Permalink
Persist all options (extrawurst#1342)
Browse files Browse the repository at this point in the history
  • Loading branch information
extrawurst authored and heiskane committed Oct 20, 2022
1 parent 50d3964 commit 3c4ac2f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 42 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions asyncgit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ log = "0.4"
openssl-sys = { version = '0.9', features = ["vendored"], optional = true }
rayon-core = "1.9"
scopetime = { path = "../scopetime", version = "0.1" }
serde = "1.0"
shellexpand = "2.1"
thiserror = "1.0"
unicode-truncate = "0.2.0"
Expand Down
8 changes: 7 additions & 1 deletion asyncgit/src/sync/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
//TODO: hopefully released in next rust (see https://github.com/rust-lang/rust-clippy/issues/9440)
#![allow(clippy::use_self)]

use crate::error::Result;
use git2::Repository;
use scopetime::scope_time;
use serde::{Deserialize, Serialize};

use super::{repository::repo, RepoPath};

// see https://git-scm.com/docs/git-config#Documentation/git-config.txt-statusshowUntrackedFiles
/// represents the `status.showUntrackedFiles` git config state
#[derive(Hash, Copy, Clone, PartialEq, Eq)]
#[derive(
Hash, Copy, Clone, PartialEq, Eq, Serialize, Deserialize,
)]
pub enum ShowUntrackedFilesConfig {
///
No,
Expand Down
5 changes: 4 additions & 1 deletion asyncgit/src/sync/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use git2::{
Delta, Diff, DiffDelta, DiffFormat, DiffHunk, Patch, Repository,
};
use scopetime::scope_time;
use serde::{Deserialize, Serialize};
use std::{cell::RefCell, fs, path::Path, rc::Rc};

/// type of diff of a single line
Expand Down Expand Up @@ -127,7 +128,9 @@ pub struct FileDiff {
}

/// see <https://libgit2.org/libgit2/#HEAD/type/git_diff_options>
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
#[derive(
Debug, Hash, Clone, Copy, PartialEq, Eq, Serialize, Deserialize,
)]
pub struct DiffOptions {
/// see <https://libgit2.org/libgit2/#HEAD/type/git_diff_options>
pub ignore_whitespace: bool,
Expand Down
6 changes: 3 additions & 3 deletions src/components/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl ChangesComponent {
};
} else {
let config =
self.options.borrow().status_show_untracked;
self.options.borrow().status_show_untracked();

//TODO: check if we can handle the one file case with it aswell
sync::stage_add_all(
Expand All @@ -123,7 +123,7 @@ impl ChangesComponent {
// would mean that after staging the workdir becomes empty
if sync::is_workdir_clean(
&self.repo.borrow(),
self.options.borrow().status_show_untracked,
self.options.borrow().status_show_untracked(),
)? {
self.queue
.push(InternalEvent::StatusLastFileMoved);
Expand All @@ -141,7 +141,7 @@ impl ChangesComponent {
}

fn index_add_all(&mut self) -> Result<()> {
let config = self.options.borrow().status_show_untracked;
let config = self.options.borrow().status_show_untracked();

sync::stage_add_all(&self.repo.borrow(), "*", config)?;

Expand Down
52 changes: 23 additions & 29 deletions src/components/options_popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl OptionsPopupComponent {
txt,
width,
"Show untracked",
match self.options.borrow().status_show_untracked {
match self.options.borrow().status_show_untracked() {
None => "Gitconfig",
Some(ShowUntrackedFilesConfig::No) => "No",
Some(ShowUntrackedFilesConfig::Normal) => "Normal",
Expand Down Expand Up @@ -179,7 +179,7 @@ impl OptionsPopupComponent {
match self.selection {
AppOption::StatusShowUntracked => {
let untracked =
self.options.borrow().status_show_untracked;
self.options.borrow().status_show_untracked();

let untracked = match untracked {
None => {
Expand All @@ -194,34 +194,31 @@ impl OptionsPopupComponent {
Some(ShowUntrackedFilesConfig::No) => None,
};

self.options.borrow_mut().status_show_untracked =
untracked;
self.options
.borrow_mut()
.set_status_show_untracked(untracked);
}
AppOption::DiffIgnoreWhitespaces => {
let old =
self.options.borrow().diff.ignore_whitespace;
self.options
.borrow_mut()
.diff
.ignore_whitespace = !old;
.diff_toggle_whitespace();
}
AppOption::DiffContextLines => {
let old = self.options.borrow().diff.context;
self.options.borrow_mut().diff.context =
old.saturating_add(1);
self.options
.borrow_mut()
.diff_context_change(true);
}
AppOption::DiffInterhunkLines => {
let old =
self.options.borrow().diff.interhunk_lines;
self.options.borrow_mut().diff.interhunk_lines =
old.saturating_add(1);
self.options
.borrow_mut()
.diff_hunk_lines_change(true);
}
};
} else {
match self.selection {
AppOption::StatusShowUntracked => {
let untracked =
self.options.borrow().status_show_untracked;
self.options.borrow().status_show_untracked();

let untracked = match untracked {
None => Some(ShowUntrackedFilesConfig::No),
Expand All @@ -236,27 +233,24 @@ impl OptionsPopupComponent {
}
};

self.options.borrow_mut().status_show_untracked =
untracked;
self.options
.borrow_mut()
.set_status_show_untracked(untracked);
}
AppOption::DiffIgnoreWhitespaces => {
let old =
self.options.borrow().diff.ignore_whitespace;
self.options
.borrow_mut()
.diff
.ignore_whitespace = !old;
.diff_toggle_whitespace();
}
AppOption::DiffContextLines => {
let old = self.options.borrow().diff.context;
self.options.borrow_mut().diff.context =
old.saturating_sub(1);
self.options
.borrow_mut()
.diff_context_change(false);
}
AppOption::DiffInterhunkLines => {
let old =
self.options.borrow().diff.interhunk_lines;
self.options.borrow_mut().diff.interhunk_lines =
old.saturating_sub(1);
self.options
.borrow_mut()
.diff_hunk_lines_change(false);
}
};
}
Expand Down
51 changes: 44 additions & 7 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ use std::{
#[derive(Default, Copy, Clone, Serialize, Deserialize)]
struct OptionsData {
pub tab: usize,
pub diff: DiffOptions,
pub status_show_untracked: Option<ShowUntrackedFilesConfig>,
}

#[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,
}
Expand All @@ -37,8 +35,6 @@ 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,
}))
}
Expand All @@ -53,7 +49,48 @@ impl Options {
}

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

pub const fn status_show_untracked(
&self,
) -> Option<ShowUntrackedFilesConfig> {
self.data.status_show_untracked
}

pub fn set_status_show_untracked(
&mut self,
value: Option<ShowUntrackedFilesConfig>,
) {
self.data.status_show_untracked = value;
self.save();
}

pub fn diff_context_change(&mut self, increase: bool) {
self.data.diff.context = if increase {
self.data.diff.context.saturating_add(1)
} else {
self.data.diff.context.saturating_sub(1)
};

self.save();
}

pub fn diff_hunk_lines_change(&mut self, increase: bool) {
self.data.diff.interhunk_lines = if increase {
self.data.diff.interhunk_lines.saturating_add(1)
} else {
self.data.diff.interhunk_lines.saturating_sub(1)
};

self.save();
}

pub fn diff_toggle_whitespace(&mut self) {
self.data.diff.ignore_whitespace =
!self.data.diff.ignore_whitespace;

self.save();
}

fn save(&self) {
Expand Down
3 changes: 2 additions & 1 deletion src/tabs/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ impl Status {
self.git_branch_name.lookup().map(Some).unwrap_or(None);

if self.is_visible() {
let config = self.options.borrow().status_show_untracked;
let config =
self.options.borrow().status_show_untracked();

self.git_diff.refresh()?;
self.git_status_workdir.fetch(&StatusParams::new(
Expand Down

0 comments on commit 3c4ac2f

Please sign in to comment.