Skip to content

Commit

Permalink
up: show - update the show logic, add global output
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 4, 2022
1 parent 98ffe1a commit c4106f3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion show/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (b *Base) Format() string {
// Print formatted message
func (b *Base) Print() {
if b.output == nil {
b.output = os.Stdout
b.output = Output
}

if b.formatted != "" {
Expand Down
4 changes: 2 additions & 2 deletions show/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package show

import (
"bytes"
"os"
"reflect"

"github.com/gookit/color"
Expand Down Expand Up @@ -65,7 +64,7 @@ func NewList(title string, data interface{}, fns ...ListOpFunc) *List {
title: title,
data: data,
// base
Base: Base{output: os.Stdout},
Base: Base{output: Output},
// options
Opts: &ListOption{
SepChar: " ",
Expand Down Expand Up @@ -209,6 +208,7 @@ type Lists struct {
// NewLists create lists
func NewLists(listMap map[string]interface{}, fns ...ListOpFunc) *Lists {
ls := &Lists{
Base: Base{output: Output},
Opts: &ListOption{
SepChar: " ",
KeyStyle: "info",
Expand Down
34 changes: 22 additions & 12 deletions show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,45 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"text/tabwriter"

"github.com/gookit/color"
)

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

// SetOutput stream
func SetOutput(out io.Writer) { Output = out }

// ResetOutput stream
func ResetOutput() { Output = os.Stdout }

// Error tips message print
func Error(format string, v ...interface{}) int {
color.Red.Print("ERROR: ")
fmt.Printf(format+"\n", v...)
prefix := color.Red.Sprint("ERROR: ")
_, _ = fmt.Fprintf(Output, prefix+format+"\n", v...)
return ERR
}

// Success tips message print
func Success(format string, v ...interface{}) int {
color.Green.Print("SUCCESS: ")
fmt.Printf(format+"\n", v...)
prefix := color.Green.Sprint("SUCCESS: ")
_, _ = fmt.Fprintf(Output, prefix+format+"\n", v...)
return OK
}

// JSON print pretty JSON data
func JSON(v interface{}, settings ...string) int {
func JSON(v interface{}, prefixAndIndent ...string) int {
prefix := ""
indent := " "

l := len(settings)
l := len(prefixAndIndent)
if l > 0 {
prefix = settings[0]
prefix = prefixAndIndent[0]
if l > 1 {
indent = settings[1]
indent = prefixAndIndent[1]
}
}

Expand All @@ -42,7 +52,7 @@ func JSON(v interface{}, settings ...string) int {
panic(err)
}

fmt.Println(string(bs))
_, _ = fmt.Fprintln(Output, string(bs))
return OK
}

Expand Down Expand Up @@ -72,13 +82,13 @@ func MList(listMap map[string]interface{}, fns ...ListOpFunc) {
//
// Usage:
//
// w := TabWriter(os.Stdout, []string{
// w := TabWriter([]string{
// "a\tb\tc\td\t.",
// "123\t12345\t1234567\t123456789\t."
// })
// w.Flush()
func TabWriter(outTo io.Writer, rows []string) *tabwriter.Writer {
w := tabwriter.NewWriter(outTo, 0, 4, 2, ' ', tabwriter.Debug)
func TabWriter(rows []string) *tabwriter.Writer {
w := tabwriter.NewWriter(Output, 0, 4, 2, ' ', tabwriter.Debug)

for _, row := range rows {
if _, err := fmt.Fprintln(w, row); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions show/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package show_test

import (
"fmt"
"os"
"testing"

"github.com/gookit/gcli/v3/show"
Expand Down Expand Up @@ -73,7 +72,7 @@ func TestTabWriter(t *testing.T) {
"aaaa\tbbbb\taligned\t",
}

err := show.TabWriter(os.Stdout, ss).Flush()
err := show.TabWriter(ss).Flush()
is.NoErr(err)
}

Expand Down
17 changes: 12 additions & 5 deletions show/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ import "io"

// Writer definition
type Writer struct {
output io.Writer
// buf bytes.Buffer
out io.Writer
}

// NewWriter create a new writer
func NewWriter(output io.Writer) *Writer {
return &Writer{}
if output == nil {
output = Output
}

return &Writer{
out: output,
}
}

// Write
func (w *Writer) Write(buf []byte) (n int, err error) {
return
// Write bytes message
func (w *Writer) Write(p []byte) (n int, err error) {
return w.out.Write(p)
}

// Print data to io.Writer
Expand Down

0 comments on commit c4106f3

Please sign in to comment.