Skip to content

Commit 28cab16

Browse files
authored
Merge pull request #166 from axdiamond/show-pending-changes
Add changes line
2 parents 90bdc4e + 4a3c136 commit 28cab16

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/info.rs

+59
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub struct Info {
2727
last_change: String,
2828
repo: String,
2929
commits: String,
30+
pending: String,
3031
repo_size: String,
3132
number_of_lines: usize,
3233
license: String,
@@ -86,6 +87,14 @@ impl std::fmt::Display for Info {
8687
)?;
8788
}
8889

90+
if !self.disable_fields.pending && self.pending != "" {
91+
write_buf(
92+
&mut buf,
93+
&self.get_formatted_info_label("Pending: ", color),
94+
&self.pending,
95+
)?;
96+
}
97+
8998
if !self.disable_fields.version {
9099
write_buf(
91100
&mut buf,
@@ -304,6 +313,7 @@ impl Info {
304313
let (git_v, git_user) = Info::get_git_info(workdir_str);
305314
let version = Info::get_version(workdir_str)?;
306315
let commits = Info::get_commits(workdir_str, no_merges)?;
316+
let pending = Info::get_pending_pending(workdir_str)?;
307317
let repo_size = Info::get_packed_size(workdir_str)?;
308318
let last_change = Info::get_last_change(workdir_str)?;
309319
let creation_date = Info::get_creation_time(workdir_str)?;
@@ -324,6 +334,7 @@ impl Info {
324334
last_change,
325335
repo: config.repository_url,
326336
commits,
337+
pending,
327338
repo_size,
328339
number_of_lines,
329340
license: project_license,
@@ -502,6 +513,54 @@ impl Info {
502513
}
503514
}
504515

516+
fn get_pending_pending(dir: &str) -> Result<String> {
517+
let output = Command::new("git")
518+
.arg("-C")
519+
.arg(dir)
520+
.arg("status")
521+
.arg("--porcelain")
522+
.output()
523+
.expect("Failed to execute git.");
524+
525+
let output = String::from_utf8_lossy(&output.stdout);
526+
527+
if output == "" {
528+
Ok("".into())
529+
} else {
530+
let lines = output.lines();
531+
532+
let mut deleted = 0;
533+
let mut added = 0;
534+
let mut modified = 0;
535+
536+
for line in lines {
537+
let prefix = &line[..2];
538+
539+
match prefix.trim() {
540+
"D" => deleted += 1,
541+
"A" | "AM" | "??" => added += 1,
542+
"M" | "MM" | "R" => modified += 1,
543+
_ => {}
544+
}
545+
}
546+
547+
let mut result = String::from("");
548+
if modified > 0 {
549+
result = format!("{}+-", modified)
550+
}
551+
552+
if added > 0 {
553+
result = format!("{} {}+", result, added);
554+
}
555+
556+
if deleted > 0 {
557+
result = format!("{} {}-", result, deleted);
558+
}
559+
560+
Ok(result.into())
561+
}
562+
}
563+
505564
fn get_packed_size(dir: &str) -> Result<String> {
506565
let output = Command::new("git")
507566
.arg("-C")

src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub struct InfoFieldOn {
5757
last_change: bool,
5858
repo: bool,
5959
commits: bool,
60+
pending: bool,
6061
lines_of_code: bool,
6162
size: bool,
6263
license: bool,
@@ -75,6 +76,7 @@ enum InfoFields {
7576
LastChange,
7677
Repo,
7778
Commits,
79+
Pending,
7880
LinesOfCode,
7981
Size,
8082
License,
@@ -258,6 +260,7 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]",
258260
InfoFields::Authors => disable_fields.authors = true,
259261
InfoFields::LastChange => disable_fields.last_change = true,
260262
InfoFields::Repo => disable_fields.repo = true,
263+
InfoFields::Pending => disable_fields.pending = true,
261264
InfoFields::Commits => disable_fields.commits = true,
262265
InfoFields::LinesOfCode => disable_fields.lines_of_code = true,
263266
InfoFields::Size => disable_fields.size = true,

0 commit comments

Comments
 (0)