Skip to content

Commit

Permalink
show usage of program (closes #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
arsham committed Jun 11, 2018
1 parent 2badee9 commit 2750bbb
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ are not followed by colouring arguments are coloured based on previously
provided colour:

```bash
$ blush -b match1 match3 FILENAME
$ blush -b match1 match2 FILENAME
```

![Colored](http://i.imgur.com/J6uZPQD.png)
Expand Down
11 changes: 7 additions & 4 deletions cmd/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,21 @@ func setup(t *testing.T, args string) (stdout, stderr *stdFile, cleanup func())
if len(args) > 1 {
os.Args = append(os.Args, strings.Split(args, " ")...)
}
logFatalErr := func(msg ...interface{}) {
fatalPatch := monkey.Patch(log.Fatal, func(msg ...interface{}) {
fmt.Fprintln(os.Stderr, msg)
}
patch := monkey.Patch(log.Fatal, logFatalErr)
})
fatalfPatch := monkey.Patch(log.Fatalf, func(format string, v ...interface{}) {
fmt.Fprintf(os.Stderr, format, v...)
})

cleanup = func() {
outCleanup()
errCleanup()
os.Args = oldArgs
os.Stdout = oldStdout
os.Stderr = oldStderr
patch.Unpatch()
fatalPatch.Unpatch()
fatalfPatch.Unpatch()
}
return stdout, stderr, cleanup
}
Expand Down
19 changes: 16 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"io"
"log"
"os"
Expand All @@ -16,8 +17,13 @@ import (
// blush.Blush instance. It then uses io.Copy() to write to standard output.
func Main() {
b, err := GetBlush(os.Args)
if err != nil {
log.Fatal(err)
switch err {
case nil:
case errShowHelp:
fmt.Println(Usage)
return
default:
log.Fatalf("%s\n%s", err, Help)
return // this return statement should be here to support tests.
}
defer func() {
Expand All @@ -43,11 +49,18 @@ func GetBlush(input []string) (*blush.Blush, error) {
ok bool
noCut bool
noFileName bool
remaining []string
err error
r io.ReadCloser
)
if len(input) == 1 {
return nil, ErrNoInput
}
remaining, r, err := getReader(input[1:])
if remaining, ok = hasArg(input[1:], "--help"); ok {
return nil, errShowHelp
}

remaining, r, err = getReader(remaining)
if err != nil {
return nil, err
}
Expand Down
15 changes: 15 additions & 0 deletions cmd/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ func TestMainNoArgs(t *testing.T) {
if !strings.Contains(stderr.String(), cmd.ErrNoInput.Error()) {
t.Errorf("stderr = `%s`, want `%s` in it", stderr.String(), cmd.ErrNoInput.Error())
}
if !strings.Contains(stderr.String(), cmd.Help) {
t.Errorf("stderr = `%s`, want `%s` in it", stderr.String(), cmd.Help)
}
}

func TestMainHelp(t *testing.T) {
stdout, stderr, cleanup := setup(t, "--help")
defer cleanup()
cmd.Main()
if len(stderr.String()) > 0 {
t.Errorf("didn't expect any stderr, got: %s", stderr.String())
}
if !strings.Contains(stdout.String(), cmd.Usage) {
t.Errorf("stdout = `%s`, want `%s` in it", stdout.String(), cmd.Usage)
}
}

func TestPipeInput(t *testing.T) {
Expand Down
46 changes: 46 additions & 0 deletions cmd/usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import "errors"

// These variables are used for showing help messages on command line.
var (
errShowHelp = errors.New("show errors")

Help = "Usage: blush [OPTION]... PATTERN [FILE]...\nTry 'blush --help' for more information."
Usage = `Usage: blush [OPTION]... PATTERN [FILE]...
Colours:
-r, --red Match decorated with red colour. See Stock Colours section.
-r[G], --red[G] Matches are grouped with the group number.
Example: blush -b1 match filename
-#RGB, --#RGB Use user defined colour schemas.
Example: blush -#1eF match filename
-#RRGGBB, --#RRGGBB Same as -#RGB/--#RGB.
Pattern:
You can use simple pattern or regexp. If your pattern expands between
multiple words or has space in between, you should put them in quotations.
Stock Colours:
-r, --red
-g, --green
-b, --blue
-w, --white
-bl, --black
-yl, --yellow
-mg, --magenta
-cy, --cyan
Control arguments:
-C, --colour Don't drop unmatched lines.
-i Case insensitive match.
--no-colour, --no-color Don't colourise the output.
-h, --no-filename Suppress the prefixing of file names on output.
Multi match colouring:
blush -b match1 [match2]...: will colourise all matches with the same colour.
Using pipes:
cat FILE | blush -b match [-g match]...
`
)

0 comments on commit 2750bbb

Please # to comment.