Skip to content

Commit

Permalink
refacto pending info
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Mar 16, 2024
1 parent 05f8f36 commit 2e48f9e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 37 deletions.
82 changes: 46 additions & 36 deletions src/info/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,65 @@ use serde::Serialize;
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PendingInfo {
pub pending_changes: String,
added: usize,
deleted: usize,
modified: usize,
}

impl PendingInfo {
pub fn new(repo: &Repository) -> Result<Self> {
let pending_changes = get_pending_changes(repo)?;
Ok(Self { pending_changes })
let statuses = repo
.status(gix::progress::Discard)?
.dirwalk_options(|options| {
options.emit_untracked(gix::dir::walk::EmissionMode::Matching)
})
.into_index_worktree_iter(Vec::new())?;

let (added, deleted, modified) = statuses
.take_while(Result::is_ok)
.filter_map(Result::ok)
.filter_map(|item| item.summary())
.fold((0, 0, 0), |(added, deleted, modified), status| {
use gix::status::index_worktree::iter::Summary;
match status {
Summary::Removed => (added, deleted + 1, modified),
Summary::Added | Summary::Copied => (added + 1, deleted, modified),
Summary::Modified | Summary::TypeChange => (added, deleted, modified + 1),
Summary::Renamed => (added + 1, deleted + 1, modified),
Summary::IntentToAdd | Summary::Conflict => (added, deleted, modified),
}
});
Ok(Self {
added,
deleted,
modified,
})
}
}

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

let (added, deleted, modified) = statuses
.take_while(Result::is_ok)
.filter_map(Result::ok)
.filter_map(|item| item.summary())
.fold((0, 0, 0), |(added, deleted, modified), status| {
use gix::status::index_worktree::iter::Summary;
match status {
Summary::Removed => (added, deleted + 1, modified),
Summary::Added | Summary::Copied => (added + 1, deleted, modified),
Summary::Modified | Summary::TypeChange => (added, deleted, modified + 1),
Summary::Renamed => (added + 1, deleted + 1, modified),
Summary::IntentToAdd | Summary::Conflict => (added, deleted, modified),
}
});
if self.added > 0 {
result = format!("{result} {}+", self.added);
}

let mut result = String::new();
if modified > 0 {
result = format!("{modified}+-");
}
if self.deleted > 0 {
result = format!("{result} {}-", self.deleted);
}

if added > 0 {
result = format!("{result} {added}+");
write!(f, "{}", result.trim())
}

if deleted > 0 {
result = format!("{result} {deleted}-");
}

Ok(result.trim().into())
}

#[typetag::serialize]
impl InfoField for PendingInfo {
fn value(&self) -> String {
self.pending_changes.to_string()
self.to_string()
}

fn title(&self) -> String {
Expand All @@ -71,7 +79,9 @@ mod test {
#[test]
fn test_display_pending_info() {
let pending_info = PendingInfo {
pending_changes: "4+-".to_string(),
added: 0,
deleted: 0,
modified: 4,
};

assert_eq!(pending_info.value(), "4+-".to_string());
Expand Down
4 changes: 3 additions & 1 deletion tests/snapshots/repo__repo.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ expression: info
},
{
"PendingInfo": {
"pendingChanges": "1+- 2+"
"added": 2,
"deleted": 0,
"modified": 1
}
},
{
Expand Down

0 comments on commit 2e48f9e

Please sign in to comment.