/
display.go
executable file
·118 lines (107 loc) · 2.38 KB
/
display.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/fatih/color"
)
var (
Red = color.New(color.FgRed)
Blue = color.New(color.FgBlue)
Green = color.New(color.FgGreen)
Magenta = color.New(color.FgMagenta)
White = color.New(color.FgWhite)
Black = color.New(color.FgBlack)
BRed = color.New(color.FgRed, color.Bold)
BBlue = color.New(color.FgBlue, color.Bold)
BGreen = color.New(color.FgGreen, color.Bold)
BMagenta = color.New(color.FgMagenta, color.Bold)
BWhite = color.New(color.Bold, color.FgWhite)
BBlack = color.New(color.Bold, color.FgBlack)
)
// Print help and exit with a status code.
func help(status int) {
defer os.Exit(status)
fmt.Printf(
"%v%v%v\n",
`Usage: lss-img [options] <patterns-to-match>
Options:
-d "`, root, `":
Start search under a specific directory
-x:
Patterns to exclude from matches, separated by commas
-f:
Find matches based on the full path of files
(by default, only the basenames of files are checked for matches)
-v:
Display slightly more output
-r:
Display the relative path to <path>
(the full path is displayed by default)
-b
Display the basename only
(the full path is displayed by default)
-c
Colorize output
-w:
Write output to file
-h:
Print this help`,
)
}
// Print a line break.
func lineBreak(s string) {
line := strings.Repeat(s, 79)
fmt.Println(line)
}
// Print a thin line break (of '-' characters).
func line(c *color.Color) {
c.Set()
defer colorUnset()
lineBreak("-")
}
// Print a thick line break (of '=' characters).
func boldLine(c *color.Color) {
c.Set()
defer colorUnset()
lineBreak("=")
}
// Print a found match.
func progress(output string) {
if doColor == false {
fmt.Println(output)
return
}
defer colorUnset()
dir, filename := filepath.Split(output)
Blue.Printf("%v/", dir)
Green.Println(filename)
}
// Print the number of found matches.
func report() {
if !doVerbose {
return
}
results := fmt.Sprintf("Matches found: %d", total)
if !doColor {
lineBreak("-")
fmt.Println(results)
return
}
defer colorUnset()
line(BWhite)
BGreen.Println(results)
}
// Exit if an error occurs.
func chkerr(err error) {
if err == nil {
return
}
log.Fatal(err)
}
// Reset console color to normal.
func colorUnset() {
color.Unset()
}