Skip to content

Commit

Permalink
Add -version, refactor main, simplify color
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Jan 28, 2018
1 parent f39a03a commit 70d0d2b
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 56 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = 5.0.2
VERSION = 5.0.3

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

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

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

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

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

binaries/linux_x86_64/tj: $(GOFILES)
GOOS=linux GOARCH=amd64 go build -ldflags "-X main.config.version=$(VERSION)" -o binaries/linux_x86_64/tj ./cmd/tj
GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=$(VERSION)" -o binaries/linux_x86_64/tj ./cmd/tj
126 changes: 78 additions & 48 deletions cmd/tj/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,26 @@ type line struct {
}

type configuration struct {
timeFormat string // -timeformat="..."
template string // -template="..."
start string // -start="..."
readJSON bool // -readjson
jsonTemplate string // -jsontemplate="..."
colorScale string // -scale="..."
fast time.Duration // -scale-fast="..."
slow time.Duration // -scale-slow="..."
buffer bool // -delta-buffer
version string
timeFormat string // -timeformat="..."
template string // -template="..."
start string // -start="..."
readJSON bool // -readjson
jsonTemplate string // -jsontemplate="..."
colorScale string // -scale="..."
fast time.Duration // -scale-fast="..."
slow time.Duration // -scale-slow="..."
buffer bool // -delta-buffer
printVersionAndExit bool // -version
}

type printerFunc func(line *line) error

var (
version string
config configuration
printer printerFunc
start *regexp.Regexp
jsonTemplate *template.Template
jsonTemplate *templateWithBuffer
scale color.Scale
)

Expand Down Expand Up @@ -162,7 +163,12 @@ func init() {
flag.DurationVar(&config.fast, "scale-fast", 100*time.Millisecond, "the lower bound for the color scale")
flag.DurationVar(&config.slow, "scale-slow", 2*time.Second, "the upper bound for the color scale")
flag.BoolVar(&config.buffer, "delta-buffer", false, "buffer lines between -start matches, copy delta values from final line to buffered lines")
flag.BoolVar(&config.printVersionAndExit, "version", false, "print version and exit")
flag.Parse()
if config.printVersionAndExit {
fmt.Println(version)
os.Exit(0)
}
if knownFormat, ok := timeFormats[config.timeFormat]; ok {
config.timeFormat = knownFormat
}
Expand All @@ -185,31 +191,60 @@ func init() {
}
if config.jsonTemplate != "" {
config.readJSON = true
jsonTemplate = template.Must(template.New("-jsontemplate").Option("missingkey=zero").Parse(config.jsonTemplate))
jsonTemplate = &templateWithBuffer{
template: template.Must(template.New("-jsontemplate").Option("missingkey=zero").Parse(config.jsonTemplate)),
buffer: bytes.NewBuffer(nil),
}
}
}

func flushLineBuffer(buffer *[]*line, line *line) {
for _, oldLine := range *buffer {
type lineBuffer []*line

func (b *lineBuffer) flush(line *line) {
for i, oldLine := range *b {
oldLine.DeltaSecs = line.DeltaSecs
oldLine.DeltaNanos = line.DeltaNanos
oldLine.DeltaString = line.DeltaString
oldLine.Delta = line.Delta
if err := printer(oldLine); err != nil {
fmt.Fprintln(os.Stderr, "output error:", err)
}
oldLine.print()
(*b)[i] = nil
}
*b = (*b)[:0]
}

func (l *line) print() {
if err := printer(l); err != nil {
fmt.Fprintln(os.Stderr, "output error:", err)
}
}

func (l *line) parseJSON() {
l.Object = new(interface{})
if err := json.Unmarshal([]byte(l.Text), &l.Object); err != nil {
fmt.Fprintln(os.Stderr, "JSON parse error:", err)
}
*buffer = (*buffer)[:0]
}

type templateWithBuffer struct {
template *template.Template
buffer *bytes.Buffer
}

func (t *templateWithBuffer) execute(data interface{}) string {
t.buffer.Reset()
if err := t.template.Execute(t.buffer, data); err != nil {
fmt.Fprintln(os.Stderr, "template error:", err)
}
return t.buffer.String()
}

func main() {
scanner := bufio.NewScanner(os.Stdin)
lineBuffer := []*line{}
lineBuffer := lineBuffer{}
line := line{Time: time.Now()}
first := line.Time
last := line.Time
i := uint64(0)
b := bytes.NewBuffer(nil)
for scanner.Scan() {
now := time.Now()
delta := now.Sub(last)
Expand All @@ -228,47 +263,42 @@ func main() {
line.Time = now
line.Text = scanner.Text()
line.I = i
match := line.Text
match := &line.Text
if config.readJSON {
line.Object = new(interface{})
if err := json.Unmarshal([]byte(line.Text), &line.Object); err != nil {
fmt.Fprintln(os.Stderr, "JSON parse error:", err)
}
line.parseJSON()
if jsonTemplate != nil {
b.Reset()
if err := jsonTemplate.Execute(b, line.Object); err != nil {
fmt.Fprintln(os.Stderr, "template error:", err)
}
line.JSONText = b.String()
match = line.JSONText
line.JSONText = jsonTemplate.execute(line.Object)
match = &line.JSONText
}
}
if !config.buffer {
if err := printer(&line); err != nil {
fmt.Fprintln(os.Stderr, "output error:", err)
}

startDefined := start != nil
printLine := !startDefined || !config.buffer
startMatches := startDefined && start.MatchString(*match)
resetStopwatch := !startDefined || startMatches

if printLine {
line.print()
}
if start != nil {
if start.MatchString(match) {
if config.buffer {
flushLineBuffer(&lineBuffer, &line)
}
last = now
line.StartText = line.Text
line.StartObject = line.Object
}
if startMatches {
line.StartText = line.Text
line.StartObject = line.Object
if config.buffer {
lineCopy := line
lineBuffer = append(lineBuffer, &lineCopy)
lineBuffer.flush(&line)
}
} else {
}
if !printLine {
lineCopy := line
lineBuffer = append(lineBuffer, &lineCopy)
}
if resetStopwatch {
last = now
}
i++
}

if config.buffer {
flushLineBuffer(&lineBuffer, &line)
lineBuffer.flush(&line)
}

if err := scanner.Err(); err != nil {
Expand Down
6 changes: 2 additions & 4 deletions pkg/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func clamp(c float64) float64 {
return c
}

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

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

Expand Down

0 comments on commit 70d0d2b

Please sign in to comment.