From f53cbe8cfa24258dc1e2830ae55cb538eda3abef Mon Sep 17 00:00:00 2001
From: Paul Schulze
Date: Wed, 5 Apr 2023 17:51:06 +0200
Subject: [PATCH] [chg] trim whitespace at end of line and append correct
newline
---
src/main.rs | 19 +++++++++++++++++--
1 file changed, 17 insertions(+), 2 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index 47cbf56..9e05083 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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()?;
};