[chg] trim whitespace at end of line and append correct newline
This commit is contained in:
parent
f4201621b7
commit
f53cbe8cfa
19
src/main.rs
19
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()?;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user