Skip to content

Commit

Permalink
fix: improve colored logging
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Sep 21, 2023
1 parent 3998796 commit 6b7bd4a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
15 changes: 9 additions & 6 deletions internal/lefthook/run/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ func (r *Runner) runLFSHook(ctx context.Context) error {

output := strings.Trim(out.String(), "\n")
if output != "" {
log.Debug("[git-lfs] output: ", output)
log.Debug("[git-lfs] out: ", output)
}
if err != nil {
log.Debug("[git-lfs] error: ", err)
log.Debug("[git-lfs] err: ", err)
}

if err == nil && output != "" {
Expand Down Expand Up @@ -481,7 +481,7 @@ func (r *Runner) logSkip(name, reason string) {
}

log.Styled().
WithLeftBorder(lipgloss.NormalBorder()).
WithLeftBorder(lipgloss.NormalBorder(), log.ColorCyan).
WithPadding(execLogPadding).
Info(
log.Cyan(log.Bold(name)) + " " +
Expand All @@ -496,18 +496,21 @@ func (r *Runner) logExecute(name string, err error, out io.Reader) {
}

var execLog string
var color lipgloss.TerminalColor
switch {
case r.SkipSettings.SkipExecutionInfo():
execLog = ""
case err != nil:
execLog = log.Red(fmt.Sprintf("%s > ", name))
execLog = log.Red(fmt.Sprintf("%s ❯ ", name))
color = log.ColorRed
default:
execLog = log.Cyan(fmt.Sprintf("%s > ", name))
execLog = log.Cyan(fmt.Sprintf("%s ❯ ", name))
color = log.ColorCyan
}

if execLog != "" {
log.Styled().
WithLeftBorder(lipgloss.ThickBorder()).
WithLeftBorder(lipgloss.ThickBorder(), color).
WithPadding(execLogPadding).
Info(execLog)
log.Info()
Expand Down
48 changes: 24 additions & 24 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ import (
)

var (
colorRed lipgloss.TerminalColor = lipgloss.CompleteAdaptiveColor{
ColorRed lipgloss.TerminalColor = lipgloss.CompleteAdaptiveColor{
Dark: lipgloss.CompleteColor{TrueColor: "#ff6347", ANSI256: "196", ANSI: "9"},
Light: lipgloss.CompleteColor{TrueColor: "#d70000", ANSI256: "160", ANSI: "1"},
}

colorGreen lipgloss.TerminalColor = lipgloss.CompleteAdaptiveColor{
ColorGreen lipgloss.TerminalColor = lipgloss.CompleteAdaptiveColor{
Dark: lipgloss.CompleteColor{TrueColor: "#76ff7a", ANSI256: "155", ANSI: "10"},
Light: lipgloss.CompleteColor{TrueColor: "#afd700", ANSI256: "148", ANSI: "2"},
}

colorYellow lipgloss.TerminalColor = lipgloss.CompleteAdaptiveColor{
ColorYellow lipgloss.TerminalColor = lipgloss.CompleteAdaptiveColor{
Dark: lipgloss.CompleteColor{TrueColor: "#fada5e", ANSI256: "191", ANSI: "11"},
Light: lipgloss.CompleteColor{TrueColor: "#ffaf00", ANSI256: "214", ANSI: "3"},
}

colorCyan lipgloss.TerminalColor = lipgloss.CompleteAdaptiveColor{
ColorCyan lipgloss.TerminalColor = lipgloss.CompleteAdaptiveColor{
Dark: lipgloss.CompleteColor{TrueColor: "#70C0BA", ANSI256: "37", ANSI: "14"},
Light: lipgloss.CompleteColor{TrueColor: "#00af87", ANSI256: "36", ANSI: "6"},
}

colorGray lipgloss.TerminalColor = lipgloss.CompleteAdaptiveColor{
GolorGray lipgloss.TerminalColor = lipgloss.CompleteAdaptiveColor{
Dark: lipgloss.CompleteColor{TrueColor: "#808080", ANSI256: "244", ANSI: "7"},
Light: lipgloss.CompleteColor{TrueColor: "#4e4e4e", ANSI256: "239", ANSI: "8"},
}
Expand Down Expand Up @@ -100,8 +100,8 @@ func Styled() StyleLogger {
}
}

func (s StyleLogger) WithLeftBorder(border lipgloss.Border) StyleLogger {
s.style = s.style.BorderStyle(border).BorderLeft(true).BorderForeground(colorCyan)
func (s StyleLogger) WithLeftBorder(border lipgloss.Border, color lipgloss.TerminalColor) StyleLogger {
s.style = s.style.BorderStyle(border).BorderLeft(true).BorderForeground(color)

return s
}
Expand All @@ -123,7 +123,7 @@ func (s StyleLogger) Info(str string) {

func Debug(args ...interface{}) {
res := fmt.Sprint(args...)
std.Debug(color(colorGray).Render(res))
std.Debug(color(GolorGray).Render(res))
}

func Debugf(format string, args ...interface{}) {
Expand All @@ -139,7 +139,7 @@ func InfoPad(s string) {
lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderLeft(true).
BorderForeground(colorCyan).
BorderForeground(ColorCyan).
Render(s),
)
}
Expand Down Expand Up @@ -183,21 +183,21 @@ func SetColors(colors interface{}) {
case bool:
std.colors = typedColors
if !std.colors {
setColor(lipgloss.NoColor{}, &colorRed)
setColor(lipgloss.NoColor{}, &colorGreen)
setColor(lipgloss.NoColor{}, &colorYellow)
setColor(lipgloss.NoColor{}, &colorCyan)
setColor(lipgloss.NoColor{}, &colorGray)
setColor(lipgloss.NoColor{}, &ColorRed)
setColor(lipgloss.NoColor{}, &ColorGreen)
setColor(lipgloss.NoColor{}, &ColorYellow)
setColor(lipgloss.NoColor{}, &ColorCyan)
setColor(lipgloss.NoColor{}, &GolorGray)
setColor(lipgloss.NoColor{}, &colorBorder)
}
return
case map[string]interface{}:
std.colors = true
setColor(typedColors["red"], &colorRed)
setColor(typedColors["green"], &colorGreen)
setColor(typedColors["yellow"], &colorYellow)
setColor(typedColors["cyan"], &colorCyan)
setColor(typedColors["gray"], &colorGray)
setColor(typedColors["red"], &ColorRed)
setColor(typedColors["green"], &ColorGreen)
setColor(typedColors["yellow"], &ColorYellow)
setColor(typedColors["cyan"], &ColorCyan)
setColor(typedColors["gray"], &GolorGray)
setColor(typedColors["gray"], &colorBorder)
return
default:
Expand Down Expand Up @@ -227,23 +227,23 @@ func setColor(colorCode interface{}, adaptiveColor *lipgloss.TerminalColor) {
}

func Cyan(s string) string {
return color(colorCyan).Render(s)
return color(ColorCyan).Render(s)
}

func Green(s string) string {
return color(colorGreen).Render(s)
return color(ColorGreen).Render(s)
}

func Red(s string) string {
return color(colorRed).Render(s)
return color(ColorRed).Render(s)
}

func Yellow(s string) string {
return color(colorYellow).Render(s)
return color(ColorYellow).Render(s)
}

func Gray(s string) string {
return color(colorGray).Render(s)
return color(GolorGray).Render(s)
}

func Bold(s string) string {
Expand Down

0 comments on commit 6b7bd4a

Please sign in to comment.