-
Notifications
You must be signed in to change notification settings - Fork 118
/
main.go
130 lines (108 loc) · 3.1 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"log"
"github.com/mattn/go-colorable"
"github.com/spf13/cobra"
)
const (
readFromStdin = "-"
)
type ccatCmd struct {
BG string
Color string
ColorCodes mapValue
HTML bool
ShowPalette bool
ShowVersion bool
}
func (c *ccatCmd) Run(cmd *cobra.Command, args []string) {
stdout := colorable.NewColorableStdout()
if c.ShowVersion {
displayVersion(stdout)
return
}
var colorPalettes ColorPalettes
if c.BG == "dark" {
colorPalettes = DarkColorPalettes
} else {
colorPalettes = LightColorPalettes
}
// override color codes
for k, v := range c.ColorCodes {
ok := colorPalettes.Set(k, v)
if !ok {
log.Fatal(fmt.Errorf("unknown color code: %s", k))
}
}
if c.ShowPalette {
fmt.Fprintf(stdout, `Applied color codes:
%s
Color code is in the format of:
color normal color
*color* bold color
_color_ underlined color
+color+ blinking color
Value of color can be %s
`, colorPalettes, colorCodes)
return
}
var printer CCatPrinter
if c.HTML {
printer = HtmlPrinter{colorPalettes}
} else if c.Color == "always" {
printer = ColorPrinter{colorPalettes}
} else if c.Color == "never" {
printer = PlainTextPrinter{}
} else {
printer = AutoColorPrinter{colorPalettes}
}
// if there's no args, read from stdin
if len(args) == 0 {
args = []string{readFromStdin}
}
for _, arg := range args {
err := CCat(arg, printer, stdout)
if err != nil {
log.Fatal(err)
}
}
}
func main() {
log.SetFlags(0)
ccatCmd := &ccatCmd{
ColorCodes: make(mapValue),
}
rootCmd := &cobra.Command{
Use: "ccat [OPTION]... [FILE]...",
Long: "Colorize FILE(s), or standard input, to standard output.",
Example: `$ ccat FILE1 FILE2 ...
$ ccat --bg=dark FILE1 FILE2 ... # dark background
$ ccat --html # output html
$ ccat -G String="_darkblue_" -G Plaintext="darkred" FILE # set color codes
$ ccat --palette # show palette
$ ccat # read from standard input
$ curl https://raw.githubusercontent.com/jingweno/ccat/master/main.go | ccat`,
Run: ccatCmd.Run,
}
usageTempl := `{{ $cmd := . }}
Usage:
{{.UseLine}}
Flags:
{{.LocalFlags.FlagUsages}}
Using color is auto both by default and with --color=auto. With --color=auto,
ccat emits color codes only when standard output is connected to a terminal.
Color codes can be changed with -G KEY=VALUE. List of color codes can
be found with --palette.
Examples:
{{ .Example }}
`
rootCmd.SetUsageTemplate(usageTempl)
rootCmd.PersistentFlags().StringVarP(&ccatCmd.BG, "bg", "", "light", `set to "light" or "dark" depending on the terminal's background`)
rootCmd.PersistentFlags().StringVarP(&ccatCmd.Color, "color", "C", "auto", `colorize the output; value can be "never", "always" or "auto"`)
rootCmd.PersistentFlags().VarP(&ccatCmd.ColorCodes, "color-code", "G", `set color codes`)
rootCmd.PersistentFlags().BoolVarP(&ccatCmd.HTML, "html", "", false, `output html`)
rootCmd.PersistentFlags().BoolVarP(&ccatCmd.ShowPalette, "palette", "", false, `show color palettes`)
rootCmd.PersistentFlags().BoolVarP(&ccatCmd.ShowVersion, "version", "v", false, `show version`)
rootCmd.Execute()
}