initial commit
This commit is contained in:
commit
63adf7d53e
4 changed files with 67 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "timer"
|
||||
version = "0.1.0"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "timer"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
51
src/main.rs
Normal file
51
src/main.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use std::io::Write;
|
||||
|
||||
const HOUR: u64 = 60 * 60;
|
||||
const MINUTE: u64 = 60;
|
||||
|
||||
fn main() {
|
||||
let Some(arg) = std::env::args().nth(1) else {
|
||||
eprintln!("Missing time argument");
|
||||
return;
|
||||
};
|
||||
let start = std::time::Instant::now();
|
||||
|
||||
let parts: Vec<&str> = arg.split(':').collect();
|
||||
let secs = match parts[..] {
|
||||
[s] => s.parse::<u64>().expect("parse seconds"),
|
||||
[m, s] => {
|
||||
s.parse::<u64>().expect("parse seconds")
|
||||
+ m.parse::<u64>().expect("parse minutes") * MINUTE
|
||||
}
|
||||
[h, m, s] => {
|
||||
s.parse::<u64>().expect("parse seconds")
|
||||
+ m.parse::<u64>().expect("parse minutes") * MINUTE
|
||||
+ h.parse::<u64>().expect("parse hours") * HOUR
|
||||
}
|
||||
_ => {
|
||||
eprintln!("Invalid time format: {arg}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let show_hours = secs > HOUR;
|
||||
let duration = std::time::Duration::from_secs(secs);
|
||||
let stop = start + duration;
|
||||
let mut now = std::time::Instant::now();
|
||||
|
||||
while now < stop {
|
||||
let rem = stop - now;
|
||||
let rems = rem.as_secs();
|
||||
let h = rems / HOUR;
|
||||
let m = (rems / MINUTE) % 60;
|
||||
let s = rems % 60;
|
||||
if show_hours {
|
||||
print!("\r{h:02}:{m:02}:{s:02}");
|
||||
} else {
|
||||
print!("\r{m:02}:{s:02}");
|
||||
}
|
||||
std::io::stdout().lock().flush().expect("stdout lock");
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
now = std::time::Instant::now();
|
||||
}
|
||||
println!();
|
||||
}
|
Loading…
Reference in a new issue