-
Notifications
You must be signed in to change notification settings - Fork 1
/
uniqrode.go
115 lines (92 loc) · 2.37 KB
/
uniqrode.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
/* Copyright (C) 2018 Dmitry Chirikov
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
package main
import (
"os"
"fmt"
"bufio"
"io"
"flag"
"strings"
ascii_mapper "github.com/dchirikov/uniqrode/ascii_mapper"
qrcode "github.com/skip2/go-qrcode"
)
var recovery_level_default = 2
var mode_default = 2
var inverse_default = false
var notrim_default = false
func showHelp() {
fmt.Println(
"Usage: uniqrode [-level=1..4] [-mode=1..3] [-inverse] [-notrim]")
}
// Read input from pipe stdin
func getInput(trim bool) string {
fi, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if fi.Mode() & os.ModeNamedPipe == 0 {
fmt.Println("Nothing to draw")
os.Exit(0)
}
reader := bufio.NewReader(os.Stdin)
var input []rune
for {
chunk, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
input = append(input, chunk)
}
if trim {
return strings.TrimSpace(string(input))
}
return string(input)
}
// Map to get recovery level for generated QR code from passes
// CLI argument
var recoveryLevels = map[int]qrcode.RecoveryLevel {
1: qrcode.Low,
2: qrcode.Medium,
3: qrcode.High,
4: qrcode.Highest,
}
func main() {
argNeedHelpPtr := flag.Bool("h", false, "help")
argRecoveryLevelPtr := flag.Int(
"level", recovery_level_default, "Recovery level")
argDrawModePtr := flag.Int(
"mode", mode_default, "Draw mode")
argInversePtr := flag.Bool(
"inverse", inverse_default, "Draw inverse")
argNoTrimPtr := flag.Bool(
"notrim", notrim_default,
"Do not trim leading and trailing white spaces")
// Parse arguments and check if they are valid
flag.Parse()
if *argNeedHelpPtr {
showHelp()
os.Exit(0)
}
var q *qrcode.QRCode
recoveryLevel, present := recoveryLevels[*argRecoveryLevelPtr];
if !present {
showHelp()
os.Exit(1)
}
// Create QR code
q, err := qrcode.New(getInput(!(*argNoTrimPtr)), recoveryLevel)
if err != nil {
panic (err)
}
// Draw ASCII picture
bits := q.Bitmap()
u, err := ascii_mapper.New(*argDrawModePtr, !(*argInversePtr), &bits)
if err != nil {
panic (err)
}
fmt.Printf("%s", u.Draw())
}