-
Notifications
You must be signed in to change notification settings - Fork 11
/
img.go
215 lines (176 loc) · 4.56 KB
/
img.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"bytes"
"flag"
"os"
"os/exec"
"path/filepath"
"strings"
"hawx.me/code/hadfield"
"hawx.me/code/img/cmd"
"hawx.me/code/img/utils"
)
type External struct {
Path, Usage, Short, Long string
}
func (e External) String() string {
return "External{" + e.Path + "}"
}
func (e *External) Name() string {
return filepath.Base(e.Path)[4:]
}
func (e *External) Data() interface{} {
return map[string]interface{}{
"Callable": e.Callable(),
"Category": e.Category(),
"Usage": e.Usage,
"Short": e.Short,
"Long": e.Long,
"Name": e.Name(),
}
}
func (e *External) Category() string {
return "External"
}
func (e *External) Callable() bool {
return true
}
func (e *External) Call(cmd hadfield.Interface, templates hadfield.Templates, args []string) {
if args[1] == "-h" || args[1] == "--help" {
hadfield.CommandUsage(cmd, templates)
}
// args[0] is set to the executable's name, so we can safely replace it with
// the output type. This is always going to be something, so needs to be
// checked for, removed, and respected!
args[0] = string(utils.Output)
ex := exec.Command(e.Path, args...)
ex.Stdin = os.Stdin
ex.Stdout = os.Stdout
ex.Stderr = os.Stderr
err := ex.Run()
if err != nil {
os.Exit(2)
}
return
}
func findExternalsIn(dir string) ([]string, error) {
found := []string{}
dirs, _ := filepath.Glob(dir + "/" + "*")
for _, possible := range dirs {
if strings.HasPrefix(filepath.Base(possible), "img-") {
found = append(found, possible)
}
}
return found, nil
}
func runExternal(ext string, flags ...string) string {
cmd := exec.Command(ext, flags...)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
// handle
}
return out.String()
}
func lookupExternals() hadfield.Commands {
found := hadfield.Commands{}
pathenv := os.Getenv("PATH")
output := string(utils.Output)
for _, dir := range strings.Split(pathenv, ":") {
if dir == "" {
dir = "."
}
if exts, err := findExternalsIn(dir); err == nil {
for _, ext := range exts {
usage := runExternal(ext, output, "--usage")
short := runExternal(ext, output, "--short")
long := runExternal(ext, output, "--long")
found = append(found, &External{ext, usage, short, long})
}
}
}
return found
}
// Commands list the available commands and help topics. The order here is the
// order in which they are printed by 'img help'.
var commands = hadfield.Commands{
cmd.Blend(),
cmd.Blur(),
cmd.Channel(),
cmd.Contrast(),
cmd.Crop(),
cmd.Gamma(),
cmd.Greyscale(),
cmd.Hxl(),
cmd.Levels(),
cmd.Pixelate(),
cmd.Pxl(),
cmd.Sharpen(),
cmd.Shuffle(),
cmd.Tint(),
cmd.Vibrance(),
cmd.Vxl(),
}
var templates = hadfield.Templates{
Help: `Usage: img [command] [arguments]
Img is a set of image manipulation tools. They each take an image from STDIN
and print the result to STDOUT (in some cases they may also require a second
image, consult the help for the particular command).
An example usage,
$ img greyscale < input.png > output.png
As standard input and output are used throughout, commands can be easily
chained together using pipes (and parentheses for clarity),
$ (img greyscale | img pxl | img contrast --by 0.05) < input.png > output.png
Commands: {{range .}}{{if eq .Category "Command"}}
{{.Name | printf "%-15s"}} # {{.Short | trim}}{{end}}{{end}}
External Commands: {{range .}}{{if eq .Category "External"}}
{{.Name | printf "%-15s"}} # {{.Short | trim}}{{end}}{{end}}
Use "img help [command]" for more information about a command.
`,
Command: `{{if .Callable}}Usage: img {{.Usage}}
{{end}}{{.Long}}
`,
}
var builtIn = []string{
"blend", "blur", "channel", "contrast", "crop", "gamma", "greyscale", "hxl",
"levels", "pixelate", "pxl", "sharpen", "shuffle", "tint", "vxl",
}
func isRunningBuiltin(args []string) bool {
if len(args) == 0 {
return false
}
for _, v := range builtIn {
if args[0] == v {
return true
}
}
return false
}
func main() {
flag.Usage = func() {
externals := lookupExternals()
hadfield.Usage(append(commands, externals...), templates)
}
var jpeg, png, tiff bool
flag.BoolVar(&jpeg, "jpg", false, "")
flag.BoolVar(&jpeg, "jpeg", false, "")
flag.BoolVar(&png, "png", false, "")
flag.BoolVar(&tiff, "tiff", false, "")
flag.BoolVar(&tiff, "tif", false, "")
flag.Parse()
if jpeg {
utils.Output = utils.JPEG
}
if png {
utils.Output = utils.PNG
}
if tiff {
utils.Output = utils.TIFF
}
if !isRunningBuiltin(flag.Args()) {
externals := lookupExternals()
commands = append(commands, externals...)
}
hadfield.Run(commands, templates)
}