summaryrefslogtreecommitdiffstats
path: root/cmd/jp/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/jp/main.go')
-rw-r--r--cmd/jp/main.go71
1 files changed, 55 insertions, 16 deletions
diff --git a/cmd/jp/main.go b/cmd/jp/main.go
index c603f68..07de252 100644
--- a/cmd/jp/main.go
+++ b/cmd/jp/main.go
@@ -8,24 +8,34 @@ import (
"os"
"reflect"
- "github.com/sgreben/jp/pkg/jp/primitives"
+ "github.com/sgreben/jp/pkg/draw"
"github.com/sgreben/jp/pkg/terminal"
"github.com/sgreben/jp/pkg/jsonpath"
)
type configuration struct {
- Box primitives.Box
- X string
- Y string
- XY string
- PlotType enumVar
+ Box draw.Box
+ X string
+ Y string
+ XY string
+ PlotType enumVar
+ CanvasType enumVar
}
-const plotTypeLine = "line"
-const plotTypeBar = "bar"
-const plotTypeScatter = "scatter"
-const plotTypeHist = "hist"
+const (
+ plotTypeLine = "line"
+ plotTypeBar = "bar"
+ plotTypeScatter = "scatter"
+ plotTypeHist = "hist"
+)
+
+const (
+ canvasTypeFull = "full"
+ canvasTypeQuarter = "quarter"
+ canvasTypeBraille = "braille"
+ canvasTypeAuto = "auto"
+)
var config = configuration{
PlotType: enumVar{
@@ -33,16 +43,29 @@ var config = configuration{
Choices: []string{
plotTypeLine,
plotTypeBar,
+ plotTypeScatter,
+ },
+ },
+ CanvasType: enumVar{
+ Value: canvasTypeAuto,
+ Choices: []string{
+ canvasTypeFull,
+ canvasTypeQuarter,
+ canvasTypeBraille,
+ canvasTypeAuto,
},
},
}
-var xPattern *jsonpath.JSONPath
-var yPattern *jsonpath.JSONPath
-var xyPattern *jsonpath.JSONPath
+var (
+ xPattern *jsonpath.JSONPath
+ yPattern *jsonpath.JSONPath
+ xyPattern *jsonpath.JSONPath
+)
func init() {
flag.Var(&config.PlotType, "type", fmt.Sprintf("Plot type. One of %v", config.PlotType.Choices))
+ flag.Var(&config.CanvasType, "canvas", fmt.Sprintf("Canvas type. One of %v", config.CanvasType.Choices))
flag.StringVar(&config.X, "x", "", "x values (JSONPath expression)")
flag.StringVar(&config.Y, "y", "", "y values (JSONPath expression)")
flag.StringVar(&config.XY, "xy", "", "x,y value pairs (JSONPath expression). Overrides -x and -y if given.")
@@ -75,6 +98,9 @@ func init() {
if config.Box.Height == 0 {
config.Box.Height = terminal.Height() - 1
}
+ if config.CanvasType.Value == canvasTypeAuto {
+ config.CanvasType.Value = autoCanvas[config.PlotType.Value]
+ }
}
func match(in interface{}, p *jsonpath.JSONPath) [][]reflect.Value {
@@ -100,11 +126,24 @@ func main() {
x = match(in, xPattern)
y = match(in, yPattern)
}
+ buffer := draw.NewBuffer(config.Box)
+ var p draw.Pixels
+ switch config.CanvasType.Value {
+ case canvasTypeBraille:
+ p = &draw.Braille{Buffer: buffer}
+ case canvasTypeQuarter:
+ p = &draw.Quarter{Buffer: buffer}
+ case canvasTypeFull:
+ p = &draw.Full{Buffer: buffer}
+ }
+ p.Clear()
+ c := draw.Canvas{Pixels: p}
switch config.PlotType.Value {
case plotTypeLine:
- fmt.Print(linePlot(x, y, config.Box))
+ fmt.Println(linePlot(x, y, c))
+ case plotTypeScatter:
+ fmt.Println(scatterPlot(x, y, c))
case plotTypeBar:
- fmt.Print(barPlot(x, y, config.Box))
+ fmt.Println(barPlot(x, y, c))
}
-
}