forked from eliukblau/pixterm
/
pixterm.go
226 lines (187 loc) · 5.77 KB
/
pixterm.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
216
217
218
219
220
221
222
223
224
225
226
// ___ _____ ____
// / _ \/ _/ |/_/ /____ ______ _
// / ___// /_> </ __/ -_) __/ ' \
// /_/ /___/_/|_|\__/\__/_/ /_/_/_/
//
// Copyright 2017 Eliuk Blau
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/eliukblau/pixterm/ansimage"
"github.com/lucasb-eyer/go-colorful"
"golang.org/x/crypto/ssh/terminal"
)
const (
pxtVersion = "1.2.4"
pxtLogo = `
___ _____ ____
/ _ \/ _/ |/_/ /____ ______ _ Made with love by Eliuk Blau
/ ___// /_> </ __/ -_) __/ ' \ github.com/eliukblau/pixterm
/_/ /___/_/|_|\__/\__/_/ /_/_/_/ v{{VERSION}}
`
)
var (
flagCredits bool
flagDither uint
flagMatte string
flagScale uint
flagRows uint
flagCols uint
)
func main() {
validateFlags()
runPixterm()
}
func printLogo() {
fmt.Print(strings.Trim(strings.Replace(pxtLogo, "{{VERSION}}", pxtVersion, 1), "\n"), "\n\n")
}
func printCredits() {
printLogo()
fmt.Print("CONTRIBUTORS:\n\n")
fmt.Print(" > @disq - http://github.com/disq\n")
fmt.Print(" Original code for image transparency support.\n")
fmt.Println()
fmt.Print(" > @timob - http://github.com/timob\n")
fmt.Print(" Fix for ANSIpixel type: use 8bit color component for output.\n")
fmt.Println()
fmt.Print(" > @HongjiangHuang - http://github.com/HongjiangHuang\n")
fmt.Print(" Original code for image download support.\n")
fmt.Println()
fmt.Print(" > @danirod - http://github.com/danirod\n")
fmt.Print(" > @Xpktro - http://github.com/Xpktro\n")
fmt.Print(" Moral support.\n")
fmt.Println()
}
func throwError(code int, v ...interface{}) {
printLogo()
log.New(os.Stderr, "[PIXTERM ERROR] ", log.LstdFlags).Println(v...)
os.Exit(code)
}
func configureFlags() {
flag.CommandLine.Usage = func() {
printLogo()
_, file := filepath.Split(os.Args[0])
fmt.Print("USAGE:\n\n")
fmt.Printf(" %s [options] image/url\n\n", file)
fmt.Print(" Supported image formats: JPEG, PNG, GIF, BMP, TIFF, WebP.\n")
fmt.Print(" Supported URL protocols: HTTP, HTTPS.\n\n")
fmt.Print("OPTIONS:\n\n")
flag.CommandLine.SetOutput(os.Stdout)
flag.CommandLine.PrintDefaults()
flag.CommandLine.SetOutput(ioutil.Discard) // hide flag errors
fmt.Print(" -help\n\tprints this message :D LOL\n")
fmt.Println()
}
flag.CommandLine.SetOutput(ioutil.Discard) // hide flag errors
flag.CommandLine.Init(os.Args[0], flag.ExitOnError)
flag.CommandLine.BoolVar(&flagCredits, "credits", false, "shows some love to contributors <3")
flag.CommandLine.UintVar(&flagDither, "d", 0, "dithering `mode`:\n 0 - no dithering (default)\n 1 - with blocks\n 2 - with chars")
flag.CommandLine.StringVar(&flagMatte, "m", "", "matte `color` for transparency or background\n(optional, hex format, default: 000000)")
flag.CommandLine.UintVar(&flagScale, "s", 0, "scale `method`:\n 0 - resize (default)\n 1 - fill\n 2 - fit")
flag.CommandLine.UintVar(&flagRows, "tr", 0, "terminal `rows` (optional, >=2; when piping, default: 24)")
flag.CommandLine.UintVar(&flagCols, "tc", 0, "terminal `columns` (optional, >=2; when piping, default: 80)")
flag.CommandLine.Parse(os.Args[1:])
}
func validateFlags() {
if flagCredits {
printCredits()
os.Exit(0)
}
if flagDither != 0 && flagDither != 1 && flagDither != 2 {
flag.CommandLine.Usage()
os.Exit(2)
}
if flagScale != 0 && flagScale != 1 && flagScale != 2 {
flag.CommandLine.Usage()
os.Exit(2)
}
if (flagRows > 0 && flagRows < 2) || (flagCols > 0 && flagCols < 2) {
flag.CommandLine.Usage()
os.Exit(2)
}
// this is image filename
if flag.CommandLine.Arg(0) == "" {
flag.CommandLine.Usage()
os.Exit(2)
}
}
func isTerminal() bool {
return terminal.IsTerminal(int(os.Stdout.Fd()))
}
func getTerminalSize() (width, height int, err error) {
if isTerminal() {
return terminal.GetSize(int(os.Stdout.Fd()))
}
// fallback when piping to a file!
return 80, 24, nil // VT100 terminal size
}
func runPixterm() {
var (
pix *ansimage.ANSImage
err error
)
// get terminal size
tx, ty, err := getTerminalSize()
if err != nil {
throwError(1, err)
}
// use custom terminal size (if applies)
if ty--; flagRows != 0 { // no custom rows? subtract 1 for prompt spacing
ty = int(flagRows) + 1 // weird, but in this case is necessary to add 1 :O
}
if flagCols != 0 {
tx = int(flagCols)
}
// get scale mode from flag
sm := ansimage.ScaleMode(flagScale)
// get dithering mode from flag
dm := ansimage.DitheringMode(flagDither)
// set image scale factor for ANSIPixel grid
sfy, sfx := ansimage.BlockSizeY, ansimage.BlockSizeX // 8x4 --> with dithering
if ansimage.DitheringMode(flagDither) == ansimage.NoDithering {
sfy, sfx = 2, 1 // 2x1 --> without dithering
}
// get matte color
if flagMatte == "" {
flagMatte = "000000" // black background
}
mc, err := colorful.Hex("#" + flagMatte) // RGB color from Hex format
if err != nil {
throwError(2, fmt.Sprintf("matte color : %s is not a hex-color", flagMatte))
}
// create new ANSImage from file
file := flag.CommandLine.Arg(0)
if matched, _ := regexp.MatchString(`^https?://`, file); matched {
pix, err = ansimage.NewScaledFromURL(file, sfy*ty, sfx*tx, mc, sm, dm)
} else {
pix, err = ansimage.NewScaledFromFile(file, sfy*ty, sfx*tx, mc, sm, dm)
}
if err != nil {
throwError(1, err)
}
// draw ANSImage to terminal
if isTerminal() {
ansimage.ClearTerminal()
}
pix.SetMaxProcs(runtime.NumCPU()) // maximum number of parallel goroutines!
pix.Draw()
if isTerminal() {
fmt.Println()
}
}
func init() {
runtime.GOMAXPROCS(runtime.NumCPU()) // use paralelism for goroutines!
configureFlags()
}