[chg] trim whitespace at end of line and append correct newline

This commit is contained in:
Paul Schulze 2023-04-05 17:51:06 +02:00 committed by ddidderr
parent f4201621b7
commit f53cbe8cfa
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14

View File

@ -49,6 +49,17 @@ where
write!(output, "{bytes}")
}
fn trim_end(line: &[u8]) -> usize {
let mut end = line.len();
for ch in line.iter().rev() {
if *ch != b'\r' && *ch != b'\n' && *ch != b'\t' {
break;
}
end -= 1;
}
end
}
fn run() -> LogtimesResult {
let mut log_file = args().nth(1).map(|f| {
File::options()
@ -84,12 +95,16 @@ fn run() -> LogtimesResult {
if buf[0] == 0xa {
print_delete_line(&mut out)?;
print_time_color(&mut out)?;
out.write_all(&linebuf)?;
let end = trim_end(&linebuf);
out.write_all(&linebuf[..end])?;
out.write_all(&[b'\r', b'\n'])?;
out.flush()?;
if let Some(ref mut f) = log_file {
print_time(f)?;
f.write_all(&linebuf)?;
f.write_all(&linebuf[..end])?;
f.write_all(&[b'\n'])?;
f.flush()?;
};