Compare commits

..

No commits in common. "main" and "v0.2.0" have entirely different histories.
main ... v0.2.0

9 changed files with 272 additions and 272 deletions

View file

@ -1,52 +0,0 @@
name: Rust
on:
push:
branches: ["next"]
pull_request:
branches: ["main"]
env:
CARGO_TERM_COLOR: always
jobs:
build:
runs-on: docker
strategy:
matrix:
toolchain:
- name: stable
- name: nightly
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Format
uses: https://git.kemitix.net/kemitix/rust@v2.1.0
with:
args: ${{ matrix.toolchain.name }} cargo fmt --all -- --check
# - name: Machete
# uses: https://github.com/bnjbvr/cargo-machete@v0.6.2
- name: Clippy
uses: https://git.kemitix.net/kemitix/rust@v2.1.0
with:
args: ${{ matrix.toolchain.name }} cargo clippy
- name: Build
uses: https://git.kemitix.net/kemitix/rust@v2.1.0
with:
args: ${{ matrix.toolchain.name }} cargo build
- name: Test
uses: https://git.kemitix.net/kemitix/rust@v2.1.0
with:
args: ${{ matrix.toolchain.name }} cargo test --no-fail-fast
- name: Integration
uses: https://git.kemitix.net/kemitix/rust@v2.1.0
with:
args: ./test.sh

View file

@ -1,46 +0,0 @@
variables:
- &rust_image "docker.io/rust:1.81.0"
- &slow_check_paths
- path:
# rust source code
- "crates/**"
- "src/**"
- "tests/**"
- "**/Cargo.toml"
- "Cargo.lock"
# database migrations
- "migrations/**"
# config files and scripts used by ci
- ".woodpecker.yml"
steps:
toml_fmt:
image: docker.io/tamasfe/taplo:0.9.3
commands:
- taplo format --check
cargo_machete:
image: *rust_image
commands:
- wget https://github.com/cargo-bins/cargo-binstall/releases/latest/download/cargo-binstall-x86_64-unknown-linux-musl.tgz
- tar -xvf cargo-binstall-x86_64-unknown-linux-musl.tgz
- rm cargo-binstall-x86_64-unknown-linux-musl.tgz
- mv cargo-binstall /usr/local/cargo/bin
- cargo binstall -y cargo-machete
- cargo machete
ignored_files:
image: docker.io/alpine:latest
commands:
- apk add git
- IGNORED=$(git ls-files --cached -i --exclude-standard)
- if [[ "$IGNORED" ]]; then echo "Ignored files present:\n$IGNORED\n"; exit 1; fi
publish_to_crates_io:
image: *rust_image
commands:
- cargo login "$CARGO_REGISTRY_TOKEN"
- cargo publish --registry crates-io --no-verify
secrets: [cargo_registry_token]
when:
event: tag

289
Cargo.lock generated
View file

@ -3,82 +3,46 @@
version = 3 version = 3
[[package]] [[package]]
name = "anstream" name = "bitflags"
version = "0.6.11" version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]] [[package]]
name = "anstyle" name = "bitflags"
version = "1.0.4" version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" checksum = "487f1e0fcbe47deb8b0574e646def1c903389d95241dd1bbcc6ce4a715dfc0c1"
[[package]] [[package]]
name = "anstyle-parse" name = "cc"
version = "0.2.3" version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c" checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]] [[package]]
name = "clap" name = "clap"
version = "4.5.0" version = "4.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80c21025abd42669a92efc996ef13cfb2c5c627858421ea58d5c3b331a6c134f" checksum = "42dfd32784433290c51d92c438bb72ea5063797fc3cc9a21a8c4346bebbb2098"
dependencies = [ dependencies = [
"clap_builder", "bitflags 2.0.2",
"clap_derive", "clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "458bf1f341769dfcf849846f65dffdf9146daa56bcd2a47cb4e1de9915567c99"
dependencies = [
"anstream",
"anstyle",
"clap_lex", "clap_lex",
"is-terminal",
"once_cell",
"strsim", "strsim",
"termcolor",
] ]
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "4.5.0" version = "4.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47" checksum = "fddf67631444a3a3e3e5ac51c36a5e01335302de677bd78759eaa90ab1f46644"
dependencies = [ dependencies = [
"heck", "heck",
"proc-macro-error",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn",
@ -86,15 +50,33 @@ dependencies = [
[[package]] [[package]]
name = "clap_lex" name = "clap_lex"
version = "0.7.0" version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" checksum = "033f6b7a4acb1f358c742aaca805c939ee73b4c6209ae4318ec7aca81c42e646"
dependencies = [
"os_str_bytes",
]
[[package]] [[package]]
name = "colorchoice" name = "errno"
version = "1.0.0" version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1"
dependencies = [
"errno-dragonfly",
"libc",
"winapi",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]] [[package]]
name = "heck" name = "heck"
@ -103,10 +85,87 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]] [[package]]
name = "proc-macro2" name = "hermit-abi"
version = "1.0.78" version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" checksum = "fed44880c466736ef9a5c5b5facefb5ed0785676d0c02d612db14e54f0d84286"
[[package]]
name = "io-lifetimes"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dd6da19f25979c7270e70fa95ab371ec3b701cd0eefc47667a09785b3c59155"
dependencies = [
"hermit-abi",
"libc",
"windows-sys",
]
[[package]]
name = "is-terminal"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8687c819457e979cc940d09cb16e42a1bf70aa6b60a549de6d3a62a0ee90c69e"
dependencies = [
"hermit-abi",
"io-lifetimes",
"rustix",
"windows-sys",
]
[[package]]
name = "libc"
version = "0.2.140"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99227334921fae1a979cf0bfdfcc6b3e5ce376ef57e16fb6fb3ea2ed6095f80c"
[[package]]
name = "linux-raw-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4"
[[package]]
name = "once_cell"
version = "1.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3"
[[package]]
name = "os_str_bytes"
version = "6.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ceedf44fb00f2d1984b0bc98102627ce622e083e49a5bacdb3e514fa4238e267"
[[package]]
name = "proc-macro-error"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn",
"version_check",
]
[[package]]
name = "proc-macro-error-attr"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2",
"quote",
"version_check",
]
[[package]]
name = "proc-macro2"
version = "1.0.52"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d0e1ae9e836cc3beddd63db0df682593d7e2d3d891ae8c9083d2113e1744224"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -120,30 +179,53 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rustix"
version = "0.36.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fe885c3a125aa45213b68cc1472a49880cb5923dc23f522ad2791b882228778"
dependencies = [
"bitflags 1.3.2",
"errno",
"io-lifetimes",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]] [[package]]
name = "skip" name = "skip"
version = "0.2.1" version = "0.2.0"
dependencies = [ dependencies = [
"clap", "clap",
] ]
[[package]] [[package]]
name = "strsim" name = "strsim"
version = "0.11.0" version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.12" version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "termcolor"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.8" version = "1.0.8"
@ -151,25 +233,56 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
[[package]] [[package]]
name = "utf8parse" name = "version_check"
version = "0.2.1" version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.52.0" version = "0.45.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
dependencies = [ dependencies = [
"windows-targets", "windows-targets",
] ]
[[package]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.52.0" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd" checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
dependencies = [ dependencies = [
"windows_aarch64_gnullvm", "windows_aarch64_gnullvm",
"windows_aarch64_msvc", "windows_aarch64_msvc",
@ -182,42 +295,42 @@ dependencies = [
[[package]] [[package]]
name = "windows_aarch64_gnullvm" name = "windows_aarch64_gnullvm"
version = "0.52.0" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
[[package]] [[package]]
name = "windows_aarch64_msvc" name = "windows_aarch64_msvc"
version = "0.52.0" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
[[package]] [[package]]
name = "windows_i686_gnu" name = "windows_i686_gnu"
version = "0.52.0" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
[[package]] [[package]]
name = "windows_i686_msvc" name = "windows_i686_msvc"
version = "0.52.0" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
[[package]] [[package]]
name = "windows_x86_64_gnu" name = "windows_x86_64_gnu"
version = "0.52.0" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
[[package]] [[package]]
name = "windows_x86_64_gnullvm" name = "windows_x86_64_gnullvm"
version = "0.52.0" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
[[package]] [[package]]
name = "windows_x86_64_msvc" name = "windows_x86_64_msvc"
version = "0.52.0" version = "0.42.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"

View file

@ -1,17 +1,9 @@
[package] [package]
name = "skip" name = "skip"
version = "0.2.1" version = "0.2.0"
edition = "2021" edition = "2021"
authors = ["Paul Campbell <pcampbell@kemitix.net>"]
categories = ["command-line-utilities"]
description = "Skip lines in a file"
license = "MIT"
repository = "https://git.kemitix.net/kemitix/skip"
keywords = ["skip", "lines", "file", "text", "utility"]
rust-version = "1.74.1"
exclude = [".cargo_home"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
clap = { version = "4.5", features = ["derive"] } clap = { version = "4.0", features = ["derive"] }

View file

@ -2,17 +2,13 @@
Skip part of a file. Skip part of a file.
As `head` will show the top of a file up-to a number of line, As `head` will show the top of a file after a number of line,
so `skip` will do the opposite, and not show the top of the file, so `skip` will do the opposite, and not show the top of the file,
but will show the rest. but will show the rest.
Additionally, it can check for whole lines matching, Additionally, it can check for whole lines matching,
or for a token being present on the line. or for a token being present on the line.
N.B.: The `skip` crate used to be an implementation of [Skip list](https://en.wikipedia.org/wiki/Skip_list),
by [Luo Jia / Zhouqi Jiang](https://github.com/luojia65) ([source](https://github.com/luojia65/skip)).
That crate will be republished as [skip-list](https://crates.io/crates/skip-list) (soon).
## Usage ## Usage
### Skip a fixed number of lines ### Skip a fixed number of lines

View file

@ -1,15 +1,15 @@
dist: target-release dist: target-release-skip
if test ! -d dist ; then mkdir dist ; fi if test ! -d dist ; then mkdir dist ; fi
cp target/release/skip dist/ cp target/release/skip dist/
target-release: unittest inttest inttest: target-debug-skip
cargo build --release
inttest: target-debug
./test.sh ./test.sh
unittest: target-debug target-release-skip: unittest inttest
cargo test cargo build --release
target-debug: target-debug-skip:
cargo build cargo build
unittest: target-debug-skip
cargo test

View file

@ -1,10 +0,0 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["config:recommended"],
"packageRules": [
{
"matchManagers": ["cargo"],
"rangeStrategy": "replace"
}
]
}

View file

@ -39,7 +39,7 @@ pub fn skip(cli: &Cli, writer: &mut impl Write) -> Result<()> {
None => Box::new(BufReader::new(std::io::stdin())), None => Box::new(BufReader::new(std::io::stdin())),
}; };
if let Some(line) = &cli.line { 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 { } else if let Some(ref token) = cli.token {
skip_tokens(cli, reader, writer, token) skip_tokens(cli, reader, writer, token)
} else { } else {
@ -49,10 +49,14 @@ pub fn skip(cli: &Cli, writer: &mut impl Write) -> Result<()> {
// skip a number of lines // skip a number of lines
fn skip_lines(cli: &Cli, reader: Box<dyn BufRead>, writer: &mut impl Write) -> Result<()> { 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 { if counter >= cli.lines {
writeln!(writer, "{}", current_line)?; writeln!(writer, "{}", current_line)?;
} }
counter += 1;
}
} }
Ok(()) Ok(())
} }
@ -65,7 +69,8 @@ fn skip_lines_matching(
line: &str, line: &str,
) -> Result<()> { ) -> Result<()> {
let mut counter = 0usize; 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 { if counter >= cli.lines {
writeln!(writer, "{}", current_line)?; writeln!(writer, "{}", current_line)?;
} }
@ -73,6 +78,7 @@ fn skip_lines_matching(
counter += 1; counter += 1;
} }
} }
}
Ok(()) Ok(())
} }
@ -86,11 +92,12 @@ fn skip_tokens(
) -> Result<()> { ) -> Result<()> {
let mut counter = 0usize; 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 { if counter >= cli.lines {
writeln!(writer, "{}", current_line)?; writeln!(writer, "{}", current_line)?;
} }
if current_line.contains(token) { if current_line.contains(&token) {
if cli.ignore_extras { if cli.ignore_extras {
counter += 1; counter += 1;
} else { } else {
@ -99,6 +106,7 @@ fn skip_tokens(
} }
} }
} }
}
Ok(()) Ok(())
} }
@ -143,7 +151,10 @@ mod tests {
skip(&cli, &mut lines)?; skip(&cli, &mut lines)?;
//then //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(()) Ok(())
} }
@ -185,7 +196,7 @@ mod tests {
//then //then
assert_eq!( assert_eq!(
String::from_utf8(lines)?, String::from_utf8(lines)?,
[ vec![
"Or help one fainting robin", "Or help one fainting robin",
"Unto his nest again,", "Unto his nest again,",
"I shall not live in vain.\n" "I shall not live in vain.\n"
@ -213,7 +224,7 @@ mod tests {
//then //then
assert_eq!( assert_eq!(
String::from_utf8(lines)?, String::from_utf8(lines)?,
[ vec![
//Lorem ipsum dolor sit amet, -- +2 = 2 //Lorem ipsum dolor sit amet, -- +2 = 2
//consectetur adipiscing elit, //consectetur adipiscing elit,
//sed do eiusmod tempor incididunt -- +1 = 3 //sed do eiusmod tempor incididunt -- +1 = 3
@ -246,7 +257,7 @@ mod tests {
//then //then
assert_eq!( assert_eq!(
String::from_utf8(lines)?, String::from_utf8(lines)?,
[ vec![
//Lorem ipsum dolor sit amet, -- 1 //Lorem ipsum dolor sit amet, -- 1
//consectetur adipiscing elit, //consectetur adipiscing elit,
//sed do eiusmod tempor incididunt -- 2 //sed do eiusmod tempor incididunt -- 2

52
test.sh
View file

@ -2,41 +2,37 @@
set -e set -e
echo "PWD: $PWD"
ls -l
ls -l target
SKIP="./target/debug/skip" SKIP="./target/debug/skip"
DIFF="diff -u --color" DIFF="diff -u --color"
if test ! -x $SKIP; then if test ! -x $SKIP ; then
echo "File missing: $SKIP - try 'cargo build'" echo "File missing: $SKIP - try 'zig build'"
exit 1 exit 1
fi fi
echo "> skip a line when reading from stdin" echo "> skip a line when reading from stdin"
INPUT=$( INPUT=$(cat<<EOF
cat <<EOF
line 1 line 1
line 2 line 2
EOF EOF
) )
echo "line 2" >test.expect echo "line 2" > test.expect
echo "$INPUT" | $SKIP 1 >test.out echo "$INPUT" | $SKIP 1 > test.out
$DIFF test.expect test.out $DIFF test.expect test.out
rm test.expect test.out rm test.expect test.out
echo "> skip a line when reading from a file" echo "> skip a line when reading from a file"
cat <<EOF >test.in cat<<EOF > test.in
line 1 line 1
line 2 line 2
EOF EOF
echo "line 2" >test.expect echo "line 2" > test.expect
$SKIP 1 test.in >test.out $SKIP 1 test.in > test.out
$DIFF test.expect test.out $DIFF test.expect test.out
rm test.expect test.out rm test.expect test.out
echo "> skip until 2 matching lines seen" echo "> skip until 2 matching lines seen"
cat <<EOF >test.in cat<<EOF > test.in
alpha alpha
beta beta
alpha alpha
@ -44,17 +40,17 @@ alpha
gamma gamma
alpha alpha
EOF EOF
cat <<EOF >test.expect cat<<EOF > test.expect
alpha alpha
gamma gamma
alpha alpha
EOF EOF
$SKIP 2 test.in --line alpha >test.out $SKIP 2 test.in --line alpha > test.out
$DIFF test.expect test.out $DIFF test.expect test.out
rm test.in test.expect test.out rm test.in test.expect test.out
echo "> skip lines until 2 tokens seen" echo "> skip lines until 2 tokens seen"
cat <<EOF >test.in cat<<EOF > test.in
Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet,
consectetur adipiscing elit, consectetur adipiscing elit,
sed do eiusmod tempor incididunt sed do eiusmod tempor incididunt
@ -64,37 +60,37 @@ quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea laboris nisi ut aliquip ex ea
commodo consequat. commodo consequat.
EOF EOF
cat <<EOF >test.expect cat<<EOF > test.expect
Ut enim ad minim veniam, Ut enim ad minim veniam,
quis nostrud exercitation ullamco quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea laboris nisi ut aliquip ex ea
commodo consequat. commodo consequat.
EOF EOF
$SKIP 2 test.in --token dolor >test.out $SKIP 2 test.in --token dolor > test.out
$DIFF test.expect test.out $DIFF test.expect test.out
rm test.in test.expect test.out rm test.in test.expect test.out
echo "> handle unknown parameter with simple error message" echo "> handle unknown parameter with simple error message"
cat <<EOF >test.expect.err cat<<EOF > test.expect.err
error: unexpected argument '--foo' found error: unexpected argument '--foo' found
tip: to pass '--foo' as a value, use '-- --foo' note: to pass '--foo' as a value, use '-- --foo'
Usage: skip [OPTIONS] <LINES> [FILE] Usage: skip [OPTIONS] <LINES> [FILE]
For more information, try '--help'. For more information, try '--help'.
EOF EOF
cat <<EOF >test.expect cat<<EOF > test.expect
EOF EOF
touch test.out test.err touch test.out test.err
$SKIP --foo >test.out 2>test.err || true $SKIP --foo > test.out 2> test.err || true
$DIFF test.expect test.out $DIFF test.expect test.out
$DIFF test.expect.err test.err $DIFF test.expect.err test.err
rm test.expect test.out rm test.expect test.out
rm test.expect.err test.err rm test.expect.err test.err
echo "> handle ignore-extra when token is missing" echo "> handle ignore-extra when token is missing"
cat <<EOF >test.expect.err cat<<EOF > test.expect.err
error: the following required arguments were not provided: error: the following required arguments were not provided:
--token <TOKEN> --token <TOKEN>
<LINES> <LINES>
@ -103,17 +99,17 @@ Usage: skip --ignore-extras --token <TOKEN> <LINES> [FILE]
For more information, try '--help'. For more information, try '--help'.
EOF EOF
cat <<EOF >test.expect cat<<EOF > test.expect
EOF EOF
touch test.out test.err touch test.out test.err
$SKIP --ignore-extras >test.out 2>test.err || true $SKIP --ignore-extras > test.out 2> test.err || true
$DIFF test.expect test.out $DIFF test.expect test.out
$DIFF test.expect.err test.err $DIFF test.expect.err test.err
rm test.expect test.out rm test.expect test.out
rm test.expect.err test.err rm test.expect.err test.err
echo "> skip lines until 4 tokens seen - ignored extra tokens on same line" echo "> skip lines until 4 tokens seen - ignored extra tokens on same line"
cat <<EOF >test.in cat<<EOF > test.in
Lorem ipsum dolor sit amet, Lorem ipsum dolor sit amet,
consectetur adipiscing elit, consectetur adipiscing elit,
sed do eiusmod tempor incididunt sed do eiusmod tempor incididunt
@ -123,12 +119,12 @@ quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea laboris nisi ut aliquip ex ea
commodo consequat. commodo consequat.
EOF EOF
cat <<EOF >test.expect cat<<EOF > test.expect
quis nostrud exercitation ullamco quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea laboris nisi ut aliquip ex ea
commodo consequat. commodo consequat.
EOF EOF
$SKIP 4 test.in --token m --ignore-extras >test.out $SKIP 4 test.in --token m --ignore-extras > test.out
$DIFF test.expect test.out $DIFF test.expect test.out
rm test.in test.expect test.out rm test.in test.expect test.out