This repository was archived by the owner on Aug 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
166 lines (142 loc) · 4.55 KB
/
config.go
File metadata and controls
166 lines (142 loc) · 4.55 KB
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
// This file is subject to a 1-clause BSD license.
// Its contents can be found in the enclosed LICENSE file.
package main
import (
"bytes"
"flag"
"fmt"
"image/color"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
)
// Config defines application configuration.
type Config struct {
Output string // Output file name.
Columns int // Number of 7-bit columns to use.
Spacing int // Spacing between columns.
Border int // Numer of rows in optional top/bottom borders.
StitchWidth int // Stitch width, in pixels.
StitchHeight int // Stitch height, in pixels.
Colors [2]color.NRGBA // Foreground and background colors.
}
func (c *Config) BorderWidth() int { return c.Border * c.StitchWidth }
func (c *Config) BorderHeight() int { return c.Border * c.StitchHeight }
func (c *Config) SpacingWidth() int { return c.Spacing * c.StitchWidth }
func (c *Config) SpacingHeight() int { return c.Spacing * c.StitchHeight }
func (c *Config) ColumnWidth() int { return 7 * c.StitchWidth }
// ParseConfig parses and validates command line arguments into a config
// struct. This function prints an error and exits the program if invalid
// data is encountered.
//
// Additionally, it returns the text contents. Read either from a file, or
// stdin.
func ParseConfig() (*Config, []byte) {
var c Config
c.Columns = 3
c.Spacing = 2
c.Border = 2
c.Output = "out.png"
c.StitchWidth = 2
c.StitchHeight = 3
clrA := "0xffffff"
clrB := "0x647384"
repeat := 1
flag.Usage = func() {
fmt.Println("usage:", os.Args[0], "[options] <textfile>")
fmt.Println(" or: cat <textfile> |", os.Args[0], "[options]")
flag.PrintDefaults()
}
flag.StringVar(&c.Output, "out", c.Output, "Filename for resulting PNG image.")
flag.StringVar(&clrA, "color-a", clrA, "Foreground color in 24-bit hexadecimal notation.")
flag.StringVar(&clrB, "color-b", clrB, "Background color in 24-bit hexadecimal notation.")
flag.IntVar(&c.StitchWidth, "stitch-width", c.StitchWidth, "Width of a single stitch, in pixels: [1..n]")
flag.IntVar(&c.StitchHeight, "stitch-height", c.StitchHeight, "Height of a single stitch, in pixels: [1..n]")
flag.IntVar(&c.Columns, "columns", c.Columns, "Number of 7-bit columns to generate: [1..n]")
flag.IntVar(&c.Spacing, "spacing", c.Spacing, "Number of stitches to leave blank between columns: [0..n]")
flag.IntVar(&c.Border, "border", c.Border, "Optional decorative n-row border at top and bottom of work: [0..n]")
flag.IntVar(&repeat, "repeat", repeat, "Number of times to repeat the input text: [1..n]")
version := flag.Bool("version", false, "Display version information.")
flag.Parse()
if *version {
fmt.Println(Version())
os.Exit(0)
}
c.Output = filepath.Clean(c.Output)
if len(c.Output) == 0 || c.Columns < 1 || c.Spacing < 0 || c.Border < 0 ||
c.StitchWidth < 1 || c.StitchHeight < 1 || repeat < 1 {
flag.Usage()
os.Exit(1)
}
err := parseColor(&c.Colors[0], clrA)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
err = parseColor(&c.Colors[1], clrB)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if flag.NArg() == 0 {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return &c, filterText(data, repeat)
}
data, err := ioutil.ReadFile(filepath.Clean(flag.Arg(0)))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return &c, filterText(data, repeat)
}
var (
bCR = []byte{'\r'}
bLF = []byte{'\n'}
bTab = []byte{'\t'}
bSpace = []byte{' '}
b2Space = []byte{' ', ' '}
)
// filterText returns n times the given text, with excessive
// whitespace and formatting removed.
func filterText(v []byte, n int) []byte {
v = bytes.Replace(v, bCR, nil, -1)
v = bytes.Replace(v, bLF, bSpace, -1)
v = bytes.Replace(v, bTab, bSpace, -1)
v = bytes.Replace(v, b2Space, bSpace, -1)
v = bytes.TrimSpace(v)
if len(v) == 0 {
fmt.Fprintln(os.Stderr, "input text is empty after filtering")
os.Exit(1)
}
return bytes.Repeat(v, n)
}
func parseColor(out *color.NRGBA, v string) error {
pickError := func(a, b, c error) error {
if a != nil {
return a
}
if b != nil {
return b
}
return c
}
v = strings.ToLower(v)
if len(v) != 8 || !strings.HasPrefix(v, "0x") {
return fmt.Errorf("invalid or missing color value: %q", v)
}
v = v[2:]
r, er := strconv.ParseUint(v[:2], 16, 8)
g, eg := strconv.ParseUint(v[2:4], 16, 8)
b, eb := strconv.ParseUint(v[4:], 16, 8)
out.R = byte(r)
out.G = byte(g)
out.B = byte(b)
out.A = 0xff
return pickError(er, eg, eb)
}