Skip to content

Commit 70d0d2b

Browse files
committed
Add -version, refactor main, simplify color
1 parent f39a03a commit 70d0d2b

File tree

3 files changed

+84
-56
lines changed

3 files changed

+84
-56
lines changed

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = 5.0.2
1+
VERSION = 5.0.3
22

33
PACKAGES := $(shell go list -f {{.Dir}} ./...)
44
GOFILES := $(addsuffix /*.go,$(PACKAGES))
@@ -32,18 +32,18 @@ release/tj_$(VERSION)_osx_x86_64.zip: binaries/osx_x86_64/tj
3232
cd binaries/osx_x86_64 && zip -r -D ../../release/tj_$(VERSION)_osx_x86_64.zip tj
3333

3434
binaries/osx_x86_64/tj: $(GOFILES)
35-
GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.config.version=$(VERSION)" -o binaries/osx_x86_64/tj ./cmd/tj
35+
GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION)" -o binaries/osx_x86_64/tj ./cmd/tj
3636

3737
release/tj_$(VERSION)_windows_x86_64.zip: binaries/windows_x86_64/tj.exe
3838
mkdir -p release
3939
cd binaries/windows_x86_64 && zip -r -D ../../release/tj_$(VERSION)_windows_x86_64.zip tj.exe
4040

4141
binaries/windows_x86_64/tj.exe: $(GOFILES)
42-
GOOS=windows GOARCH=amd64 go build -ldflags "-X main.config.version=$(VERSION)" -o binaries/windows_x86_64/tj.exe ./cmd/tj
42+
GOOS=windows GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION)" -o binaries/windows_x86_64/tj.exe ./cmd/tj
4343

4444
release/tj_$(VERSION)_linux_x86_64.zip: binaries/linux_x86_64/tj
4545
mkdir -p release
4646
cd binaries/linux_x86_64 && zip -r -D ../../release/tj_$(VERSION)_linux_x86_64.zip tj
4747

4848
binaries/linux_x86_64/tj: $(GOFILES)
49-
GOOS=linux GOARCH=amd64 go build -ldflags "-X main.config.version=$(VERSION)" -o binaries/linux_x86_64/tj ./cmd/tj
49+
GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION)" -o binaries/linux_x86_64/tj ./cmd/tj

cmd/tj/main.go

Lines changed: 78 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,26 @@ type line struct {
3838
}
3939

4040
type configuration struct {
41-
timeFormat string // -timeformat="..."
42-
template string // -template="..."
43-
start string // -start="..."
44-
readJSON bool // -readjson
45-
jsonTemplate string // -jsontemplate="..."
46-
colorScale string // -scale="..."
47-
fast time.Duration // -scale-fast="..."
48-
slow time.Duration // -scale-slow="..."
49-
buffer bool // -delta-buffer
50-
version string
41+
timeFormat string // -timeformat="..."
42+
template string // -template="..."
43+
start string // -start="..."
44+
readJSON bool // -readjson
45+
jsonTemplate string // -jsontemplate="..."
46+
colorScale string // -scale="..."
47+
fast time.Duration // -scale-fast="..."
48+
slow time.Duration // -scale-slow="..."
49+
buffer bool // -delta-buffer
50+
printVersionAndExit bool // -version
5151
}
5252

5353
type printerFunc func(line *line) error
5454

5555
var (
56+
version string
5657
config configuration
5758
printer printerFunc
5859
start *regexp.Regexp
59-
jsonTemplate *template.Template
60+
jsonTemplate *templateWithBuffer
6061
scale color.Scale
6162
)
6263

@@ -162,7 +163,12 @@ func init() {
162163
flag.DurationVar(&config.fast, "scale-fast", 100*time.Millisecond, "the lower bound for the color scale")
163164
flag.DurationVar(&config.slow, "scale-slow", 2*time.Second, "the upper bound for the color scale")
164165
flag.BoolVar(&config.buffer, "delta-buffer", false, "buffer lines between -start matches, copy delta values from final line to buffered lines")
166+
flag.BoolVar(&config.printVersionAndExit, "version", false, "print version and exit")
165167
flag.Parse()
168+
if config.printVersionAndExit {
169+
fmt.Println(version)
170+
os.Exit(0)
171+
}
166172
if knownFormat, ok := timeFormats[config.timeFormat]; ok {
167173
config.timeFormat = knownFormat
168174
}
@@ -185,31 +191,60 @@ func init() {
185191
}
186192
if config.jsonTemplate != "" {
187193
config.readJSON = true
188-
jsonTemplate = template.Must(template.New("-jsontemplate").Option("missingkey=zero").Parse(config.jsonTemplate))
194+
jsonTemplate = &templateWithBuffer{
195+
template: template.Must(template.New("-jsontemplate").Option("missingkey=zero").Parse(config.jsonTemplate)),
196+
buffer: bytes.NewBuffer(nil),
197+
}
189198
}
190199
}
191200

192-
func flushLineBuffer(buffer *[]*line, line *line) {
193-
for _, oldLine := range *buffer {
201+
type lineBuffer []*line
202+
203+
func (b *lineBuffer) flush(line *line) {
204+
for i, oldLine := range *b {
194205
oldLine.DeltaSecs = line.DeltaSecs
195206
oldLine.DeltaNanos = line.DeltaNanos
196207
oldLine.DeltaString = line.DeltaString
197208
oldLine.Delta = line.Delta
198-
if err := printer(oldLine); err != nil {
199-
fmt.Fprintln(os.Stderr, "output error:", err)
200-
}
209+
oldLine.print()
210+
(*b)[i] = nil
211+
}
212+
*b = (*b)[:0]
213+
}
214+
215+
func (l *line) print() {
216+
if err := printer(l); err != nil {
217+
fmt.Fprintln(os.Stderr, "output error:", err)
218+
}
219+
}
220+
221+
func (l *line) parseJSON() {
222+
l.Object = new(interface{})
223+
if err := json.Unmarshal([]byte(l.Text), &l.Object); err != nil {
224+
fmt.Fprintln(os.Stderr, "JSON parse error:", err)
201225
}
202-
*buffer = (*buffer)[:0]
226+
}
227+
228+
type templateWithBuffer struct {
229+
template *template.Template
230+
buffer *bytes.Buffer
231+
}
232+
233+
func (t *templateWithBuffer) execute(data interface{}) string {
234+
t.buffer.Reset()
235+
if err := t.template.Execute(t.buffer, data); err != nil {
236+
fmt.Fprintln(os.Stderr, "template error:", err)
237+
}
238+
return t.buffer.String()
203239
}
204240

205241
func main() {
206242
scanner := bufio.NewScanner(os.Stdin)
207-
lineBuffer := []*line{}
243+
lineBuffer := lineBuffer{}
208244
line := line{Time: time.Now()}
209245
first := line.Time
210246
last := line.Time
211247
i := uint64(0)
212-
b := bytes.NewBuffer(nil)
213248
for scanner.Scan() {
214249
now := time.Now()
215250
delta := now.Sub(last)
@@ -228,47 +263,42 @@ func main() {
228263
line.Time = now
229264
line.Text = scanner.Text()
230265
line.I = i
231-
match := line.Text
266+
match := &line.Text
232267
if config.readJSON {
233-
line.Object = new(interface{})
234-
if err := json.Unmarshal([]byte(line.Text), &line.Object); err != nil {
235-
fmt.Fprintln(os.Stderr, "JSON parse error:", err)
236-
}
268+
line.parseJSON()
237269
if jsonTemplate != nil {
238-
b.Reset()
239-
if err := jsonTemplate.Execute(b, line.Object); err != nil {
240-
fmt.Fprintln(os.Stderr, "template error:", err)
241-
}
242-
line.JSONText = b.String()
243-
match = line.JSONText
270+
line.JSONText = jsonTemplate.execute(line.Object)
271+
match = &line.JSONText
244272
}
245273
}
246-
if !config.buffer {
247-
if err := printer(&line); err != nil {
248-
fmt.Fprintln(os.Stderr, "output error:", err)
249-
}
274+
275+
startDefined := start != nil
276+
printLine := !startDefined || !config.buffer
277+
startMatches := startDefined && start.MatchString(*match)
278+
resetStopwatch := !startDefined || startMatches
279+
280+
if printLine {
281+
line.print()
250282
}
251-
if start != nil {
252-
if start.MatchString(match) {
253-
if config.buffer {
254-
flushLineBuffer(&lineBuffer, &line)
255-
}
256-
last = now
257-
line.StartText = line.Text
258-
line.StartObject = line.Object
259-
}
283+
if startMatches {
284+
line.StartText = line.Text
285+
line.StartObject = line.Object
260286
if config.buffer {
261-
lineCopy := line
262-
lineBuffer = append(lineBuffer, &lineCopy)
287+
lineBuffer.flush(&line)
263288
}
264-
} else {
289+
}
290+
if !printLine {
291+
lineCopy := line
292+
lineBuffer = append(lineBuffer, &lineCopy)
293+
}
294+
if resetStopwatch {
265295
last = now
266296
}
267297
i++
268298
}
269299

270300
if config.buffer {
271-
flushLineBuffer(&lineBuffer, &line)
301+
lineBuffer.flush(&line)
272302
}
273303

274304
if err := scanner.Err(); err != nil {

pkg/color/color.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ func clamp(c float64) float64 {
2929
return c
3030
}
3131

32-
var notHexChars = regexp.MustCompile("[^0-9a-fA-F]")
33-
var spaces = regexp.MustCompile("\\s+")
32+
var notHexChars = regexp.MustCompile("[^0-9a-fA-F]+")
3433

3534
func parse3(s string, c *rgb) {
3635
r, _ := strconv.ParseUint(s[0:1], 16, 8)
@@ -53,8 +52,7 @@ func parse6(s string, c *rgb) {
5352
// ParseScale parses a sequence of hex colors as a Scale
5453
func ParseScale(scale string) Scale {
5554
hexOnly := notHexChars.ReplaceAllString(scale, " ")
56-
singleSpaced := spaces.ReplaceAllString(hexOnly, " ")
57-
trimmed := strings.TrimSpace(singleSpaced)
55+
trimmed := strings.TrimSpace(hexOnly)
5856
lowercase := strings.ToLower(trimmed)
5957
parts := strings.Split(lowercase, " ")
6058

0 commit comments

Comments
 (0)