Skip to content

Commit

Permalink
chore: Use lipgloss for output (evilmartians#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Dec 12, 2022
1 parent d13ba16 commit a1ffd9b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ go 1.19
require (
github.com/MakeNowJust/heredoc v1.0.0
github.com/briandowns/spinner v1.19.0
github.com/charmbracelet/lipgloss v0.6.0
github.com/creack/pty v1.1.18
github.com/gobwas/glob v0.2.3
github.com/google/go-cmp v0.5.9
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/mitchellh/mapstructure v1.5.0
github.com/spf13/afero v1.9.3
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.14.0
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61
)

require (
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68 // indirect
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
)

require (
github.com/alessio/shellescape v1.4.1 // indirect
github.com/fatih/color v1.13.0 // indirect
Expand Down
16 changes: 14 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPp
github.com/briandowns/spinner v1.19.0 h1:s8aq38H+Qju89yhp89b4iIiMzMm8YN3p6vGpwyh/a8E=
github.com/briandowns/spinner v1.19.0/go.mod h1:mQak9GHqbspjC/5iUx3qMlIho8xBS/ppAL/hX5SmPJU=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/charmbracelet/lipgloss v0.6.0 h1:1StyZB9vBSOyuZxQUcUwGr17JmojPNm87inij9N3wJY=
github.com/charmbracelet/lipgloss v0.6.0/go.mod h1:tHh2wr34xcHjC2HCXIlGSG1jaDF0S0atAUvBMP6Ppuk=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
Expand Down Expand Up @@ -148,8 +150,8 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
github.com/magiconair/properties v1.8.6/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
Expand All @@ -161,8 +163,15 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68 h1:y1p/ycavWjGT9FnmSjdbWUlLGvcxrY0Rw3ATltrxOhk=
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0 h1:STjmj0uFfRryL9fzRA/OupNppeAID6QJYPMavTL7jtY=
github.com/muesli/termenv v0.11.1-0.20220204035834-5ac8409525e0/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.5 h1:ipoSadvV8oGUjnUbMub59IDPPwfxF694nG/jwbMiyQg=
Expand All @@ -172,6 +181,9 @@ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qR
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down
4 changes: 2 additions & 2 deletions internal/lefthook/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,10 +469,10 @@ func (r *Runner) run(opts ExecuteOptions) {
var execName string
if err != nil {
r.fail(opts.name, opts.failText)
execName = fmt.Sprint(log.Red("\n EXECUTE >"), log.Bold(opts.name))
execName = fmt.Sprint(log.Red("\n EXECUTE > "), log.Bold(opts.name))
} else {
r.success(opts.name)
execName = fmt.Sprint(log.Cyan("\n EXECUTE >"), log.Bold(opts.name))
execName = fmt.Sprint(log.Cyan("\n EXECUTE > "), log.Bold(opts.name))
}

if out != nil {
Expand Down
50 changes: 32 additions & 18 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ import (
"time"

"github.com/briandowns/spinner"
"github.com/logrusorgru/aurora"
"github.com/charmbracelet/lipgloss"
)

const grayColor = 11
const (
colorCyan = "#70C0BA"
colorYellow = "#fada5e"
colorRed = "#ff6347"
colorGreen = "#76ff7a"
colorGray = "#808080"
)

var std = New()

Expand All @@ -31,18 +37,18 @@ const (

type Logger struct {
level Level
aurora aurora.Aurora
out io.Writer
mu sync.Mutex
spinner *spinner.Spinner
colors bool
names []string
spinner *spinner.Spinner
}

func New() *Logger {
return &Logger{
level: InfoLevel,
out: os.Stdout,
aurora: aurora.NewAurora(true),
colors: true,
spinner: spinner.New(
spinner.CharSets[spinnerCharSet],
spinnerRefreshRate,
Expand All @@ -61,7 +67,7 @@ func StopSpinner() {

func Debug(args ...interface{}) {
res := fmt.Sprint(args...)
std.Debug(std.aurora.Gray(grayColor, res))
std.Debug(color(colorGray).Render(res))
}

func Debugf(format string, args ...interface{}) {
Expand All @@ -87,7 +93,7 @@ func Errorf(format string, args ...interface{}) {

func Warn(args ...interface{}) {
res := fmt.Sprint(args...)
std.Warn(std.aurora.Yellow(res))
std.Warn(Yellow(res))
}

func Warnf(format string, args ...interface{}) {
Expand All @@ -107,27 +113,35 @@ func SetLevel(level Level) {
}

func SetColors(enable bool) {
std.aurora = aurora.NewAurora(enable)
std.colors = enable
}

func Cyan(arg interface{}) aurora.Value {
return std.aurora.Cyan(arg)
func Cyan(s string) string {
return color(colorCyan).Render(s)
}

func Green(arg interface{}) aurora.Value {
return std.aurora.Green(arg)
func Green(s string) string {
return color(colorGreen).Render(s)
}

func Red(arg interface{}) aurora.Value {
return std.aurora.Red(arg)
func Red(s string) string {
return color(colorRed).Render(s)
}

func Yellow(arg interface{}) aurora.Value {
return std.aurora.Yellow(arg)
func Yellow(s string) string {
return color(colorYellow).Render(s)
}

func Bold(arg interface{}) aurora.Value {
return std.aurora.Bold(arg)
func Bold(s string) string {
return lipgloss.NewStyle().Bold(true).Render(s)
}

func color(colorCode string) lipgloss.Style {
if !std.colors {
return lipgloss.NewStyle()
}

return lipgloss.NewStyle().Foreground(lipgloss.Color(colorCode))
}

func SetOutput(out io.Writer) {
Expand Down

0 comments on commit a1ffd9b

Please sign in to comment.