Compare commits
No commits in common. "f949fc5474ca3f72a1782bad49616c556fae327a" and "3cc1b6743c956adc59a2f65f297268e16d7471d5" have entirely different histories.
f949fc5474
...
3cc1b6743c
2 changed files with 36 additions and 25 deletions
29
src/lib.rs
29
src/lib.rs
|
@ -39,7 +39,7 @@ pub fn skip(cli: &Cli, writer: &mut impl Write) -> Result<()> {
|
|||
None => Box::new(BufReader::new(std::io::stdin())),
|
||||
};
|
||||
if let Some(line) = &cli.line {
|
||||
skip_lines_matching(cli, reader, writer, line)
|
||||
skip_lines_matching(&cli, reader, writer, line)
|
||||
} else if let Some(ref token) = cli.token {
|
||||
skip_tokens(cli, reader, writer, token)
|
||||
} else {
|
||||
|
@ -49,10 +49,14 @@ pub fn skip(cli: &Cli, writer: &mut impl Write) -> Result<()> {
|
|||
|
||||
// skip a number of lines
|
||||
fn skip_lines(cli: &Cli, reader: Box<dyn BufRead>, writer: &mut impl Write) -> Result<()> {
|
||||
for (counter, current_line) in reader.lines().map_while(Option::Some).flatten().enumerate() {
|
||||
let mut counter = 0usize;
|
||||
for current_line in reader.lines() {
|
||||
if let Ok(current_line) = current_line {
|
||||
if counter >= cli.lines {
|
||||
writeln!(writer, "{}", current_line)?;
|
||||
}
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
@ -65,7 +69,8 @@ fn skip_lines_matching(
|
|||
line: &str,
|
||||
) -> Result<()> {
|
||||
let mut counter = 0usize;
|
||||
for current_line in reader.lines().map_while(Option::Some).flatten() {
|
||||
for current_line in reader.lines() {
|
||||
if let Ok(current_line) = current_line {
|
||||
if counter >= cli.lines {
|
||||
writeln!(writer, "{}", current_line)?;
|
||||
}
|
||||
|
@ -73,6 +78,7 @@ fn skip_lines_matching(
|
|||
counter += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -86,11 +92,12 @@ fn skip_tokens(
|
|||
) -> Result<()> {
|
||||
let mut counter = 0usize;
|
||||
|
||||
for current_line in reader.lines().map_while(Option::Some).flatten() {
|
||||
for current_line in reader.lines() {
|
||||
if let Ok(current_line) = current_line {
|
||||
if counter >= cli.lines {
|
||||
writeln!(writer, "{}", current_line)?;
|
||||
}
|
||||
if current_line.contains(token) {
|
||||
if current_line.contains(&token) {
|
||||
if cli.ignore_extras {
|
||||
counter += 1;
|
||||
} else {
|
||||
|
@ -99,6 +106,7 @@ fn skip_tokens(
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -143,7 +151,10 @@ mod tests {
|
|||
skip(&cli, &mut lines)?;
|
||||
|
||||
//then
|
||||
assert_eq!(String::from_utf8(lines)?, ["alpha", "gamma\n"].join("\n"));
|
||||
assert_eq!(
|
||||
String::from_utf8(lines)?,
|
||||
vec!["alpha", "gamma\n"].join("\n")
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -185,7 +196,7 @@ mod tests {
|
|||
//then
|
||||
assert_eq!(
|
||||
String::from_utf8(lines)?,
|
||||
[
|
||||
vec![
|
||||
"Or help one fainting robin",
|
||||
"Unto his nest again,",
|
||||
"I shall not live in vain.\n"
|
||||
|
@ -213,7 +224,7 @@ mod tests {
|
|||
//then
|
||||
assert_eq!(
|
||||
String::from_utf8(lines)?,
|
||||
[
|
||||
vec![
|
||||
//Lorem ipsum dolor sit amet, -- +2 = 2
|
||||
//consectetur adipiscing elit,
|
||||
//sed do eiusmod tempor incididunt -- +1 = 3
|
||||
|
@ -246,7 +257,7 @@ mod tests {
|
|||
//then
|
||||
assert_eq!(
|
||||
String::from_utf8(lines)?,
|
||||
[
|
||||
vec![
|
||||
//Lorem ipsum dolor sit amet, -- 1
|
||||
//consectetur adipiscing elit,
|
||||
//sed do eiusmod tempor incididunt -- 2
|
||||
|
|
2
test.sh
2
test.sh
|
@ -6,7 +6,7 @@ SKIP="./target/debug/skip"
|
|||
DIFF="diff -u --color"
|
||||
|
||||
if test ! -x $SKIP ; then
|
||||
echo "File missing: $SKIP - try 'cargo build'"
|
||||
echo "File missing: $SKIP - try 'zig build'"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
|
Loading…
Reference in a new issue