Remove newline at the end of stream lines

Bug introduced by !24 which kept trailing `\n` and fed it to filters.
Thus regexes ending with `$` couldn't match anymore.

Fixes #128
This commit is contained in:
ppom 2025-07-07 12:00:00 +02:00
commit bba113b6ab
No known key found for this signature in database
3 changed files with 50 additions and 9 deletions

View file

@ -23,11 +23,10 @@ use crate::{
use super::shutdown::ShutdownToken;
/** Converts bytes to string, discarding invalid utf8 sequences
*/
fn to_string(data: &[u8]) -> String {
let res = String::from_utf8_lossy(data);
res.to_string()
/// Converts bytes to line string, discarding invalid utf8 sequences and newlines at the end
fn to_line(data: &[u8]) -> String {
String::from_utf8_lossy(data)
.trim_end_matches('\n')
.replace(std::char::REPLACEMENT_CHARACTER, "")
}
@ -46,7 +45,7 @@ fn lines_to_stream<T: tokio::io::AsyncRead + Unpin>(
}
// Try to read until LF or EOF. If interrupted, buffer might contain data, in which case
// new data will be happened to it
let nl = lines.read_until(0x0a, &mut buffer);
let nl = lines.read_until(b'\n', &mut buffer);
pin!(nl);
match futures::Future::poll(nl, cx) {
Poll::Pending => Poll::Pending,
@ -57,13 +56,13 @@ fn lines_to_stream<T: tokio::io::AsyncRead + Unpin>(
} else {
// reached eof with data in the buffer
at_eof = true;
let line = to_string(&buffer);
let line = to_line(&buffer);
buffer.clear();
Poll::Ready(Some(Ok(line)))
}
}
Poll::Ready(Ok(_)) => {
let line = to_string(&buffer);
let line = to_line(&buffer);
buffer.clear();
Poll::Ready(Some(Ok(line)))
}

View file

@ -0,0 +1,42 @@
{
patterns: {
num: {
regex: '[0-9]+',
ignore: ['1'],
// ignoreregex: ['2.?'],
},
},
start: [
['echo', 'coucou'],
],
stop: [
['echo', 'byebye'],
],
streams: {
s1: {
cmd: ['sh', '-c', 'seq 20 | while read i; do echo found $((i % 5)); sleep 1; done'],
filters: {
f1: {
regex: [
'^found [0-9]+$',
],
retry: 4,
retryperiod: '60s',
actions: {
damn: {
cmd: ['notify-send', 'first stream', 'found!'],
},
undamn: {
cmd: ['notify-send', 'first stream', 'unfound'],
after: '3s',
onexit: true,
},
},
},
},
},
},
}

View file

@ -22,7 +22,7 @@
streams: {
s1: {
cmd: ['sh', '-c', "seq 20 | tr ' ' '\n' | while read i; do echo found $((i % 5)); sleep 1; done"],
cmd: ['sh', '-c', 'seq 20 | while read i; do echo found $((i % 5)); sleep 1; done'],
filters: {
f1: {
regex: [