diff --git a/format/command.go b/format/command.go index a88b790..0c9f2b8 100644 --- a/format/command.go +++ b/format/command.go @@ -1,27 +1,55 @@ -// Package format provides a way to format a string using a template. -// Behind the scenes it uses the termenv templating helpers to format -// the string. https://github.com/muesli/termenv +// Package format allows you to render formatted text from the command line. +// There are a few different types of formats that can be used: // -// Use it to quickly print styled strings without needing to manually -// mess with lots of style commands. If you need more powerful styling -// use the `gum style` and `gum join` to build up output. +// 1. Markdown +// ------------- +// Render any input as markdown text. This uses glamour behind the scenes. +// Input passed as arguments are placed on new lines. +// Input passed as stdin are rendered as is. +// https://github.com/charmbracelet/glamour // -// $ gum format '{{ Bold "Tasty" }} {{ Underline "Bubble" }} {{ Foreground "212" "Gum" }}' +// $ gum format '**Tasty** *Bubble* `Gum`' +// $ gum format "1. List" "2. Of" "3. Items" +// $ echo "# Bubble Gum \n - List \n - of \n - Items" | gum format +// $ gum format -- "- Bulleted" "- List" // -// Or, pass the format string over stdin: +// Or, pass input via stdin: // -// $ printf '{{ Bold "Tasty" }} {{ Underline "Bubble" }} {{ Foreground "212" "Gum" }}' | gum format -// $ printf 'Inline {{ Bold (Color "#eb5757" "#292927" " code ") }} block' | gum format +// $ echo '**Tasty** *Bubble* `Gum`' | gum format // -// Markdown also works! +// 2. Template +// ------------- +// Render styled input from a template. Templates are handled by termenv. +// https://github.com/muesli/termenv // -// $ gum format --type markdown '**Tasty** ~~Bubble~~ `Gum`' +// $ gum format '{{ Bold "Tasty" }} {{ Italic "Bubble" }} {{ Color "99" "0" " Gum " }}' --type template +// +// 3. Code +// ------------- +// Perform syntax highlighting on some code snippets. Styling is handled by +// glamour, which in turn uses chroma. https://github.com/alecthomas/chroma +// +// $ cat code.go | gum format --type code +// +// 4. Emoji +// ------------- +// Parse emojis within text and render emojis. Emoji rendering is handled by +// glamour, which in turn uses goldmark-emoji. +// https://github.com/yuin/goldmark-emoji +// +// $ gum format 'I :heart: Bubble Gum :candy:' --type emoji +// $ echo "I :heart: Bubble Gum :candy:" | gum format --type emoji +// +// Output: +// +// I ❤️ Bubble Gum 🍬 // package format import ( "bytes" "fmt" + "strings" "text/template" "github.com/charmbracelet/glamour" @@ -31,27 +59,39 @@ import ( // Run runs the format command func (o Options) Run() error { - in, err := stdin.Read() - if in != "" || err != nil { - o.Template = in + var input string + if len(o.Template) > 0 { + input = strings.Join(o.Template, "\n") + } else { + input, _ = stdin.Read() } switch o.Type { - case "markdown", "md": - renderer, err := glamour.NewTermRenderer(glamour.WithStandardStyle("pink")) + case "code": + out, err := glamour.Render(fmt.Sprintf("```\n%s\n```", input), "auto") if err != nil { - fmt.Println(err) return err } - out, err := renderer.Render(o.Template) + fmt.Println(out) + case "markdown", "md": + out, err := glamour.Render(input, "pink") + if err != nil { + return err + } + fmt.Println(out) + case "emoji": + renderer, err := glamour.NewTermRenderer(glamour.WithEmoji()) + if err != nil { + return err + } + out, err := renderer.Render(input) if err != nil { return err } fmt.Println(out) case "template", "tpl": f := termenv.TemplateFuncs(termenv.ColorProfile()) - tpl := template.New("tpl").Funcs(f) - tpl, err = tpl.Parse(o.Template) + tpl, err := template.New("tpl").Funcs(f).Parse(input) if err != nil { return err } diff --git a/format/options.go b/format/options.go index e8e49d7..9933609 100644 --- a/format/options.go +++ b/format/options.go @@ -2,7 +2,7 @@ package format // Options is customization options for the format command type Options struct { - Template string `arg:"" optional:"" help:"Template string to format"` + Template []string `arg:"" optional:"" help:"Template string to format (can also be provided via stdin)"` - Type string `help:"Format to use" enum:"markdown,md,template,tpl" short:"t" default:"template"` + Type string `help:"Format to use (markdown,template,code,emoji)" enum:"markdown,md,template,tpl,code,emoji" short:"t" default:"markdown"` } diff --git a/gum.go b/gum.go index a70e5fd..1079550 100644 --- a/gum.go +++ b/gum.go @@ -40,20 +40,46 @@ type Gum struct { // Filter filter.Options `cmd:"" help:"Filter items from a list"` - // Format provides a way to format a string using a template. - // Behind the scenes it uses the termenv templating helpers to format the string. + // Package format allows you to render formatted text from the command line. + // There are a few different types of formats that can be used: + // + // 1. Markdown + // ───────────── + // Render any input as markdown text. This uses glamour behind the scenes. + // https://github.com/charmbracelet/glamour + // + // $ gum format '**Tasty** *Bubble* `Gum`' + // + // Or, pass input via stdin: + // + // $ echo '**Tasty** *Bubble* `Gum`' | gum format + // + // 2. Template + // ───────────── + // Render styled input from a template. Templates are handled by termenv. // https://github.com/muesli/termenv // - // Use it to quickly print styled strings without needing to manually mess - // with lots of style commands. If you need more powerful styling use the - // `gum style` and `gum join` to build up output. + // $ gum format '{{ Bold "Tasty" }} {{ Italic "Bubble" }} {{ Color "99" "0" " Gum " }}' --type template // - // $ gum format '{{ Bold "Tasty" }} {{ Underline "Bubble" }} {{ Foreground "212" "Gum" }}' + // 3. Code + // ───────────── + // Perform syntax highlighting on some code snippets. Styling is handled by + // glamour, which in turn uses chroma. https://github.com/alecthomas/chroma // - // Or, pass the format string over stdin: + // $ cat code.go | gum format --type code // - // $ printf '{{ Bold "Tasty" }} {{ Underline "Bubble" }} {{ Foreground "212" "Gum" }}' | gum format - // $ printf 'Inline {{ Bold (Color "#eb5757" "#292927" " code ") }} block' | gum format + // 4. Emoji + // ───────────── + // Parse emojis within text and render emojis. Emoji rendering is handled by + // glamour, which in turn uses goldmark-emoji. + // https://github.com/yuin/goldmark-emoji + // + // $ gum format 'I :heart: Bubble Gum :candy:' --type emoji + // $ echo "I :heart: Bubble Gum :candy:" | gum format --type emoji + // + // Output: + // + // I ❤️ Bubble Gum 🍬 // Format format.Options `cmd:"" help:"Format a string using a template"`