Skip to content

Commit 30b5d01

Browse files
committed
Add changes line
1 parent 90bdc4e commit 30b5d01

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/info.rs

+40
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+
changes: String,
3031
repo_size: String,
3132
number_of_lines: usize,
3233
license: String,
@@ -191,6 +192,14 @@ impl std::fmt::Display for Info {
191192
)?;
192193
}
193194

195+
if !self.disable_fields.changes && self.changes != "" {
196+
write_buf(
197+
&mut buf,
198+
&self.get_formatted_info_label("Changes: ", color),
199+
&self.changes,
200+
)?;
201+
}
202+
194203
if !self.disable_fields.lines_of_code {
195204
write_buf(
196205
&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 changes = Info::get_pending_changes(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+
changes,
327338
repo_size,
328339
number_of_lines,
329340
license: project_license,
@@ -502,6 +513,35 @@ impl Info {
502513
}
503514
}
504515

516+
fn get_pending_changes(dir: &str) -> Result<String> {
517+
let output = Command::new("git")
518+
.arg("-C")
519+
.arg(dir)
520+
.arg("diff")
521+
.arg("--shortstat")
522+
.arg("HEAD")
523+
.output()
524+
.expect("Failed to execute git.");
525+
526+
let output = String::from_utf8_lossy(&output.stdout);
527+
528+
if output == "" {
529+
Ok("".into())
530+
} else {
531+
let result = String::from(output)
532+
.replace(",", &"")
533+
.replace("\n", &"")
534+
.replace(" files changed", &"+-")
535+
.replace(" file changed", &"+-")
536+
.replace(" insertions(+)", &"+")
537+
.replace(" insertion(+)", &"+")
538+
.replace(" deletions(-)", &"-")
539+
.replace(" deletion(-)", &"-");
540+
541+
Ok(result.trim().into())
542+
}
543+
}
544+
505545
fn get_packed_size(dir: &str) -> Result<String> {
506546
let output = Command::new("git")
507547
.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+
changes: 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+
Changes,
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::Changes => disable_fields.changes = 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)