[chg] rewrite current line with timestamp prefix after newline

timestamp marks the time when the line was "sent" or "committed"
This commit is contained in:
ddidderr 2022-08-03 13:10:14 +02:00
parent 606d882ec1
commit 70754218dd
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14
4 changed files with 54 additions and 33 deletions

3
.cargo/config.toml Normal file
View File

@ -0,0 +1,3 @@
[build]
rustflags = ["-C", "target-cpu=native", "-C", "strip=symbols"]
target = "x86_64-unknown-linux-musl"

18
Cargo.lock generated
View File

@ -1,10 +1,12 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.0.1" version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]] [[package]]
name = "chrono" name = "chrono"
@ -21,9 +23,9 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.80" version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]] [[package]]
name = "logtimes" name = "logtimes"
@ -34,9 +36,9 @@ dependencies = [
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.44" version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db" checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"num-traits", "num-traits",
@ -44,9 +46,9 @@ dependencies = [
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.14" version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [ dependencies = [
"autocfg", "autocfg",
] ]

View File

@ -2,13 +2,13 @@
name = "logtimes" name = "logtimes"
version = "0.1.0" version = "0.1.0"
authors = ["Paul Schulze <p.schulze@avm.de>"] authors = ["Paul Schulze <p.schulze@avm.de>"]
edition = "2018" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
chrono = "0.4" chrono = "0.4"
[profile.release] [profile.release]
debug = false
lto = true lto = true
codegen-units = 1
debug = false
panic = "unwind"

View File

@ -1,49 +1,65 @@
use std::io::{Write, Read, ErrorKind, stdin, stdout};
use chrono::prelude::*; use chrono::prelude::*;
use std::io::{stdin, stdout, ErrorKind, Read, StdoutLock, Write};
#[inline(always)] #[inline(always)]
fn print_time() { fn print_time(output: &mut StdoutLock) -> Result<(), std::io::Error> {
let date_now = Local::now().format("%H:%M:%S%.9f"); let date_now = Local::now().format("%H:%M:%S%.9f");
let color_green = "\x1b\x5b\x30\x3b\x33\x32\x6d"; let color_green = "\x1b\x5b\x30\x3b\x33\x32\x6d";
let color_off = "\x1b\x5b\x30\x6d"; let color_off = "\x1b\x5b\x30\x6d";
print!("{}[{}]{} ", color_green, &date_now, color_off); write!(output, "{}[{}]{} ", color_green, &date_now, color_off)
} }
#[inline(always)]
fn print_delete_line(output: &mut StdoutLock) -> Result<(), std::io::Error> {
// tput dl1 und tput hpa 0
let bytes = "\x1b\x5b\x4d\x1b\x5b\x31\x47";
write!(output, "{}", bytes)
}
fn main() { fn run() -> Result<(), std::io::Error> {
let out = stdout(); let out = stdout();
let mut output = out.lock(); let mut output = out.lock();
let inp = stdin(); let inp = stdin();
let mut input = inp.lock(); let mut input = inp.lock();
let mut buf = [0; 1];
let mut linebuf = Vec::with_capacity(64 * 1024);
print_time();
loop { loop {
let mut buf = [0; 1]; if let Err(e) = input.read_exact(&mut buf) {
if e.kind() == ErrorKind::UnexpectedEof {
let _ = input.read_exact(&mut buf).map_err(|e| { return Err(e);
} else {
if e.kind() != ErrorKind::UnexpectedEof { output.flush()?;
println!("{}", e.to_string()); return Ok(());
std::process::exit(1);
} }
}
let _ = output.flush(); linebuf.push(buf[0]);
std::process::exit(0);
});
if buf[0] == 0xa { if buf[0] == 0xa {
print!("\n"); print_delete_line(&mut output)?;
print_time(); print_time(&mut output)?;
output.write_all(&linebuf)?;
output.flush()?;
linebuf.clear();
continue; continue;
} }
let _ = output.write_all(&mut buf); output.write_all(&buf)?;
let _ = output.flush(); output.flush()?;
}
}
fn main() {
if let Err(e) = run() {
println!("{:?}", e);
std::process::exit(1);
} }
} }