Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled JSON and ZSON color by default #5114

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion zio/jsonio/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (w *Writer) punc(b byte) {
}

func (w *Writer) writeColor(b []byte, code []byte) {
if w.tab > 0 && color.Enabled {
if color.Enabled {
w.writer.Write(code)
defer w.writer.WriteString(color.Reset.String())
}
Expand Down
2 changes: 1 addition & 1 deletion zio/lakeio/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Writer struct {
func NewWriter(w io.WriteCloser, opts WriterOpts) *Writer {
writer := &Writer{
writer: w,
zson: zson.NewFormatter(0, nil),
zson: zson.NewFormatter(0, false, nil),
commits: make(table),
branches: make(map[ksuid.KSUID][]string),
width: 80, //XXX
Expand Down
7 changes: 4 additions & 3 deletions zio/zsonio/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ type Writer struct {
}

type WriterOpts struct {
Pretty int
Persist *regexp.Regexp
ColorDisabled bool
Pretty int
Persist *regexp.Regexp
}

func NewWriter(w io.WriteCloser, opts WriterOpts) *Writer {
return &Writer{
formatter: zson.NewFormatter(opts.Pretty, opts.Persist),
formatter: zson.NewFormatter(opts.Pretty, opts.ColorDisabled, opts.Persist),
writer: w,
}
}
Expand Down
48 changes: 25 additions & 23 deletions zson/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ import (
)

type Formatter struct {
typedefs map[string]*zed.TypeNamed
permanent map[string]*zed.TypeNamed
persist *regexp.Regexp
tab int
newline string
builder strings.Builder
stack []strings.Builder
implied map[zed.Type]bool
colors color.Stack
}

func NewFormatter(pretty int, persist *regexp.Regexp) *Formatter {
typedefs map[string]*zed.TypeNamed
permanent map[string]*zed.TypeNamed
persist *regexp.Regexp
tab int
newline string
builder strings.Builder
stack []strings.Builder
implied map[zed.Type]bool
colors color.Stack
colorDisabled bool
}

func NewFormatter(pretty int, colorDisabled bool, persist *regexp.Regexp) *Formatter {
Comment on lines +26 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be clearer with color bool instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this is that color = true wouldn't necessarily mean color is enabled since it depends when the writer is outputting to a terminal.

Copy link
Member

@nwt nwt Apr 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. To make that work we'd need to get rid of pkg/terminal/color.Enabled. Let's do that later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed I think we should get rid of that but yeah, later.

var newline string
if pretty > 0 {
newline = "\n"
Expand All @@ -35,12 +36,13 @@ func NewFormatter(pretty int, persist *regexp.Regexp) *Formatter {
permanent = make(map[string]*zed.TypeNamed)
}
return &Formatter{
typedefs: make(map[string]*zed.TypeNamed),
permanent: permanent,
tab: pretty,
newline: newline,
implied: make(map[zed.Type]bool),
persist: persist,
typedefs: make(map[string]*zed.TypeNamed),
permanent: permanent,
tab: pretty,
newline: newline,
implied: make(map[zed.Type]bool),
persist: persist,
colorDisabled: colorDisabled,
}
}

Expand Down Expand Up @@ -75,7 +77,7 @@ func (f *Formatter) FormatRecord(rec zed.Value) string {
}

func FormatValue(val zed.Value) string {
return NewFormatter(0, nil).Format(val)
return NewFormatter(0, true, nil).Format(val)
}

func String(p interface{}) string {
Expand Down Expand Up @@ -661,7 +663,7 @@ var colors = map[zed.Type]color.Code{
}

func (f *Formatter) startColorPrimitive(typ zed.Type) {
if f.tab > 0 {
if !f.colorDisabled {
c, ok := colors[zed.TypeUnder(typ)]
if !ok {
c = color.Reset
Expand All @@ -671,13 +673,13 @@ func (f *Formatter) startColorPrimitive(typ zed.Type) {
}

func (f *Formatter) startColor(code color.Code) {
if f.tab > 0 {
if !f.colorDisabled {
f.colors.Start(&f.builder, code)
}
}

func (f *Formatter) endColor() {
if f.tab > 0 {
if !f.colorDisabled {
f.colors.End(&f.builder)
}
}
Expand Down Expand Up @@ -830,7 +832,7 @@ func formatPrimitive(b *strings.Builder, typ zed.Type, bytes zcode.Bytes) {
}

func FormatTypeValue(tv zcode.Bytes) string {
f := NewFormatter(0, nil)
f := NewFormatter(0, true, nil)
f.formatTypeValue(0, tv)
return f.builder.String()
}
2 changes: 1 addition & 1 deletion zson/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func NewMarshaler() *MarshalContext {
func NewMarshalerIndent(indent int) *MarshalContext {
return &MarshalContext{
MarshalZNGContext: NewZNGMarshaler(),
formatter: NewFormatter(indent, nil),
formatter: NewFormatter(indent, false, nil),
}
}

Expand Down
Loading