-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prints): Added console output functions with color and confirmat…
…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
Showing
7 changed files
with
233 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |