Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display modification times of input files in context diff
Browse files Browse the repository at this point in the history
Fixes #31
TanmayPatil105 authored and sylvestre committed Mar 30, 2024
1 parent 14e7754 commit 0941828
Showing 3 changed files with 175 additions and 1 deletion.
155 changes: 155 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ name = "diffutils"
path = "src/main.rs"

[dependencies]
chrono = "0.4.35"
diff = "0.1.10"
regex = "1.10.3"
same-file = "1.0.6"
20 changes: 19 additions & 1 deletion src/context_diff.rs
Original file line number Diff line number Diff line change
@@ -264,6 +264,22 @@ fn make_diff(
results
}

fn get_modification_time(file_path: &str) -> String {
use chrono::{DateTime, Local};
use std::fs;

let metadata = fs::metadata(file_path).expect("Failed to get metadata");
let modification_time = metadata
.modified()
.expect("Failed to get modification time");
let modification_time: DateTime<Local> = modification_time.into();
let modification_time: String = modification_time
.format("%Y-%m-%d %H:%M:%S%.9f %z")
.to_string();

modification_time
}

#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn diff(
@@ -276,7 +292,9 @@ pub fn diff(
expand_tabs: bool,
tabsize: usize,
) -> Vec<u8> {
let mut output = format!("*** {expected_filename}\t\n--- {actual_filename}\t\n").into_bytes();
let expected_file_modified_time = get_modification_time(expected_filename);
let actual_file_modified_time = get_modification_time(actual_filename);
let mut output = format!("*** {expected_filename}\t{expected_file_modified_time}\n--- {actual_filename}\t{actual_file_modified_time}\n").into_bytes();
let diff_results = make_diff(expected, actual, context_size, stop_early);
if diff_results.is_empty() {
return Vec::new();

0 comments on commit 0941828

Please sign in to comment.