Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Jan 28, 2018
1 parent 26185c1 commit fba46d5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = 5.0.1
VERSION = 5.0.2

PACKAGES := $(shell go list -f {{.Dir}} ./...)
GOFILES := $(addsuffix /*.go,$(PACKAGES))
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Usage of tj:
the lower bound for the color scale (default 100ms)
-scale-slow duration
the upper bound for the color scale (default 2s)
-delta-buffer
buffer lines between -start matches, copy delta values from final line to buffered lines
```

### JSON output
Expand Down Expand Up @@ -109,7 +111,7 @@ $ (echo Hello; echo World) | tj -template '{{ .I }} {{.TimeSecs}} {{.Text}}'

The fields available to the template are specified in the [`line` struct](cmd/tj/main.go#L19).

Some templates are pre-defined and can be specified via `-template NAME`:
Some templates are pre-defined and can be used via `-template NAME`:

| Name | Template |
|------------|----------------------------------------------|
Expand Down
10 changes: 5 additions & 5 deletions cmd/tj/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ type configuration struct {
start string // -start="..."
readJSON bool // -readjson
jsonTemplate string // -jsontemplate="..."
colorScale string // -scale
fast time.Duration // -scale-fast
slow time.Duration // -scale-slow
buffer bool // -buffer
colorScale string // -scale="..."
fast time.Duration // -scale-fast="..."
slow time.Duration // -scale-slow="..."
buffer bool // -delta-buffer
version string
}

Expand Down Expand Up @@ -173,7 +173,7 @@ func init() {
config.colorScale = knownScale
}
if config.colorScale != "" {
scale = color.NewScale(color.Parse(config.colorScale))
scale = color.ParseScale(config.colorScale)
}
if config.template != "" {
printer = templatePrinter(config.template)
Expand Down
42 changes: 19 additions & 23 deletions pkg/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"strings"
)

// RGB is an RGB color
type RGB struct{ R, G, B uint8 }
type rgb struct{ r, g, b uint8 }

// Scale is a color scale
// Scale is a color scale, a function mapping [0,1] to rgb colors.
type Scale func(float64) (r, g, b uint8)

func index(r, g, b uint8) int {
Expand All @@ -33,32 +32,33 @@ func clamp(c float64) float64 {
var notHexChars = regexp.MustCompile("[^0-9a-fA-F]")
var spaces = regexp.MustCompile("\\s+")

func parse3(s string, c *RGB) {
func parse3(s string, c *rgb) {
r, _ := strconv.ParseUint(s[0:1], 16, 8)
c.R = uint8((r << 4) | r)
c.r = uint8((r << 4) | r)
g, _ := strconv.ParseUint(s[1:2], 16, 8)
c.G = uint8((g << 4) | g)
c.g = uint8((g << 4) | g)
b, _ := strconv.ParseUint(s[2:3], 16, 8)
c.B = uint8((b << 4) | b)
c.b = uint8((b << 4) | b)
}

func parse6(s string, c *RGB) {
func parse6(s string, c *rgb) {
r, _ := strconv.ParseUint(s[0:2], 16, 8)
c.R = uint8(r)
c.r = uint8(r)
g, _ := strconv.ParseUint(s[2:4], 16, 8)
c.G = uint8(g)
c.g = uint8(g)
b, _ := strconv.ParseUint(s[4:6], 16, 8)
c.B = uint8(b)
c.b = uint8(b)
}

func Parse(scale string) []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)
lowercase := strings.ToLower(trimmed)
parts := strings.Split(lowercase, " ")

colors := make([]RGB, len(parts))
colors := make([]rgb, len(parts))
for i, s := range parts {
switch len(s) {
case 3:
Expand All @@ -67,18 +67,20 @@ func Parse(scale string) []RGB {
parse6(s, &colors[i])
}
}
return colors
return func(c float64) (r, g, b uint8) {
return interpolate(c, colors)
}
}

func Interpolate2(c float64, r1, g1, b1, r2, g2, b2 uint8) (r, g, b uint8) {
func interpolate2(c float64, r1, g1, b1, r2, g2, b2 uint8) (r, g, b uint8) {
c = clamp(c)
r = uint8(float64(r1)*(1-c) + float64(r2)*c)
g = uint8(float64(g1)*(1-c) + float64(g2)*c)
b = uint8(float64(b1)*(1-c) + float64(b2)*c)
return
}

func Interpolate(c float64, points []RGB) (r, g, b uint8) {
func interpolate(c float64, points []rgb) (r, g, b uint8) {
c = clamp(c)
x := float64(len(points)-1) * c
i := int(x)
Expand All @@ -89,13 +91,7 @@ func Interpolate(c float64, points []RGB) (r, g, b uint8) {
}
right := points[j]
c = x - float64(i)
return Interpolate2(c, left.R, left.G, left.B, right.R, right.G, right.B)
}

func NewScale(points []RGB) Scale {
return func(c float64) (r, g, b uint8) {
return Interpolate(c, points)
}
return interpolate2(c, left.r, left.g, left.b, right.r, right.g, right.b)
}

// Foreground returns the closest matching terminal foreground color escape sequence
Expand Down

0 comments on commit fba46d5

Please sign in to comment.