Skip to content

Commit 2e48f9e

Browse files
committed
refacto pending info
1 parent 05f8f36 commit 2e48f9e

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed

src/info/pending.rs

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,57 +6,65 @@ use serde::Serialize;
66
#[derive(Serialize)]
77
#[serde(rename_all = "camelCase")]
88
pub struct PendingInfo {
9-
pub pending_changes: String,
9+
added: usize,
10+
deleted: usize,
11+
modified: usize,
1012
}
1113

1214
impl PendingInfo {
1315
pub fn new(repo: &Repository) -> Result<Self> {
14-
let pending_changes = get_pending_changes(repo)?;
15-
Ok(Self { pending_changes })
16+
let statuses = repo
17+
.status(gix::progress::Discard)?
18+
.dirwalk_options(|options| {
19+
options.emit_untracked(gix::dir::walk::EmissionMode::Matching)
20+
})
21+
.into_index_worktree_iter(Vec::new())?;
22+
23+
let (added, deleted, modified) = statuses
24+
.take_while(Result::is_ok)
25+
.filter_map(Result::ok)
26+
.filter_map(|item| item.summary())
27+
.fold((0, 0, 0), |(added, deleted, modified), status| {
28+
use gix::status::index_worktree::iter::Summary;
29+
match status {
30+
Summary::Removed => (added, deleted + 1, modified),
31+
Summary::Added | Summary::Copied => (added + 1, deleted, modified),
32+
Summary::Modified | Summary::TypeChange => (added, deleted, modified + 1),
33+
Summary::Renamed => (added + 1, deleted + 1, modified),
34+
Summary::IntentToAdd | Summary::Conflict => (added, deleted, modified),
35+
}
36+
});
37+
Ok(Self {
38+
added,
39+
deleted,
40+
modified,
41+
})
1642
}
1743
}
1844

19-
fn get_pending_changes(repo: &Repository) -> Result<String> {
20-
let statuses = repo
21-
.status(gix::progress::Discard)?
22-
.dirwalk_options(|options| options.emit_untracked(gix::dir::walk::EmissionMode::Matching))
23-
.into_index_worktree_iter(Vec::new())?;
45+
impl std::fmt::Display for PendingInfo {
46+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
47+
let mut result = String::new();
48+
if self.modified > 0 {
49+
result = format!("{}+-", self.modified);
50+
}
2451

25-
let (added, deleted, modified) = statuses
26-
.take_while(Result::is_ok)
27-
.filter_map(Result::ok)
28-
.filter_map(|item| item.summary())
29-
.fold((0, 0, 0), |(added, deleted, modified), status| {
30-
use gix::status::index_worktree::iter::Summary;
31-
match status {
32-
Summary::Removed => (added, deleted + 1, modified),
33-
Summary::Added | Summary::Copied => (added + 1, deleted, modified),
34-
Summary::Modified | Summary::TypeChange => (added, deleted, modified + 1),
35-
Summary::Renamed => (added + 1, deleted + 1, modified),
36-
Summary::IntentToAdd | Summary::Conflict => (added, deleted, modified),
37-
}
38-
});
52+
if self.added > 0 {
53+
result = format!("{result} {}+", self.added);
54+
}
3955

40-
let mut result = String::new();
41-
if modified > 0 {
42-
result = format!("{modified}+-");
43-
}
56+
if self.deleted > 0 {
57+
result = format!("{result} {}-", self.deleted);
58+
}
4459

45-
if added > 0 {
46-
result = format!("{result} {added}+");
60+
write!(f, "{}", result.trim())
4761
}
48-
49-
if deleted > 0 {
50-
result = format!("{result} {deleted}-");
51-
}
52-
53-
Ok(result.trim().into())
5462
}
5563

5664
#[typetag::serialize]
5765
impl InfoField for PendingInfo {
5866
fn value(&self) -> String {
59-
self.pending_changes.to_string()
67+
self.to_string()
6068
}
6169

6270
fn title(&self) -> String {
@@ -71,7 +79,9 @@ mod test {
7179
#[test]
7280
fn test_display_pending_info() {
7381
let pending_info = PendingInfo {
74-
pending_changes: "4+-".to_string(),
82+
added: 0,
83+
deleted: 0,
84+
modified: 4,
7585
};
7686

7787
assert_eq!(pending_info.value(), "4+-".to_string());

tests/snapshots/repo__repo.snap

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ expression: info
3232
},
3333
{
3434
"PendingInfo": {
35-
"pendingChanges": "1+- 2+"
35+
"added": 2,
36+
"deleted": 0,
37+
"modified": 1
3638
}
3739
},
3840
{

0 commit comments

Comments
 (0)