forked from codeliveroil/img
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimg.go
95 lines (83 loc) · 2.37 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
// Copyright (c) 2018 codeliveroil. All rights reserved.
//
// This work is licensed under the terms of the MIT license.
// For a copy, see <https://opensource.org/licenses/MIT>.
package main
import (
"fmt"
_ "image/jpeg"
_ "image/png"
"os"
"github.com/codeliveroil/img/viz"
"github.com/codeliveroil/niceflags"
)
func main() {
args := os.Args
flags := niceflags.NewFlags(
args[0],
"Image viewer for Linux terminal emulators",
"Supports PNG, JPEG and GIF.\n"+
"Images can be rendered on screen (default) or exported to a shell script to be "+
"rendered later (e.g. to display a logo during SSH login).\n"+
"GIFs are animated and restricted to a 40 character width by default.\n"+
"To obtain best quality rendering, try reducing the font size of the terminal.",
"[options] file",
"help",
false,
)
flags.Examples = []string{
"car.png",
"logo.gif",
"-l 2 wheel.gif",
}
userWidth := flags.Int("w", 0, "Use specified `width` instead of auto-computing it.")
exportFilename := flags.String("o", "", "Export image as a shell script to specified `file`.")
loopCount := flags.Int("l", 1, "Specify the `num`ber of times the GIF should be looped or set to 0 to render the first frame only.")
delayMultiplier := flags.Float64("s", 1.0, "Specify a multiplier to change the `speed` of animation. "+
"Larger the multiplier, slower the speed of animation. "+
"For example, 2 decreases the speed to 50% and 0.5 increases the speed to 200%.")
version := flags.Bool("v", false, "Display version.")
check(flags.Parse(args[1:]))
flags.Help()
argc := len(args)
if argc < 2 {
flags.Usage()
os.Exit(0)
}
if *version {
fmt.Println("1.1")
os.Exit(0)
}
filename := args[argc-1]
if filename == "" {
niceflags.PrintErr("image file not specified.\n")
flags.Usage()
os.Exit(1)
}
//Render/Export image
img := viz.Image{
Filename: filename,
ExportFilename: *exportFilename,
LoopCount: *loopCount,
DelayMultiplier: *delayMultiplier,
UserWidth: *userWidth,
}
check(img.Init())
var canvas viz.Canvas
if img.ExportFilename == "" {
canvas = &viz.StdoutCanvas{}
} else {
var err error
canvas, err = viz.NewFileCanvas(img.ExportFilename)
check(err)
}
check(img.Draw(canvas))
}
// check prints the error message and exits
// if err is not nil.
func check(err error) {
if err != nil {
niceflags.PrintErr("%v\n", err)
os.Exit(1)
}
}