mirror of
https://github.com/charmbracelet/gum
synced 2024-06-11 02:02:17 +02:00
feat: gum format --type emoji|markdown|code|template
This commit is contained in:
parent
71b8363f97
commit
0223da8b6c
|
@ -1,27 +1,55 @@
|
||||||
// Package format provides a way to format a string using a template.
|
// Package format allows you to render formatted text from the command line.
|
||||||
// Behind the scenes it uses the termenv templating helpers to format
|
// There are a few different types of formats that can be used:
|
||||||
// the string. https://github.com/muesli/termenv
|
|
||||||
//
|
//
|
||||||
// Use it to quickly print styled strings without needing to manually
|
// 1. Markdown
|
||||||
// mess with lots of style commands. If you need more powerful styling
|
// -------------
|
||||||
// use the `gum style` and `gum join` to build up output.
|
// 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
|
// $ echo '**Tasty** *Bubble* `Gum`' | gum format
|
||||||
// $ printf 'Inline {{ Bold (Color "#eb5757" "#292927" " code ") }} block' | 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
|
package format
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/charmbracelet/glamour"
|
"github.com/charmbracelet/glamour"
|
||||||
|
@ -31,27 +59,39 @@ import (
|
||||||
|
|
||||||
// Run runs the format command
|
// Run runs the format command
|
||||||
func (o Options) Run() error {
|
func (o Options) Run() error {
|
||||||
in, err := stdin.Read()
|
var input string
|
||||||
if in != "" || err != nil {
|
if len(o.Template) > 0 {
|
||||||
o.Template = in
|
input = strings.Join(o.Template, "\n")
|
||||||
|
} else {
|
||||||
|
input, _ = stdin.Read()
|
||||||
}
|
}
|
||||||
|
|
||||||
switch o.Type {
|
switch o.Type {
|
||||||
case "markdown", "md":
|
case "code":
|
||||||
renderer, err := glamour.NewTermRenderer(glamour.WithStandardStyle("pink"))
|
out, err := glamour.Render(fmt.Sprintf("```\n%s\n```", input), "auto")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
|
||||||
return 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Println(out)
|
fmt.Println(out)
|
||||||
case "template", "tpl":
|
case "template", "tpl":
|
||||||
f := termenv.TemplateFuncs(termenv.ColorProfile())
|
f := termenv.TemplateFuncs(termenv.ColorProfile())
|
||||||
tpl := template.New("tpl").Funcs(f)
|
tpl, err := template.New("tpl").Funcs(f).Parse(input)
|
||||||
tpl, err = tpl.Parse(o.Template)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package format
|
||||||
|
|
||||||
// Options is customization options for the format command
|
// Options is customization options for the format command
|
||||||
type Options struct {
|
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"`
|
||||||
}
|
}
|
||||||
|
|
44
gum.go
44
gum.go
|
@ -40,20 +40,46 @@ type Gum struct {
|
||||||
//
|
//
|
||||||
Filter filter.Options `cmd:"" help:"Filter items from a list"`
|
Filter filter.Options `cmd:"" help:"Filter items from a list"`
|
||||||
|
|
||||||
// Format provides a way to format a string using a template.
|
// Package format allows you to render formatted text from the command line.
|
||||||
// Behind the scenes it uses the termenv templating helpers to format the string.
|
// 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
|
// https://github.com/muesli/termenv
|
||||||
//
|
//
|
||||||
// Use it to quickly print styled strings without needing to manually mess
|
// $ gum format '{{ Bold "Tasty" }} {{ Italic "Bubble" }} {{ Color "99" "0" " Gum " }}' --type template
|
||||||
// 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" }} {{ 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
|
// 4. Emoji
|
||||||
// $ printf 'Inline {{ Bold (Color "#eb5757" "#292927" " code ") }} block' | gum format
|
// ─────────────
|
||||||
|
// 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"`
|
Format format.Options `cmd:"" help:"Format a string using a template"`
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue