Skip to content

Commit

Permalink
Merge pull request #69 from nevdelap/issue67
Browse files Browse the repository at this point in the history
Issue67
  • Loading branch information
nevdelap authored Feb 2, 2019
2 parents b46afbf + 9dc794e commit b4c5958
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 57 deletions.
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

[package]
name = "ned"
version = "1.2.6"
version = "1.2.7"
authors = ["Nev Delap <[email protected]>"]

[dependencies]
Expand Down
112 changes: 74 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ Options:
(or ASCII). because this requires reading the file the
--exclude option should be preferred
-a, --all do not ignore entries starting with .
-c, --colors [WHEN] show filenames and matches in color when a real
stdout. defaults to auto, can be set to always to show
color even when not a real stdout, or never
-c show filenames, line numbers, and matches in color. is
the same as --colors=always
--colors [WHEN] 'auto' shows filenames, line numbers, and matches in
color when stdout is a terminal, not when it is a
pipe, 'always' shows color even when stdout is a pipe,
and 'never' never shows colors
--stdout output to stdout
-q, --quiet suppress all normal output
-V, --version output version information and exit
Expand All @@ -94,12 +97,12 @@ Exit codes:
1 no matches
Quiet:
When -q --quiet is specified ned tests for matches and returns an exit
When -q (--quiet) is specified, ned tests for matches and returns an exit
code of 0 if a match is found in ANY file. Quiet matches will only read
as many files as needed to find a match. Even without this shortcutting
behaviour quiet matches are more performant than non-quiet matches.
behaviour, quiet matches are more performant than non-quiet matches.
ned 1.2.6 Copyright (C) 2016-2019 Nev Delap - https://github.com/nevdelap/ned
ned 1.2.7 Copyright (C) 2016-2019 Nev Delap - https://github.com/nevdelap/ned
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
Expand Down Expand Up @@ -186,12 +189,10 @@ Download the 64-bit or 32-bit deb file from the latest release: https://github.c

# TL;DR

**NOTE:** This section is new, so far I've just typed these in off the top of my head, some are not there yet, and I have not tested any of them, so there will be typos.
**IMPORTANT NOTE:** The search capabilities of `ned` are not so interesting, you can do them all with `grep`. It is the replace that is interesting. Examples of searching are shown first, followed by examples of replacing. Replacing with `ned` is a very powerful way of doing bulk editing from the terminal.

These examples use short options and search for 'dog' and replace with 'cat' wherever the example doesn't need a regular expression to demonstrate what it is doing.

**IMPORTANT NOTE:** The search capabilities of `ned` are not interesting, you can do them all with `grep`. It is the replace that is its super power. Examples of searching are shown here first, before examples of replacing.

#### Search non-hidden files in the current directory.
```
ned dog .
Expand Down Expand Up @@ -221,24 +222,27 @@ ned --colors=always dog .
```
ned --colors=never dog .
```
#### Search showing colored output when outputting to a terminal.
#### Search showing colored output when outputting to a terminal, but don't send colored output if piped.
```
ned --colors=auto dog .
```
#### Set default arguments in your terminal environment.
```
export NED_DEFAULTS='-i --colors=always'
```
#### Search showing colored output and having it work when paging with less.
#### Search showing colored output through less.
```
ned --colors=always dog . | less -R
ned -c dog . | less -R
```
#### Search showing no output, to just use the exit code in a script if something is found or not found.
Is also more efficient.
This is more efficient when you don't need the output since it shortcuts when it finds the first match.
```
ned -q dog .
ned -q dog .; echo $?
0 # Found.
ned -q dinosaur .; echo $?
1 # Not found.
```
#### Search specifying the pattern at the end of the command, for convenience of editing.
#### Search specifying the pattern at the end of the command - for convenience of editing when you have a lot of options.
```
ned . -p dog
```
Expand Down Expand Up @@ -266,33 +270,45 @@ ned -o dog .
```
ned -oFL dog .
```
#### Search matching 3 occurences.
#### Search matching first 3 occurences per line.
```
ned -n 3 dog .
```
#### Search skipping 3 occurrences and finding 2 occurences. (Most useful for replaces.)
#### Search matching first 3 occurences per file.
```
ned -w -n 3 dog .
```
#### Search backwards, matching first 3 occurences per line.
```
ned -b -n 3 dog .
```
#### Search backwards, matching first 3 occurences per file.
```
ned -b -w -n 3 dog .
```
ned -s 3 -n 2 dog .
#### Search skipping 3 occurrences and finding 2 occurences.
**Note:** -k is the short form of --skip. (-s is the short form of the --single option.)
```
#### Search backwards from the end of the file. (Most useful for replaces.)
You can also skip backwards, and match n occurrences backwards.
ned -k 3 -n 2 dog .
```
ned -b dog .
#### Search backwards, skipping 3 occurrences and finding 2 occurences.
```
ned -b -k 3 -n 2 dog .
```
#### Search recursively only including certain files.
```
ned -R --include '*.txt' dog .
```
#### Search ignoring files.
#### Search ignoring certain files.
```
ned -R--exclude '*.htm' dog .
ned -R --exclude '*.htm' dog .
```
#### Search ignoring all non-utf8 files.
Quietly ignore files that cannot be parsed as UTF-8 (or ASCII). Because this requires reading the file the --exclude option should be preferred. E.g. --exclude '*.png'
```
ned -u dog .
```
#### Search ignoring directories.
#### Search ignoring certain directories.
```
ned -R --exclude-dir '.git' dog .
```
Expand All @@ -308,45 +324,65 @@ ned -B 5 dog .
```
ned -A 5 dog .
```
#### Search matching the beginning or end of lines.
#### Search matching the beginnings of lines.
```
ned '^dog' .
```
#### Search matching the ends of lines.
```
ned 'dog$' .
```
#### Search matching the beginnings of files.
```
ned -w '^dog' .
```
#### Search matching the beginning or end of files.
#### Search matching the ends of files.
```
ned -w 'dog$' .
```
#### Search spanning lines.
Search for any and all three consecutive lines containing the word dog.
```
ned -w 'dog.*\n.*dog.*\n.*dog' .
```
#### Replace.
```
ned dog -r cat .
```
#### Replace using numbered group references.
'the big dog and the smelly dog' replaced with 'the smelly dog and the big dog'.
```
ned 'the ([a-z]+) dog and the ([a-z]+) cat' -r 'the $2 dog and the $1 cat'
ned 'the ([a-z]+) dog and the ([a-z]+) dog' -r 'the $2 dog and the $1 dog' .
```
#### Replace using named group references.
'the big dog and the smelly dog' replaced with 'the smelly dog and the big dog'.
```
```
#### Replace matching beginnings and endings of lines.
```
ned 'the (?P<first>[a-z]+) dog and the (?P<second>[a-z]+) dog' -r 'the $second dog and the $first dog' .
```
#### Replace spanning lines.
Delete any and all three consecutive lines containing the word dog.
```
```
#### Replace spanning lines and still matching beginnings and endings of lines.
As opposed to matching the beginnings and endings of the file.
```
ned '\n.*dog.*\n.*dog.*\n.*dog.*\n' -r '\n'
```
#### Replace changing case.
'big dog' and 'smelly dog' replaced with 'BIG! dog' and 'SMELLY! dog'.

Available case replacements: \U - uppercase, \L - lowercase, \I - initial uppercase (title case), \F - first uppercase (sentence case).
```
ned ' ([a-z]+) dog' --case-replacements -r '\U$1\E! dog' --stdout .
```
#### Replace and see the results without updating the target files.
#### Replace and see the results in the terminal without updating the target files.
```
ned dog -r cat --stdout .
```
#### Unident tables and lists in MadCap Flare XHTML topic and snippet files.
#### Strip blank lines from files.
```
ned -w '(\s*\n)+' -r '\n' .
```
#### Strip blank lines from the ends of files.
```
ned -w '(\s*\n?)*$' -r '' .
```
#### Unident tables and lists in XHTML files, ignoring the .git directory.
```
ned -R --include '*.htm' --include '*.flsnp' --exclude-dir .git ' (</?(table|col|tbody|tr|th|td|ol|ul|li)[^>]*>)' -r '$1'
ned -R --include '*.htm' --exclude-dir '.git ' (</?(table|col|tbody|tr|th|td|ol|ul|li)[^>]*>)' -r '$1'
```
9 changes: 5 additions & 4 deletions src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ Exit codes:
1 no matches
Quiet:
When -q --quiet is specified ned tests for matches and returns an exit
When -q (--quiet) is specified, ned tests for matches and returns an exit
code of 0 if a match is found in ANY file. Quiet matches will only read
as many files as needed to find a match. Even without this shortcutting
behaviour quiet matches are more performant than non-quiet matches.";
behaviour, quiet matches are more performant than non-quiet matches.";

static COPYRIGHT: &'static str =
"\
Expand Down Expand Up @@ -176,10 +176,11 @@ pub fn make_opts() -> Options {
this requires reading the file the --exclude option should be preferred",
);
opts.optflag("a", "all", "do not ignore entries starting with .");
opts.optflag("c", "", "show filenames, line numbers, and matches in color. is the same as --colors=always");
opts.optflagopt(
"c",
"",
"colors",
"show filenames and matches in color when a real stdout. defaults to auto, can be set to always to show color even when not a real stdout, or never",
"'auto' shows filenames, line numbers, and matches in color when stdout is a terminal, not when it is a pipe, 'always' shows color even when stdout is a pipe, and 'never' never shows colors",
"WHEN",
);
opts.optflag("", "stdout", "output to stdout");
Expand Down
3 changes: 2 additions & 1 deletion src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,10 @@ pub fn get_parameters(options_with_defaults: &OptionsWithDefaults) -> NedResult<
libc::isatty(/*libc::STDOUT_FILENO as i32*/ 1)
} != 0;

let c = options_with_defaults.opt_present("c");
let colors = parse_opt_str(&options_with_defaults, "colors", Some(Colors::Off))?
.expect("The default is a Some.");
let colors = (colors == Colors::Always && (replace.is_none() || replace.is_some() && stdout)
let colors = c || (colors == Colors::Always && (replace.is_none() || replace.is_some() && stdout)
|| colors == Colors::Auto && (replace.is_none() || stdout) && isatty)
&& colors != Colors::Never;

Expand Down
11 changes: 11 additions & 0 deletions src/tests/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,17 @@ fn colored_match() {
test(&args, expected_exit_code, &expected_screen_output);
}

#[test]
fn colored_match_short_option_not_last_argument() {
let args = vec!["accidentally.*hand", "-c", "test"];
let expected_exit_code = 0;
let expected_screen_output = ["\u{1b}[35mtest/file1.txt:1:\u{1b}[0mThe \
\u{1b}[1;31maccidentally ghastly hand\u{1b}[0m plans AN \
ESCAPE from a cream puff the placid widow. A slovenly\n"];

test(&args, expected_exit_code, &expected_screen_output);
}

#[test]
fn colored_match_whole_files() {
let args = vec![
Expand Down

0 comments on commit b4c5958

Please sign in to comment.