A set of utilities/helpers I'm finding myself adding to multiple go codebases.
ui.go
110 lines 3.9 kB view raw
1package ui 2 3import ( 4 "fmt" 5 6 "github.com/charmbracelet/lipgloss" 7) 8 9func newStyle() lipgloss.Style { return lipgloss.NewStyle() } 10func newPStyle(v, h int) lipgloss.Style { return lipgloss.NewStyle().Padding(v, h) } 11func newBoldStyle() lipgloss.Style { return newStyle().Bold(true) } 12func newPBoldStyle(v, h int) lipgloss.Style { return newPStyle(v, h).Bold(true) } 13func newEmStyle() lipgloss.Style { return newStyle().Italic(true) } 14 15func success(msg string) string { return SuccessStyle.Render("✓ " + msg) } 16func errorMsg(msg string) string { return ErrorStyle.Render("✗ " + msg) } 17func warning(msg string) string { return WarningStyle.Render("⚠ " + msg) } 18func info(msg string) string { return InfoStyle.Render("ℹ " + msg) } 19func title(msg string) string { return TitleStyle.Render(msg) } 20func subtitle(msg string) string { return SubtitleStyle.Render(msg) } 21func box(content string) string { return BoxStyle.Render(content) } 22func errorBox(content string) string { return ErrorBoxStyle.Render(content) } 23func text(content string) string { return TextStyle.Render(content) } 24func header(content string) string { return HeaderStyle.Render(content) } 25 26// Success prints a formatted success message 27func Success(format string, a ...any) { 28 fmt.Print(success(fmt.Sprintf(format, a...))) 29} 30 31// Successln prints a formatted success message with a newline 32func Successln(format string, a ...any) { 33 fmt.Println(success(fmt.Sprintf(format, a...))) 34} 35 36// Error prints a formatted error message 37func Error(format string, a ...any) { 38 fmt.Print(errorMsg(fmt.Sprintf(format, a...))) 39} 40 41// Errorln prints a formatted error message with a newline 42func Errorln(format string, a ...any) { 43 fmt.Println(errorMsg(fmt.Sprintf(format, a...))) 44} 45 46// Warning prints a formatted warning message 47func Warning(format string, a ...any) { 48 fmt.Print(warning(fmt.Sprintf(format, a...))) 49} 50 51// Warningln prints a formatted warning message with a newline 52func Warningln(format string, a ...any) { 53 fmt.Println(warning(fmt.Sprintf(format, a...))) 54} 55 56// Info prints a formatted info message 57func Info(format string, a ...any) { 58 fmt.Print(info(fmt.Sprintf(format, a...))) 59} 60 61// Infoln prints a formatted info message with a newline 62func Infoln(format string, a ...any) { 63 fmt.Println(info(fmt.Sprintf(format, a...))) 64} 65 66// Title prints a formatted title 67func Title(format string, a ...any) { 68 fmt.Print(title(fmt.Sprintf(format, a...))) 69} 70 71// Titleln prints a formatted title with a newline 72func Titleln(format string, a ...any) { 73 fmt.Println(title(fmt.Sprintf(format, a...))) 74} 75 76// Subtitle prints a formatted subtitle 77func Subtitle(format string, a ...any) { 78 fmt.Print(subtitle(fmt.Sprintf(format, a...))) 79} 80 81// Subtitleln prints a formatted subtitle with a newline 82func Subtitleln(format string, a ...any) { 83 fmt.Println(subtitle(fmt.Sprintf(format, a...))) 84} 85 86// Box prints content in a styled box 87func Box(format string, a ...any) { 88 fmt.Print(box(fmt.Sprintf(format, a...))) 89} 90 91// Boxln prints content in a styled box with a newline 92func Boxln(format string, a ...any) { 93 fmt.Println(box(fmt.Sprintf(format, a...))) 94} 95 96// ErrorBox prints error content in a styled error box 97func ErrorBox(format string, a ...any) { 98 fmt.Print(errorBox(fmt.Sprintf(format, a...))) 99} 100 101// ErrorBoxln prints error content in a styled error box with a newline 102func ErrorBoxln(format string, a ...any) { 103 fmt.Println(errorBox(fmt.Sprintf(format, a...))) 104} 105 106func Newline() { fmt.Println() } 107func Plain(format string, a ...any) { fmt.Print(text(fmt.Sprintf(format, a...))) } 108func Plainln(format string, a ...any) { fmt.Println(text(fmt.Sprintf(format, a...))) } 109func Header(format string, a ...any) { fmt.Print(header(fmt.Sprintf(format, a...))) } 110func Headerln(format string, a ...any) { fmt.Print(header(fmt.Sprintf(format, a...))) }