Skip to content

Commit

Permalink
feat(charmbracelet#58): added --copy flag and config setting
Browse files Browse the repository at this point in the history
  • Loading branch information
AlejandroSuero committed May 19, 2024
1 parent 236cd0a commit 37dafec
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
Language string `json:"language,omitempty" help:"Language of code file." short:"l" group:"Settings" placeholder:"go"`
Theme string `json:"theme" help:"Theme to use for syntax highlighting." short:"t" group:"Settings" placeholder:"charm"`

Copy bool `json:"copy" help:"Copy the output image to the clipboard." group:"Settings"`
Output string `json:"output,omitempty" help:"Output location for {{.svg}}, {{.png}}, or {{.webp}}." short:"o" group:"Settings" default:"" placeholder:"freeze.svg"`
Execute string `json:"-" help:"Capture output of command execution." short:"x" group:"Settings" default:""`
ExecuteTimeout time.Duration `json:"-" help:"Execution timeout." group:"Settings" default:"10s" prefix:"execute." name:"timeout" hidden:""`
Expand Down
5 changes: 5 additions & 0 deletions interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func runForm(config *Config) (*Config, error) {
Prompt("").
Value(&config.Output),

huh.NewConfirm().Title("Clipboard").
// Description("Copy the output image to the clipboard.").
Inline(true).
Value(&config.Copy),

huh.NewSelect[string]().Title("Theme ").
// Description("Theme for syntax highlighting.").
Inline(true).
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func main() {
}

// could not convert with libsvg, try resvg
svgConversionErr = resvgConvert(doc, imageWidth, imageHeight, config.Output)
svgConversionErr = resvgConvert(doc, imageWidth, imageHeight, config.Output, config.Copy)
if svgConversionErr != nil {
printErrorFatal("Unable to convert SVG to PNG", svgConversionErr)
}
Expand Down
10 changes: 9 additions & 1 deletion png.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/beevik/etree"
"github.com/charmbracelet/freeze/font"
"github.com/kanrichan/resvg-go"
"golang.design/x/clipboard"
)

func libsvgConvert(doc *etree.Document, w, h float64, output string) error {
Expand All @@ -30,7 +31,7 @@ func libsvgConvert(doc *etree.Document, w, h float64, output string) error {
return err
}

func resvgConvert(doc *etree.Document, w, h float64, output string) error {
func resvgConvert(doc *etree.Document, w, h float64, output string, copy bool) error {
svg, err := doc.WriteToBytes()
if err != nil {
return err
Expand Down Expand Up @@ -94,5 +95,12 @@ func resvgConvert(doc *etree.Document, w, h float64, output string) error {
if err != nil {
return err
}
if copy {
err = clipboard.Init()
if err != nil {
return err
}
clipboard.Write(clipboard.FmtImage, png)
}
return err
}

0 comments on commit 37dafec

Please sign in to comment.