Skip to content

Commit

Permalink
refactor: use liplgoss for help styling
Browse files Browse the repository at this point in the history
allows automatic color downgrading
  • Loading branch information
maaslalani committed Jan 29, 2024
1 parent 989b784 commit 55992ee
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ package main

import (
"fmt"
"regexp"
"strings"

"github.com/alecthomas/kong"
"github.com/charmbracelet/lipgloss"
)

var highlighter = regexp.MustCompile("{{(.+?)}}")

func helpPrinter(options kong.HelpOptions, ctx *kong.Context) error {
var titleStyle = lipgloss.NewStyle().Bold(true).Margin(1, 0, 0, 2).Foreground(lipgloss.Color("#875FFF"))
var codeBlockStyle = lipgloss.NewStyle().Background(lipgloss.Color("0")).Padding(1, 3).Margin(0, 2)
codeBlockStyle := lipgloss.NewStyle().Background(lipgloss.Color("0")).Padding(1, 0)
programStyle := codeBlockStyle.Copy().Foreground(lipgloss.Color("12")).PaddingLeft(2).MarginLeft(2)
argumentStyle := codeBlockStyle.Copy().Foreground(lipgloss.Color("7")).Padding(1, 1)
flagStyle := codeBlockStyle.Copy().Foreground(lipgloss.Color("244")).PaddingRight(2)
titleStyle := lipgloss.NewStyle().Bold(true).Margin(1, 0, 0, 2).Foreground(lipgloss.Color("#875FFF"))
dashStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("240")).MarginLeft(2)
keywordStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("1"))

fmt.Println()
fmt.Println(" Screenshot code on the command line. 📸")
fmt.Println()
fmt.Println(codeBlockStyle.Render(foreground("freeze", 13) + " main.go " + foreground("[-o code.svg] [--flags]", 244)))
fmt.Println(lipgloss.JoinHorizontal(lipgloss.Center, programStyle.Render("freeze"), argumentStyle.Render("main.go"), flagStyle.Render("[-o code.svg] [--flags]")))

flags := ctx.Flags()
lastGroup := ""
Expand All @@ -31,27 +39,18 @@ func helpPrinter(options kong.HelpOptions, ctx *kong.Context) error {
}

if f.Short > 0 {
fmt.Printf(" %s%c", foreground("-", 8), f.Short)
fmt.Printf(" %s%s", foreground("--", 8), f.Name)
fmt.Print(" ", dashStyle.Render("-"), string(f.Short))
fmt.Print(dashStyle.Render("--"), f.Name)
fmt.Print(strings.Repeat(" ", 16-len(f.Name)))
} else {
fmt.Printf(" %s%c", " ", ' ')
fmt.Printf(" %s%s", foreground("--", 8), f.Name)
fmt.Print(" ", dashStyle.Render(" "), " ")
fmt.Print(dashStyle.Render("--"), f.Name)
fmt.Print(strings.Repeat(" ", 16-len(f.Name)))

}
help := strings.ReplaceAll(f.Help, "{{", color(1))
help = strings.ReplaceAll(help, "}}", color(7))
help := highlighter.ReplaceAllString(f.Help, keywordStyle.Render("$1"))
fmt.Println(help)
}
fmt.Println()
return nil
}

func color(c int) string {
return fmt.Sprintf("\x1b[38;5;%dm", c)
}

func foreground(s string, c int) string {
return color(c) + s + color(7)
}

0 comments on commit 55992ee

Please sign in to comment.