-
Notifications
You must be signed in to change notification settings - Fork 2
/
explosion.go
111 lines (96 loc) · 2.53 KB
/
explosion.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
package main
import (
"flag"
"fmt"
"image"
"os"
"github.com/kr/pty"
"github.com/nfnt/resize"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff"
)
func printImage(img image.Image, width uint, height uint) {
resized := resize.Thumbnail(width, height, img, resize.NearestNeighbor)
bounds := resized.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y += 2 {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
upperRed, upperGreen, upperBlue, _ := resized.At(x, y).RGBA()
lowerRed, lowerGreen, lowerBlue, _ := resized.At(x, y+1).RGBA()
// Only print the background if upper and lower row are same color
if upperRed == lowerRed && upperGreen == lowerGreen && upperBlue == lowerBlue {
fmt.Printf(
"\x1b[48;2;%d;%d;%dm ",
upperRed/256,
upperGreen/256,
upperBlue/256,
)
} else {
fmt.Printf(
"\x1b[48;2;%d;%d;%dm\x1b[38;2;%d;%d;%dm▄",
upperRed/256,
upperGreen/256,
upperBlue/256,
lowerRed/256,
lowerGreen/256,
lowerBlue/256,
)
}
}
fmt.Println("\x1b[0m")
}
}
func main() {
heightInt, widthInt, _ := pty.Getsize(os.Stdout)
var width uint
var height uint
// The three subtracted lines is to have room for command, file name and prompt after explosion
flag.UintVar(&width, "w", uint(widthInt), "Maximum width of output in number of columns")
flag.UintVar(&height, "h", uint((heightInt-3)*2), "Maximum height of output in number of half lines")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] [file | - ...]\n\n", os.Args[0])
fmt.Fprintln(os.Stderr, " Specify \"-\" or just nothing to read from stdin.")
fmt.Fprintln(os.Stderr)
fmt.Fprintln(os.Stderr, "Options:")
flag.PrintDefaults()
}
flag.Parse()
filenames := flag.Args()
if len(filenames) == 0 {
fmt.Println("stdin:")
sourceImage, _, err := image.Decode(os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
} else {
printImage(sourceImage, width, height)
}
} else {
for i, filename := range filenames {
if i > 0 {
fmt.Println()
}
var file *os.File
var err error
if filename == "-" {
fmt.Println("stdin:")
file = os.Stdin
} else {
fmt.Printf("%s:\n", filename)
file, err = os.Open(filename)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
continue
}
}
sourceImage, _, err := image.Decode(file)
_ = file.Close()
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
continue
}
printImage(sourceImage, width, height)
}
}
}