Skip to content

Commit abed3ad

Browse files
authored
Merge pull request #429 from Bluemangoo/feature/vmstat-timestamp
vmstat: implement `--timestamp`
2 parents fa83319 + f3860a7 commit abed3ad

5 files changed

Lines changed: 73 additions & 4 deletions

File tree

Cargo.lock

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/vmstat/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ version.workspace = true
1212

1313
[dependencies]
1414
bytesize = { workspace = true }
15+
chrono = { workspace = true, default-features = false, features = ["clock"] }
1516
clap = { workspace = true }
1617
terminal_size = { workspace = true }
17-
uucore = { workspace = true }
18+
uucore = { workspace = true, features = ["custom-tz-fmt"] }
1819

1920
[lib]
2021
path = "src/vmstat.rs"

src/uu/vmstat/src/picker.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub type Picker = (
1717
#[cfg(target_os = "linux")]
1818
pub fn get_pickers(matches: &ArgMatches) -> Vec<Picker> {
1919
let wide = matches.get_flag("wide");
20-
vec![
20+
let mut pickers = vec![
2121
concat_helper(
2222
if wide {
2323
("--procs--".into(), " r b".into())
@@ -62,7 +62,18 @@ pub fn get_pickers(matches: &ArgMatches) -> Vec<Picker> {
6262
},
6363
get_cpu_info,
6464
),
65-
]
65+
];
66+
if matches.get_flag("timestamp") {
67+
pickers.push(concat_helper(
68+
(
69+
"-----timestamp-----".into(),
70+
format!("{:>19}", uucore::custom_tz_fmt::custom_time_format("%Z")),
71+
),
72+
get_timestamp,
73+
));
74+
}
75+
76+
pickers
6677
}
6778

6879
#[cfg(target_os = "linux")]
@@ -267,3 +278,15 @@ fn get_cpu_info(
267278
(len, format!("{:.0}", cpu_load.guest)),
268279
]
269280
}
281+
282+
#[cfg(target_os = "linux")]
283+
fn get_timestamp(
284+
_proc_data: &ProcData,
285+
_proc_data_before: Option<&ProcData>,
286+
_matches: &ArgMatches,
287+
) -> Vec<(usize, String)> {
288+
vec![(
289+
10,
290+
chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(),
291+
)]
292+
}

src/uu/vmstat/src/vmstat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub fn uu_app() -> Command {
133133
// arg!(-D --"disk-sum" "Report some summary statistics about disk activity"),
134134
// arg!(-p --partition <device> "Detailed statistics about partition"),
135135
arg!(-S --unit <character> "Switches outputs between 1000 (k), 1024 (K), 1000000 (m), or 1048576 (M) bytes"),
136-
// arg!(-t --timestamp "Append timestamp to each line"),
136+
arg!(-t --timestamp "Append timestamp to each line"),
137137
arg!(-w --wide "Wide output mode"),
138138
arg!(-y --"no-first" "Omits first report with statistics since system boot"),
139139
])

tests/by-util/test_vmstat.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,15 @@ fn test_active() {
6969
.unwrap()
7070
.contains("active"));
7171
}
72+
73+
#[test]
74+
#[cfg(target_os = "linux")]
75+
fn test_timestamp() {
76+
let result = new_ucmd!().arg("-t").succeeds();
77+
assert!(result
78+
.stdout_str()
79+
.lines()
80+
.next()
81+
.unwrap()
82+
.contains("timestamp"));
83+
}

0 commit comments

Comments
 (0)