Skip to content

Commit

Permalink
feat: better help
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Jan 16, 2024
1 parent 17b792e commit 5832a6e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 22 deletions.
44 changes: 24 additions & 20 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
package main

type Config struct {
Input string `arg:"" help:"code file to screenshot" optional:""`
Config string `help:"Base configuration file or template." short:"c" group:"Settings" default:"base"`
Output string `help:"Output location for SVG, PNG, or JPEG." short:"o" group:"Settings" default:"out.svg"`
Language string `help:"Language of code file." short:"l" group:"Settings"`
Theme string `help:"Theme to use for syntax highlighting." short:"t" group:"Settings"`
Input string `arg:"" help:"Code to screenshot." optional:""`

Window bool `help:"Display window controls." short:"w"`
Border Border `embed:"" prefix:"border." group:"Border"`
Shadow Shadow `embed:"" prefix:"shadow." help:"add a shadow to the window" short:"s" group:"Shadow"`
Padding []int `help:"Apply padding to the code." short:"p"`
Margin []int `help:"Apply margin to the window." short:"m"`
Background string `help:"Apply a background fill." short:"b"`
// Window
Background string `help:"Apply a background fill." short:"b" placeholder:"#FFF" group:"Window"`
Margin []int `help:"Apply margin to the window." short:"m" placeholder:"0" group:"Window"`
Padding []int `help:"Apply padding to the code." short:"p" placeholder:"0" group:"Window"`
Window bool `help:"Display window controls." short:"w" group:"Window"`

// Settings
Config string `help:"Base configuration file or template." short:"c" group:"Settings" default:"base" placeholder:"base"`
Language string `help:"Language of code file." short:"l" group:"Settings" placeholder:"go"`
Output string `help:"Output location for {{.svg}}, {{.png}}, or {{.jpeg}}." short:"o" group:"Settings" default:"out.svg" placeholder:"out.svg"`
Theme string `help:"Theme to use for syntax highlighting." short:"t" group:"Settings" placeholder:"charm"`

Border Border `embed:"" prefix:"border." group:"Border"`
Shadow Shadow `embed:"" prefix:"shadow." help:"add a shadow to the window" short:"s" group:"Shadow"`

Font Font `embed:"" prefix:"font." group:"Font"`
LineHeight float64 `help:"Line height relative to font size." group:"font"`
LineHeight float64 `help:"Line height relative to font size." group:"Font" placeholder:"1.2"`
}

type Shadow struct {
Blur int `help:"Shadow Gaussian Blur."`
X int `help:"Shadow offset X coordinate"`
Y int `help:"Shadow offset Y coordinate"`
Blur int `help:"Shadow Gaussian Blur." placeholder:"0"`
X int `help:"Shadow offset {{x}} coordinate" placeholder:"0"`
Y int `help:"Shadow offset {{y}} coordinate" placeholder:"0"`
}

type Border struct {
Radius int `help:"Cornder radius of window." short:"r"`
Width int `help:"Border width thickness."`
Color string `help:"Border color."`
Radius int `help:"Corner radius of window." short:"r" placeholder:"0"`
Width int `help:"Border width thickness." placeholder:"1"`
Color string `help:"Border color." placeholder:"#000"`
}

type Font struct {
Family string `help:"Font family to use for code."`
Size float64 `help:"Font size to use for code."`
Family string `help:"Font family to use for code." placeholder:"monospace"`
Size float64 `help:"Font size to use for code." placeholder:"14"`
}

var configs = map[string]string{
Expand Down
57 changes: 57 additions & 0 deletions help.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"strings"

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

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)

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)))

flags := ctx.Flags()
lastGroup := ""

for _, f := range flags {
if f.Name == "help" {
continue
}

if f.Group != nil && lastGroup != f.Group.Title {
lastGroup = f.Group.Title
fmt.Println(titleStyle.Render(strings.ToUpper(f.Group.Title)))
}

if f.Short > 0 {
fmt.Printf(" %s%c", foreground("-", 8), f.Short)
fmt.Printf(" %s%s", foreground("--", 8), 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(strings.Repeat(" ", 16-len(f.Name)))

}
help := strings.ReplaceAll(f.Help, "{{", color(1))
help = strings.ReplaceAll(help, "}}", color(7))
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)
}
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func main() {
config Config
)

_ = kong.Parse(&config)
_ = kong.Parse(&config,
kong.Help(helpPrinter))

c, ok := configs[config.Config]
if !ok {
Expand All @@ -47,7 +48,9 @@ func main() {
if err != nil {
log.Fatal("invalid json configuration", "error", err)
}
_ = kong.Parse(&config, kong.Resolvers(r))
_ = kong.Parse(&config,
kong.Resolvers(r),
kong.Help(helpPrinter))

config.Margin = expandMargin(config.Margin)
config.Padding = expandPadding(config.Padding)
Expand Down

0 comments on commit 5832a6e

Please sign in to comment.