Commit d99e82fa authored by Eric Myhre's avatar Eric Myhre

First pass of very basic coloration; and demo.

Key coloration is easy because we already have key emission in one place,
and we already have size computation for alignment separated from emission.
Value coloration will be a little more involved.
parent d2c11dad
package jst
type Color struct {
Enabled bool
KeyHighlight []byte
PlainValue []byte
}
func (c *Color) initDefaults() {
if !c.Enabled {
return
}
if c.KeyHighlight == nil {
c.KeyHighlight = []byte("\033[32m")
}
if c.PlainValue == nil {
c.PlainValue = []byte("\033[1;34m")
}
}
package main
import (
"bytes"
"fmt"
"os"
"github.com/ipld/go-ipld-prime/codec/dagjson"
"github.com/ipld/go-ipld-prime/codec/jst"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
)
func main() {
fixture := `[
{"path": "./foo", "moduleName": "whiz.org/teamBar/foo", "status": "changed"},
{"path": "./baz", "moduleName": "whiz.org/teamBar/baz", "status": "green"},
{"path": "./quxx", "moduleName": "example.net/quxx", "status": "lit",
"subtable": [
{"widget": "shining", "property": "neat", "familiarity": 14},
{"widget": "shimmering", "property": "neat", "familiarity": 140},
{"widget": "scintillating", "familiarity": 0},
{"widget": "irridescent", "property": "yes"},
]}
]`
nb := basicnode.Style.Any.NewBuilder()
if err := dagjson.Decoder(nb, bytes.NewBufferString(fixture)); err != nil {
panic(err)
}
n := nb.Build()
if err := jst.MarshalConfigured(jst.Config{
Indent: []byte{' ', ' '},
Color: jst.Color{Enabled: true},
}, n, os.Stdout); err != nil {
fmt.Printf("\nerror: %s\n", err)
os.Exit(5)
}
fmt.Println()
}
...@@ -59,6 +59,20 @@ func Marshal(n ipld.Node, w io.Writer) error { ...@@ -59,6 +59,20 @@ func Marshal(n ipld.Node, w io.Writer) error {
return marshal(&ctx, n, w) return marshal(&ctx, n, w)
} }
func MarshalConfigured(cfg Config, n ipld.Node, w io.Writer) error {
ctx := state{
cfg: cfg,
}
ctx.cfg.Color.initDefaults()
// Stride first -- see how much spacing we need.
err := stride(&ctx, n)
if err != nil {
return err
}
// Marshal -- using the spacing nodes from our stride.
return marshal(&ctx, n, w)
}
type state struct { type state struct {
cfg Config cfg Config
path []ipld.PathSegment // TODO replace with PathBuffer... once you, you know, write it. path []ipld.PathSegment // TODO replace with PathBuffer... once you, you know, write it.
...@@ -92,6 +106,7 @@ const ( ...@@ -92,6 +106,7 @@ const (
type Config struct { type Config struct {
Indent []byte Indent []byte
Color Color
// FUTURE: selectors and other forms of specification can override where tables appear, what their tableGroupID is, and so on. // FUTURE: selectors and other forms of specification can override where tables appear, what their tableGroupID is, and so on.
// FUTURE: whether to emit trailing commas unconditionally, even on the last elements of maps and lists. // FUTURE: whether to emit trailing commas unconditionally, even on the last elements of maps and lists.
...@@ -452,10 +467,15 @@ func marshalListValue(ctx *state, tab *table, row ipld.Node, w io.Writer) error ...@@ -452,10 +467,15 @@ func marshalListValue(ctx *state, tab *table, row ipld.Node, w io.Writer) error
} }
func emitKey(ctx *state, k ipld.Node, w io.Writer) error { func emitKey(ctx *state, k ipld.Node, w io.Writer) error {
// FUTURE: ansi color goes around here too (hence the need to have ctx in hand). if ctx.cfg.Color.Enabled {
w.Write(ctx.cfg.Color.KeyHighlight)
}
if err := dagjson.Marshal(k, json.NewEncoder(w, json.EncodeOptions{})); err != nil { if err := dagjson.Marshal(k, json.NewEncoder(w, json.EncodeOptions{})); err != nil {
return recordErrorPosition(ctx, err) return recordErrorPosition(ctx, err)
} }
if ctx.cfg.Color.Enabled {
w.Write([]byte("\033[0m"))
}
w.Write([]byte{':'}) w.Write([]byte{':'})
w.Write([]byte{' '}) // FUTURE: this should be configurable w.Write([]byte{' '}) // FUTURE: this should be configurable
return nil return nil
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment