Skip to content

Commit

Permalink
feat: add info box and summary separator
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed Sep 20, 2023
1 parent 4fd24a5 commit 9e120d8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 18 deletions.
26 changes: 17 additions & 9 deletions internal/lefthook/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,20 @@ func (l *Lefthook) Run(hookName string, args RunArgs, gitArgs []string) error {
}

if !logSettings.SkipMeta() {
log.Info(
log.Cyan("lefthook"),
log.Gray(fmt.Sprintf("v%s,", version.Version(false))),
log.Gray("hook:"),
log.Bold(hookName),
log.Box(
strings.Join(
[]string{
log.Cyan("🥊 lefthook"),
log.Gray(fmt.Sprintf("v%s", version.Version(false))),
},
" ",
),
strings.Join(
[]string{
log.Gray("hook:"),
log.Bold(hookName),
}, " ",
),
)
}

Expand Down Expand Up @@ -183,7 +192,7 @@ func printSummary(
) {
if len(results) == 0 {
if !logSettings.SkipEmptySummary() {
log.Info(
log.Separate(
fmt.Sprintf(
"%s %s %s",
log.Cyan("summary:"),
Expand All @@ -195,9 +204,8 @@ func printSummary(
return
}

log.Info(
log.Cyan("\nsummary:"),
log.Gray(fmt.Sprintf("(done in %.2f seconds)", duration.Seconds())),
log.Separate(
log.Cyan("summary: ") + log.Gray(fmt.Sprintf("(done in %.2f seconds)", duration.Seconds())),
)

if !logSettings.SkipSuccess() {
Expand Down
58 changes: 49 additions & 9 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ var (
Light: lipgloss.CompleteColor{TrueColor: "#4e4e4e", ANSI256: "239", ANSI: "8"},
}

colorBorder lipgloss.TerminalColor = lipgloss.AdaptiveColor{Light: "#D9DCCF", Dark: "#383838"}

std = New()

separatorWidth = 36
separatorMargin = 2
)

type Level uint32
Expand Down Expand Up @@ -136,6 +141,14 @@ func SetColors(colors interface{}) {
switch typedColors := colors.(type) {
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{}, &colorBorder)
}
return
case map[string]interface{}:
std.colors = true
Expand All @@ -144,23 +157,23 @@ func SetColors(colors interface{}) {
setColor(typedColors["yellow"], &colorYellow)
setColor(typedColors["cyan"], &colorCyan)
setColor(typedColors["gray"], &colorGray)
setColor(typedColors["gray"], &colorBorder)
return
default:
std.colors = true
}
}

func setColor(colorCode interface{}, adaptiveColor *lipgloss.TerminalColor) {
if colorCode == nil {
return
}

var code string
switch typedCode := colorCode.(type) {
case int:
code = strconv.Itoa(typedCode)
case string:
code = typedCode
case lipgloss.NoColor:
*adaptiveColor = typedCode
return
default:
return
}
Expand Down Expand Up @@ -200,11 +213,33 @@ func Bold(s string) string {
return lipgloss.NewStyle().Bold(true).Render(s)
}

func color(clr lipgloss.TerminalColor) lipgloss.Style {
if !std.colors {
return lipgloss.NewStyle()
}
func Box(left, right string) {
Info(
lipgloss.JoinHorizontal(
lipgloss.Top,
lipgloss.NewStyle().Border(lipgloss.RoundedBorder(), true, false, true, true).BorderForeground(colorBorder).Padding(0, 1).Render(left),
lipgloss.NewStyle().Border(lipgloss.RoundedBorder(), true, true, true, false).BorderForeground(colorBorder).Padding(0, 1).Render(right),
),
)
}

func Separate(s string) {
Info(
lipgloss.JoinVertical(
lipgloss.Left,
lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderBottom(true).
BorderForeground(colorBorder).
Width(separatorWidth).
MarginLeft(separatorMargin).
Render(""),
s,
),
)
}

func color(clr lipgloss.TerminalColor) lipgloss.Style {
return lipgloss.NewStyle().Foreground(clr)
}

Expand Down Expand Up @@ -243,7 +278,12 @@ func (l *Logger) Info(args ...interface{}) {
}

func (l *Logger) Debug(args ...interface{}) {
l.Log(DebugLevel, args...)
leftBorder := lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderLeft(true).
BorderForeground(colorBorder).
Render("")
l.Log(DebugLevel, append([]interface{}{leftBorder}, args...)...)
}

func (l *Logger) Error(args ...interface{}) {
Expand Down

0 comments on commit 9e120d8

Please sign in to comment.