-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquirky.go
141 lines (123 loc) · 2.47 KB
/
quirky.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
qrcode "github.com/skip2/go-qrcode"
)
var doubleSize = flag.Bool("d", false, "Make QR code double size")
var inverted = flag.Bool("i", false, "Invert colors")
func normal() {
if *inverted {
fmt.Print("\x1b[0m")
} else {
fmt.Print("\x1b[7m")
}
}
func invert() {
if *inverted {
fmt.Print("\x1b[7m")
} else {
fmt.Print("\x1b[0m")
}
}
func printCode(bitmap [][]bool) {
width := len(bitmap)
height := len(bitmap[0])
for y := 0; y < width; y += 2 {
lastInverted := false
if !*inverted {
lastInverted = true
normal()
}
for x := 0; x < height; x++ {
upper := bitmap[y][x]
var lower bool
if y+1 < width {
lower = bitmap[y+1][x]
} else {
lower = false
}
if upper == lower {
if upper && !lastInverted {
invert()
lastInverted = true
} else if !upper && lastInverted {
normal()
lastInverted = false
}
fmt.Print(" ")
} else {
if upper && !lastInverted {
invert()
lastInverted = true
} else if !upper && lastInverted {
normal()
lastInverted = false
}
fmt.Print("▄")
}
}
fmt.Println("\x1b[0m")
}
}
func printCodeDouble(bitmap [][]bool) {
width := len(bitmap)
height := len(bitmap[0])
for y := 0; y < width; y++ {
lastInverted := false
if !*inverted {
lastInverted = true
normal()
}
for x := 0; x < height; x++ {
if bitmap[y][x] && !lastInverted {
invert()
lastInverted = true
} else if !bitmap[y][x] && lastInverted {
normal()
lastInverted = false
}
fmt.Print(" ")
}
fmt.Println("\x1b[0m")
}
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] [text | -]\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()
data := flag.Args()
var dataString string
if len(data) > 1 {
flag.Usage()
os.Exit(1)
} else if len(data) == 0 || data[0] == "-" {
input := bufio.NewReader(os.Stdin)
line, _, _ := input.ReadLine()
dataString = strings.TrimSpace(string(line[:]))
} else {
dataString = strings.TrimSpace(data[0])
}
if len(dataString) == 0 {
fmt.Println("No data was provided")
os.Exit(1)
}
qr, err := qrcode.New(dataString, qrcode.Low)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if *doubleSize {
printCodeDouble(qr.Bitmap())
} else {
printCode(qr.Bitmap())
}
}