Skip to content

Commit

Permalink
✨ feat: add new pkg colorp for quickly print message
Browse files Browse the repository at this point in the history
- add ci tests on go1.10
  • Loading branch information
inhere committed Mar 20, 2023
1 parent fff8921 commit 6037df8
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 8 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
name: action-tests
on:
pull_request:
paths:
- 'go.mod'
- '**.go'
- '**.yml'
push:
paths:
- '**.go'
Expand All @@ -14,7 +19,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
go_version: [1.16, 1.17, 1.18, 1.19]
go_version: [1.16, 1.17, 1.18, 1.19, '1.20']
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ Check out these projects, which use https://github.com/gookit/color :
- [xo/terminfo](https://github.com/xo/terminfo)
- [beego/bee](https://github.com/beego/bee)
- [issue9/term](https://github.com/issue9/term)
- [muesli/termenv](https://github.com/muesli/termenv)
- [ANSI escape code](https://en.wikipedia.org/wiki/ANSI_escape_code)
- [Standard ANSI color map](https://conemu.github.io/en/AnsiEscapeCodes.html#Standard_ANSI_color_map)
- [Terminal Colors](https://gist.github.com/XVilka/8346728)
Expand Down
1 change: 1 addition & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ const (
## 参考项目

- [inhere/console](https://github.com/inhere/php-console)
- [muesli/termenv](https://github.com/muesli/termenv)
- [xo/terminfo](https://github.com/xo/terminfo)
- [beego/bee](https://github.com/beego/bee)
- [issue9/term](https://github.com/issue9/term)
Expand Down
6 changes: 6 additions & 0 deletions any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !go1.18
// +build !go1.18

package color

type any = interface{}
6 changes: 6 additions & 0 deletions colorp/any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !go1.18
// +build !go1.18

package colorp

type any = interface{}
111 changes: 111 additions & 0 deletions colorp/colorp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Package colorp provide some functions for quick print colored text.
package colorp

import "github.com/gookit/color"

/*************************************************************
* quick use color print message
*************************************************************/

// Redp print message with Red color
func Redp(a ...any) { color.Red.Print(a...) }

// Redf print message with Red color
func Redf(format string, a ...any) { color.Red.Printf(format, a...) }

// Redln print message line with Red color
func Redln(a ...any) { color.Red.Println(a...) }

// Bluep print message with Blue color
func Bluep(a ...any) { color.Blue.Print(a...) }

// Bluef print message with Blue color
func Bluef(format string, a ...any) { color.Blue.Printf(format, a...) }

// Blueln print message line with Blue color
func Blueln(a ...any) { color.Blue.Println(a...) }

// Cyanp print message with Cyan color
func Cyanp(a ...any) { color.Cyan.Print(a...) }

// Cyanf print message with Cyan color
func Cyanf(format string, a ...any) { color.Cyan.Printf(format, a...) }

// Cyanln print message line with Cyan color
func Cyanln(a ...any) { color.Cyan.Println(a...) }

// Grayp print message with Gray color
func Grayp(a ...any) { color.Gray.Print(a...) }

// Grayf print message with Gray color
func Grayf(format string, a ...any) { color.Gray.Printf(format, a...) }

// Grayln print message line with Gray color
func Grayln(a ...any) { color.Gray.Println(a...) }

// Greenp print message with Green color
func Greenp(a ...any) { color.Green.Print(a...) }

// Greenf print message with Green color
func Greenf(format string, a ...any) { color.Green.Printf(format, a...) }

// Greenln print message line with Green color
func Greenln(a ...any) { color.Green.Println(a...) }

// Yellowp print message with Yellow color
func Yellowp(a ...any) { color.Yellow.Print(a...) }

// Yellowf print message with Yellow color
func Yellowf(format string, a ...any) { color.Yellow.Printf(format, a...) }

// Yellowln print message line with Yellow color
func Yellowln(a ...any) { color.Yellow.Println(a...) }

// Magentap print message with Magenta color
func Magentap(a ...any) { color.Magenta.Print(a...) }

// Magentaf print message with Magenta color
func Magentaf(format string, a ...any) { color.Magenta.Printf(format, a...) }

// Magentaln print message line with Magenta color
func Magentaln(a ...any) { color.Magenta.Println(a...) }

/*************************************************************
* quick use style print message
*************************************************************/

// Infop print message with Info color
func Infop(a ...any) { color.Info.Print(a...) }

// Infof print message with Info style
func Infof(format string, a ...any) { color.Info.Printf(format, a...) }

// Infoln print message with Info style
func Infoln(a ...any) { color.Info.Println(a...) }

// Successp print message with success color
func Successp(a ...any) { color.Success.Print(a...) }

// Successf print message with success style
func Successf(format string, a ...any) { color.Success.Printf(format, a...) }

// Successln print message with success style
func Successln(a ...any) { color.Success.Println(a...) }

// Errorp print message with Error color
func Errorp(a ...any) { color.Error.Print(a...) }

// Errorf print message with Error style
func Errorf(format string, a ...any) { color.Error.Printf(format, a...) }

// Errorln print message with Error style
func Errorln(a ...any) { color.Error.Println(a...) }

// Warnp print message with Warn color
func Warnp(a ...any) { color.Warn.Print(a...) }

// Warnf print message with Warn style
func Warnf(format string, a ...any) { color.Warn.Printf(format, a...) }

// Warnln print message with Warn style
func Warnln(a ...any) { color.Warn.Println(a...) }
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/gookit/color

go 1.16
go 1.18

require (
github.com/stretchr/testify v1.8.0
Expand Down
33 changes: 29 additions & 4 deletions issues_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package color
package color_test

import (
"fmt"
"strings"
"testing"

"github.com/gookit/color"
)

// https://github.com/gookit/color/issues/51
Expand All @@ -20,7 +22,30 @@ func TestIssues_51(t *testing.T) {
}
title := string(titleRs)

fmt.Printf("topBar:\n%q\n%q\n", topBar, ClearCode(topBar))
fmt.Printf("title:\n%q\n%q\n", title, ClearCode(title))
fmt.Printf("Split:\n%#v\n", strings.Split(ClearCode(topBar), ClearCode(title)))
fmt.Printf("topBar:\n%q\n%q\n", topBar, color.ClearCode(topBar))
fmt.Printf("title:\n%q\n%q\n", title, color.ClearCode(title))
fmt.Printf("Split:\n%#v\n", strings.Split(color.ClearCode(topBar), color.ClearCode(title)))
}

// https://github.com/gookit/color/issues/52
func TestIssues_52(t *testing.T) {
test1 := `FAILS:
one <bg=lightGreen;fg=black>two</> <three>
foo <bg=lightGreen;fg=black>two</> <four>
`

test2 := `WORKS:
one <bg=lightGreen;fg=black>two <three></>
foo <bg=lightGreen;fg=black>two <four></>
`

test3 := `WORKS:
one <bg=lightGreen;fg=black>two</> three
foo <bg=lightGreen;fg=black>two</> four
`

// colorp.Info
color.Print(test1)
color.Print(test2)
color.Print(test3)
}
15 changes: 13 additions & 2 deletions printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ type PrinterFace interface {
// Printer a generic color message printer.
//
// Usage:
// p := &Printer{Code: "32;45;3"}
// p.Print("message")
//
// p := &Printer{Code: "32;45;3"}
// p.Print("message")
type Printer struct {
// NoColor disable color.
NoColor bool
Expand Down Expand Up @@ -91,6 +92,16 @@ func (s *SimplePrinter) Println(v ...interface{}) {
Println(v...)
}

// Successf message
func (s *SimplePrinter) Successf(format string, a ...interface{}) {
Success.Printf(format, a...)
}

// Successln message
func (s *SimplePrinter) Successln(a ...interface{}) {
Success.Println(a...)
}

// Infof message
func (s *SimplePrinter) Infof(format string, a ...interface{}) {
Info.Printf(format, a...)
Expand Down

0 comments on commit 6037df8

Please sign in to comment.