Skip to content

Commit

Permalink
up: show - update some show tool implement logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 2, 2022
1 parent b9fbf43 commit 189e253
Show file tree
Hide file tree
Showing 10 changed files with 424 additions and 118 deletions.
3 changes: 3 additions & 0 deletions show/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ go get github.com/gookit/gcli/v3/show

## Related

- https://github.com/jedib0t/go-pretty
- https://github.com/alexeyco/simpletable
- https://github.com/InVisionApp/tabular
- https://github.com/gosuri/uitable
- https://github.com/rodaine/table
- https://github.com/tomlazar/table
- https://github.com/nwidger/jsoncolor
76 changes: 54 additions & 22 deletions show/base.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package show

import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"strings"
"unicode/utf8"
Expand All @@ -19,15 +20,29 @@ const (
ERR = 2
)

// PosFlag type
type PosFlag = strutil.PosFlag

// some position constants
const (
PosLeft PosFlag = iota
PosRight
PosMiddle
)

// var errInvalidType = errors.New("invalid input data type")

// FormatterFace interface
type FormatterFace interface {
Format() string
Format()
}

// ShownFace shown interface
type ShownFace interface {
// io.WriterTo TODO
// Format()
// Buffer()

// String data to string
String() string
// Print print current message
Expand All @@ -38,45 +53,62 @@ type ShownFace interface {

// Base formatter
type Base struct {
output io.Writer
// TODO lock sync.Mutex
out io.Writer
// formatted string
formatted string
buf *bytes.Buffer
err error
}

// SetOutput for print message
func (b *Base) SetOutput(output io.Writer) {
b.output = output
func (b *Base) SetOutput(out io.Writer) {
b.out = out
}

// SetBuffer field
func (b *Base) SetBuffer(buf *bytes.Buffer) {
b.buf = buf
}

// Buffer get
func (b *Base) Buffer() *bytes.Buffer {
if b.buf == nil {
b.buf = new(bytes.Buffer)
}
return b.buf
}

// String format given data to string
func (b *Base) String() string {
panic("please implement the method")
}

// Format given data to string
func (b *Base) Format() string {
func (b *Base) Format() {
panic("please implement the method")
}

// Err get
func (b *Base) Err() error {
return b.err
}

// Print formatted message
func (b *Base) Print() {
if b.output == nil {
b.output = Output
if b.out == nil {
b.out = Output
}

if b.formatted != "" {
color.Fprint(b.output, b.formatted)
// clear data
b.formatted = ""
if b.buf != nil && b.buf.Len() > 0 {
color.Fprint(b.out, b.buf.String())
b.buf.Reset()
}
}

// Println formatted message and print newline
func (b *Base) Println() {
if b.output == nil {
b.output = os.Stdout
}

if b.formatted != "" {
color.Fprintln(b.output, b.formatted)
// clear data
b.formatted = ""
}
b.Print()
fmt.Println()
}

/*************************************************************
Expand Down
11 changes: 11 additions & 0 deletions show/json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package show

// PrettyJSON struct
type PrettyJSON struct {
Base
}

// NewPrettyJSON instance
func NewPrettyJSON() *PrettyJSON {
return &PrettyJSON{}
}
59 changes: 22 additions & 37 deletions show/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ type List struct {
title string
// list data. allow type: struct, slice, array, map
data any
// formatted data buffer
buffer *bytes.Buffer
}

// SetBuffer field
func (l *List) SetBuffer(buffer *bytes.Buffer) {
l.buffer = buffer
}

// NewList instance.
Expand All @@ -64,7 +57,7 @@ func NewList(title string, data any, fns ...ListOpFunc) *List {
title: title,
data: data,
// base
Base: Base{output: Output},
Base: Base{out: Output},
// options
Opts: &ListOption{
SepChar: " ",
Expand Down Expand Up @@ -96,18 +89,18 @@ func (l *List) WithOptions(fns ...ListOpFunc) *List {
}

// Format as string
func (l *List) Format() string {
if l.data == nil || l.formatted != "" {
return l.formatted
func (l *List) Format() {
if l.data == nil {
return
}

if l.buffer == nil {
l.buffer = new(bytes.Buffer)
if l.buf == nil {
l.buf = new(bytes.Buffer)
}

if l.title != "" { // has title
title := strutil.UpperWord(l.title)
l.buffer.WriteString(color.WrapTag(title, l.Opts.TitleStyle) + "\n")
l.buf.WriteString(color.WrapTag(title, l.Opts.TitleStyle) + "\n")
}

items := NewItems(l.data) // build items
Expand All @@ -126,14 +119,14 @@ func (l *List) Format() string {
}

if l.Opts.LeftIndent != "" {
l.buffer.WriteString(l.Opts.LeftIndent)
l.buf.WriteString(l.Opts.LeftIndent)
}

// format key - parsed from map, struct
if items.itemType == ItemMap {
key := strutil.PadRight(item.Key, " ", keyWidth)
key = color.WrapTag(key, l.Opts.KeyStyle)
l.buffer.WriteString(key + l.Opts.SepChar)
l.buf.WriteString(key + l.Opts.SepChar)
}

// format value
Expand All @@ -142,34 +135,32 @@ func (l *List) Format() string {
f.Indent = mlIndent
f.ClosePrefix = " "
// f.AfterReset = true
f.SetOutput(l.buffer)
f.SetOutput(l.buf)
}).Format()
l.buffer.WriteByte('\n')
l.buf.WriteByte('\n')
} else if item.Kind() == reflect.Map {
maputil.NewFormatter(item.rftVal).WithFn(func(f *maputil.MapFormatter) {
f.Indent = mlIndent
f.ClosePrefix = " "
// f.AfterReset = true
f.SetOutput(l.buffer)
f.SetOutput(l.buf)
}).Format()
l.buffer.WriteByte('\n')
l.buf.WriteByte('\n')
} else {
val := item.ValString()
if l.Opts.UpperFirst {
val = strutil.UpperFirst(val)
}
l.buffer.WriteString(val + "\n")
l.buf.WriteString(val + "\n")
}

}

l.formatted = l.buffer.String()
return l.formatted
}

// String returns formatted string
func (l *List) String() string {
return l.Format()
l.Format()
return l.buf.String()
}

// Print formatted message
Expand All @@ -187,8 +178,6 @@ func (l *List) Println() {
// Flush formatted message to console
func (l *List) Flush() {
l.Println()
l.buffer.Reset()
l.formatted = ""
}

/*************************************************************
Expand All @@ -208,7 +197,7 @@ type Lists struct {
// NewLists create lists
func NewLists(listMap map[string]any, fns ...ListOpFunc) *Lists {
ls := &Lists{
Base: Base{output: Output},
Base: Base{out: Output},
Opts: &ListOption{
SepChar: " ",
KeyStyle: "info",
Expand Down Expand Up @@ -240,9 +229,9 @@ func (ls *Lists) WithOptions(fns ...ListOpFunc) *Lists {
}

// Format as string
func (ls *Lists) Format() string {
if len(ls.rows) == 0 || ls.formatted != "" {
return ls.formatted
func (ls *Lists) Format() {
if len(ls.rows) == 0 {
return
}

ls.buffer = new(bytes.Buffer)
Expand All @@ -252,14 +241,12 @@ func (ls *Lists) Format() string {
list.SetBuffer(ls.buffer)
list.Format()
}

ls.formatted = ls.buffer.String()
return ls.formatted
}

// String returns formatted string
func (ls *Lists) String() string {
return ls.Format()
ls.Format()
return ls.buf.String()
}

// Print formatted message
Expand All @@ -277,6 +264,4 @@ func (ls *Lists) Println() {
// Flush formatted message to console
func (ls *Lists) Flush() {
ls.Println()
ls.buffer.Reset()
ls.formatted = ""
}
2 changes: 1 addition & 1 deletion show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/gookit/color"
)

// Output the global input output stream
// Output the global input out stream
var Output io.Writer = os.Stdout

// SetOutput stream
Expand Down
40 changes: 0 additions & 40 deletions show/table.go

This file was deleted.

Loading

0 comments on commit 189e253

Please sign in to comment.