-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (162 loc) · 4.07 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
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
package main
import (
"errors"
"fmt"
"github.com/oderwat/timg/humz"
"github.com/oderwat/timg/must"
"github.com/oderwat/timg/out"
"github.com/urfave/cli/v2"
"github.com/ysmood/gotrace"
"os"
"runtime"
"runtime/debug"
"time"
)
const (
LogDebug = 1
LogInfo = 2
LogNormal = 3
LogWarn = 4
LogErr = 5
)
var LogLevel = LogNormal
var flagLogLevel = &cli.IntFlag{
Name: "log-level",
Value: LogLevel,
Usage: "logging level",
}
var flagPerf = &cli.BoolFlag{
Name: "perf",
Usage: "Giving some performance statistics at the end",
Value: false,
}
var flagRecursive = &cli.BoolFlag{
Name: "recursive",
Aliases: []string{"r"},
Usage: "Giving some performance statistics at the end",
Value: false,
}
var flagShowUnknownTypes = &cli.BoolFlag{
Name: "show-unknown-extensions",
Aliases: []string{"uext"},
Value: false,
Usage: "show files that are not of a known extension and get skipped",
}
var flagUseExtensions = &cli.BoolFlag{
Name: "use-extensions",
Aliases: []string{"x"},
Value: true,
Usage: "use extensions to predict what files can be shown",
}
var flagWidth = &cli.IntFlag{
Name: "width",
Aliases: []string{"w"},
Value: 0,
Usage: "sets the with of the output",
}
var flagHeight = &cli.IntFlag{
Name: "height",
Aliases: []string{"h"},
Value: 0,
Usage: "sets the height of the output",
}
var flagVersion = &cli.BoolFlag{
Name: "print-version",
Aliases: []string{"V"},
Usage: "print only the version",
}
var cmdPanic = &cli.Command{
Name: "panic",
Usage: "just for testing a panic",
Action: func(c *cli.Context) error {
must.Ok(errors.New("don't panic"))
return nil
},
}
func main() {
cli.VersionFlag = flagVersion
started := time.Now()
app := &cli.App{
Name: "Timg",
Usage: "Shows a single image or all images of a directory inside the terminal (e.g. ITerm2)",
UsageText: "timg [global options] [commands] [comand options]\n\n" +
"The default command is show and can be omitted. See 'timg h show' for help.\n\n" +
"Examples: 'timg ~/Pictures/', 'timg -r -h 80 ~/Pictures/Photos\\ Library.photoslibrary/originals/'",
Version: "v0.1.0",
Compiled: time.Now(),
Flags: []cli.Flag{
flagLogLevel,
flagPerf,
flagWidth,
flagHeight,
flagUseExtensions,
flagShowUnknownTypes,
flagRecursive,
},
Before: func(c *cli.Context) error {
LogLevel = c.Int(flagLogLevel.Name)
if LogLevel <= LogDebug {
out.Outf("Set Log Level %d\n", LogLevel)
}
return nil
},
ArgsUsage: "[imagepath, ...]",
Action: runTImg,
After: func(c *cli.Context) error {
if len(os.Args) > 1 && os.Args[len(os.Args)-1] == "--generate-bash-completion" {
return nil
}
if c.Bool(flagPerf.Name) {
var m runtime.MemStats
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
runtime.ReadMemStats(&m)
out.Outf("Performance:\n")
out.Outf(" Time taken: %s\n", humz.Duration(time.Since(started)))
out.Outf(" Alloc : %s\n", humz.Bytes(int64(m.Alloc)))
out.Outf(" TotalAlloc: %s\n", humz.Bytes(int64(m.TotalAlloc)))
out.Outf(" Sys : %s\n", humz.Bytes(int64(m.Sys)))
out.Outf(" NumGC : %s\n", humz.Count(int64(m.NumGC)))
}
return nil
},
}
cli.HelpFlag = &cli.BoolFlag{
Name: "help",
Aliases: []string{"?"},
Usage: "show help",
}
app.Commands = append(app.Commands, cmdPanic)
app.Commands = append(app.Commands, cmdTImg())
app.EnableBashCompletion = true
// add all our commands
exitCode := 0
must.Protect(func() {
defer must.CheckLock(func(x gotrace.Traces) {
// here we end up with CTRL+C (and other signals)
out.OutfKeep("*** Aborted\n")
if LogLevel <= LogDebug {
fmt.Println(x)
}
exitCode = 1
})()
go func() {
err := app.Run(os.Args)
// this is the regular ending
if err != nil {
out.OutfKeep("%s\n", err.Error())
if LogLevel <= LogDebug {
debug.PrintStack()
}
exitCode = 2
}
}()
}, func(x any) {
// here we end up if a panic happened
out.OutfKeep("*** Panic: %v!\n", x)
if LogLevel <= LogDebug {
debug.PrintStack()
}
exitCode = 3
})
os.Exit(exitCode)
}