Skip to content

Commit

Permalink
feat(prints): Added console output functions with color and confirmat…
Browse files Browse the repository at this point in the history
…ion support. (#88)

* feat(prints): Added prints

Signed-off-by: Flc゛ <[email protected]>

* feat(prints): Added prints

Signed-off-by: Flc゛ <[email protected]>

* feat(prints): Added prints

Signed-off-by: Flc゛ <[email protected]>

* feat(prints): Added prints

Signed-off-by: Flc゛ <[email protected]>

* feat(prints): Added prints

Signed-off-by: Flc゛ <[email protected]>

* feat(prints): Added prints

Signed-off-by: Flc゛ <[email protected]>

* feat(prints): Added prints

Signed-off-by: Flc゛ <[email protected]>

---------

Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 authored Feb 2, 2024
1 parent c73e765 commit 50f4a41
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 3 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/davecgh/go-spew v1.1.1
github.com/fatih/color v1.16.0
github.com/gin-gonic/gin v1.9.1
github.com/go-kratos/kratos/v2 v2.7.2
github.com/google/uuid v1.6.0
Expand Down Expand Up @@ -33,7 +34,8 @@ require (
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand Down Expand Up @@ -57,8 +59,11 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand Down Expand Up @@ -99,6 +104,7 @@ golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down
53 changes: 53 additions & 0 deletions prints/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Prints

## Usage

```go
package main

import (
"fmt"

"github.com/go-kratos-ecosystem/components/v2/prints"
)

func main() {
// basic
fmt.Println("Hello World!", "Hello Go!")
prints.Line("Line: Hello World!", "Hello Go!")
prints.Linef("Linef: %s %s\n", "Hello World!", "Hello Go!")
prints.NewLine()
println("----------------------------------------")
prints.NewLine(0)
println("----------------------------------------")
prints.NewLine(1)
println("----------------------------------------")
prints.NewLine(2)
println("----------------------------------------")
prints.NewLine(3)
prints.Info("Info: Hello World!", "Hello Go!")
prints.Infof("Infof: %s %s\n", "Hello World!", "Hello Go!")
prints.Comment("Comment: Hello World!", "Hello Go!")
prints.Commentf("Commentf: %s %s\n", "Hello World!", "Hello Go!")
prints.Error("Error: Hello World!", "Hello Go!")
prints.Errorf("Errorf: %s %s\n", "Hello World!", "Hello Go!")
prints.Warn("Warn: Hello World!", "Hello Go!")
prints.Warnf("Warnf: %s %s\n", "Hello World!", "Hello Go!")
prints.Alert("Alert: Hello World!", "Hello Go!")
prints.Alertf("Alertf: %s %s\n", "Hello World!", "Hello Go!")

// prompt
r1, _ := prints.Ask("What is your name?", "FLC")
if r1 == "FLC" {
prints.Warn("You are FLC!")
} else {
prints.Infof("You are %s!\n", r1)
}
r2, _ := prints.Ask("What is your name?")
prints.Info(r2)
}
```

Output:

![](output.jpg)
76 changes: 76 additions & 0 deletions prints/basic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package prints

import (
"strings"

"github.com/fatih/color"
)

var (
line = color.New()
info = color.New(color.FgGreen)
comment = color.New(color.FgYellow)
errs = color.New(color.FgRed)
warn = color.New(color.FgHiYellow)
alert = comment
)

func Line(a ...interface{}) (int, error) {
return line.Println(a...)
}

func Linef(format string, a ...interface{}) (int, error) {
return line.Printf(format, a...)
}

func NewLine(length ...int) (int, error) {
var brs []string

if len(length) > 0 {
for i := 0; i < length[0]-1; i++ {
brs = append(brs, "\n")
}
}

return line.Println(strings.Join(brs, ""), "")
}

func Info(a ...interface{}) (int, error) {
return info.Println(a...)
}

func Infof(format string, a ...interface{}) (int, error) {
return info.Printf(format, a...)
}

func Comment(a ...interface{}) (int, error) {
return comment.Println(a...)
}

func Commentf(format string, a ...interface{}) (int, error) {
return comment.Printf(format, a...)
}

func Error(a ...interface{}) (int, error) {
return errs.Println(a...)
}

func Errorf(format string, a ...interface{}) (int, error) {
return errs.Printf(format, a...)
}

func Warn(a ...interface{}) (int, error) {
return warn.Println(a...)
}

func Warnf(format string, a ...interface{}) (int, error) {
return warn.Printf(format, a...)
}

func Alert(a ...interface{}) (int, error) {
return alert.Println(a...)
}

func Alertf(format string, a ...interface{}) (int, error) {
return alert.Printf(format, a...)
}
19 changes: 19 additions & 0 deletions prints/basic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package prints

import "testing"

func TestInfo(*testing.T) {
Line("Line: Hello World!", "Hello Go!") //nolint:errcheck
Linef("Linef: %s %s\n", "Hello World!", "Hello Go!") //nolint:errcheck
NewLine() //nolint:errcheck
Info("Info: Hello World!", "Hello Go!") //nolint:errcheck
Infof("Infof: %s %s\n", "Hello World!", "Hello Go!") //nolint:errcheck
Comment("Comment: Hello World!", "Hello Go!") //nolint:errcheck
Commentf("Commentf: %s %s\n", "Hello World!", "Hello Go!") //nolint:errcheck
Error("Error: Hello World!", "Hello Go!") //nolint:errcheck
Errorf("Errorf: %s %s\n", "Hello World!", "Hello Go!") //nolint:errcheck
Warn("Warn: Hello World!", "Hello Go!") //nolint:errcheck
Warnf("Warnf: %s %s\n", "Hello World!", "Hello Go!") //nolint:errcheck
Alert("Alert: Hello World!", "Hello Go!") //nolint:errcheck
Alertf("Alertf: %s %s\n", "Hello World!", "Hello Go!") //nolint:errcheck
}
Binary file added prints/output.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions prints/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package prints

import (
"bufio"
"os"
"strings"
)

type Prompt struct {
question string

defaultAnswer string
secret bool // TODO: support secret
}

type PromptOption func(*Prompt)

func WithDefaultAnswer(defaultAnswer string) PromptOption {
return func(p *Prompt) {
p.defaultAnswer = defaultAnswer
}
}

func WithSecret() PromptOption {
return func(p *Prompt) {
p.secret = true
}
}

func NewPrompt(question string, opts ...PromptOption) *Prompt {
p := &Prompt{
question: question,
}

for _, opt := range opts {
opt(p)
}

return p
}

func (p *Prompt) Ask() (string, error) {
if _, err := Infof("%s ", p.question); err != nil {
return "", err
}
if p.defaultAnswer != "" {
if _, err := Alertf("[%s] ", p.defaultAnswer); err != nil {
return "", err
}
}
if _, err := Linef("\n> "); err != nil {
return "", err
}

reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return "", err
}
input = strings.TrimSpace(input)
if input == "" {
return p.defaultAnswer, nil
}
return input, nil
}

func Ask(question string, defaults ...string) (string, error) {
var opts []PromptOption
if len(defaults) > 0 {
opts = append(opts, WithDefaultAnswer(defaults[0]))
}

return NewPrompt(question, opts...).Ask()
}

0 comments on commit 50f4a41

Please sign in to comment.