[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:
58
src/main.rs
58
src/main.rs
@ -1,49 +1,65 @@
|
||||
use std::io::{Write, Read, ErrorKind, stdin, stdout};
|
||||
use chrono::prelude::*;
|
||||
|
||||
use std::io::{stdin, stdout, ErrorKind, Read, StdoutLock, Write};
|
||||
|
||||
#[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 color_green = "\x1b\x5b\x30\x3b\x33\x32\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 mut output = out.lock();
|
||||
|
||||
let inp = stdin();
|
||||
let mut input = inp.lock();
|
||||
|
||||
let mut buf = [0; 1];
|
||||
|
||||
let mut linebuf = Vec::with_capacity(64 * 1024);
|
||||
|
||||
print_time();
|
||||
loop {
|
||||
let mut buf = [0; 1];
|
||||
|
||||
let _ = input.read_exact(&mut buf).map_err(|e| {
|
||||
|
||||
if e.kind() != ErrorKind::UnexpectedEof {
|
||||
println!("{}", e.to_string());
|
||||
std::process::exit(1);
|
||||
if let Err(e) = input.read_exact(&mut buf) {
|
||||
if e.kind() == ErrorKind::UnexpectedEof {
|
||||
return Err(e);
|
||||
} else {
|
||||
output.flush()?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
let _ = output.flush();
|
||||
std::process::exit(0);
|
||||
});
|
||||
|
||||
linebuf.push(buf[0]);
|
||||
|
||||
if buf[0] == 0xa {
|
||||
print!("\n");
|
||||
print_time();
|
||||
print_delete_line(&mut output)?;
|
||||
print_time(&mut output)?;
|
||||
output.write_all(&linebuf)?;
|
||||
|
||||
output.flush()?;
|
||||
|
||||
linebuf.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
let _ = output.write_all(&mut buf);
|
||||
let _ = output.flush();
|
||||
output.write_all(&buf)?;
|
||||
output.flush()?;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if let Err(e) = run() {
|
||||
println!("{:?}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user