Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(crate) struct Config {
pub struct GeneralConfig {
pub always_show_help: BoolConfigEntry,
pub confirm_quit: BoolConfigEntry,
pub collapsed_sections: Vec<String>,
}

#[derive(Default, Debug, Deserialize)]
Expand Down
3 changes: 3 additions & 0 deletions src/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
[general]
always_show_help.enabled = false
confirm_quit.enabled = false
# Sets initially collapsed sections in the editor. e.g.:
# collapsed_sections = ["untracked", "recent_commits", "branch_status"]
collapsed_sections = []

[style]
# fg / bg can be either of:
Expand Down
10 changes: 9 additions & 1 deletion src/screen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ impl Screen {
size: Size,
refresh_items: Box<dyn Fn() -> Res<Vec<Item>>>,
) -> Res<Self> {
let collapsed = config
.general
.collapsed_sections
.clone()
.into_iter()
.map(Cow::Owned)
.collect();

let mut screen = Self {
cursor: 0,
scroll: 0,
Expand All @@ -44,7 +52,7 @@ impl Screen {
refresh_items,
items: vec![],
line_index: vec![],
collapsed: HashSet::new(),
collapsed,
};

screen.update()?;
Expand Down
41 changes: 28 additions & 13 deletions src/screen/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,25 @@ pub(crate) fn create(config: Rc<Config>, repo: Rc<Repository>, size: Size) -> Re
.chain(unmerged)
.chain(create_status_section_items(
Rc::clone(&config),
"Unstaged changes",
"unstaged_changes",
Some(TargetData::AllUnstaged),
&git::diff_unstaged(&config, repo.as_ref())?,
))
.chain(create_status_section_items(
Rc::clone(&config),
"Staged changes",
"staged_changes",
Some(TargetData::AllStaged),
&git::diff_staged(&config, repo.as_ref())?,
))
.chain(create_stash_list_section_items(
Rc::clone(&config),
repo.as_ref(),
"Stashes",
"stashes",
))
.chain(create_log_section_items(
Rc::clone(&config),
repo.as_ref(),
"Recent commits",
"recent_commits",
))
.collect();

Expand Down Expand Up @@ -219,7 +219,7 @@ fn branch_status_items(config: &Config, repo: &Repository) -> Res<Vec<Item>> {

fn create_status_section_items<'a>(
config: Rc<Config>,
header: &str,
snake_case_header: &str,
header_data: Option<TargetData>,
diff: &'a Diff,
) -> impl Iterator<Item = Item> + 'a {
Expand All @@ -235,9 +235,12 @@ fn create_status_section_items<'a>(
..Default::default()
},
Item {
id: header.to_string().into(),
id: snake_case_header.to_string().into(),
display: Line::from(vec![
Span::styled(header.to_string(), &style.section_header),
Span::styled(
capitalize(&snake_case_header.replace("_", " ")),
&style.section_header,
),
format!(" ({})", diff.deltas.len()).into(),
]),
section: true,
Expand All @@ -251,10 +254,16 @@ fn create_status_section_items<'a>(
.chain(items::create_diff_items(config, diff, &1, true))
}

fn capitalize(str: &str) -> String {
let first: String = str.chars().take(1).flat_map(char::to_uppercase).collect();
let rest: String = str.chars().skip(1).collect();
format!("{first}{rest}")
}

fn create_stash_list_section_items<'a>(
config: Rc<Config>,
repo: &Repository,
header: &str,
snake_case_header: &str,
) -> impl Iterator<Item = Item> + 'a {
let stashes = items::stash_list(&config, repo, 10).unwrap();
if stashes.is_empty() {
Expand All @@ -264,8 +273,11 @@ fn create_stash_list_section_items<'a>(
vec![
items::blank_line(),
Item {
id: header.to_string().into(),
display: Line::styled(header.to_string(), &style.section_header),
id: snake_case_header.to_string().into(),
display: Line::styled(
capitalize(&snake_case_header.replace("_", " ")),
&style.section_header,
),
section: true,
depth: 0,
..Default::default()
Expand All @@ -279,7 +291,7 @@ fn create_stash_list_section_items<'a>(
fn create_log_section_items<'a>(
config: Rc<Config>,
repo: &Repository,
header: &str,
snake_case_header: &str,
) -> impl Iterator<Item = Item> + 'a {
let style = &config.style;
[
Expand All @@ -290,8 +302,11 @@ fn create_log_section_items<'a>(
..Default::default()
},
Item {
id: header.to_string().into(),
display: Line::styled(header.to_string(), &style.section_header),
id: snake_case_header.to_string().into(),
display: Line::styled(
capitalize(&snake_case_header.replace("_", " ")),
&style.section_header,
),
section: true,
depth: 0,
..Default::default()
Expand Down
14 changes: 14 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ fn binary_file() {
snapshot!(ctx, "jj<tab>");
}

#[test]
fn collapsed_sections_config() {
let mut ctx = TestContext::setup_clone();
ctx.config().general.collapsed_sections = vec![
"untracked".into(),
"recent_commits".into(),
"branch_status".into(),
// TODO rebase / revert/ merge conlict?
];
fs::write(ctx.dir.child("untracked_file.txt"), "").unwrap();

snapshot!(ctx, "");
}

#[test]
fn log() {
let ctx = TestContext::setup_clone();
Expand Down
25 changes: 25 additions & 0 deletions src/tests/snapshots/gitu__tests__collapsed_sections_config.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
source: src/tests/mod.rs
expression: ctx.redact_buffer()
---
▌On branch main… |
|
Untracked files… |
|
Recent commits… |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
styles_hash: cbfd7594a526d60d