From 236cd0a9827f201ef964b9bd88435d4db30f9254 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Sun, 19 May 2024 17:03:27 +0200 Subject: [PATCH 01/19] feat(#58): added `golang.design/x/clipboard` package --- go.mod | 4 ++++ go.sum | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/go.mod b/go.mod index 405bfb2..5f4523e 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/kanrichan/resvg-go v0.0.2-0.20231001163256-63db194ca9f5 github.com/mattn/go-isatty v0.0.20 github.com/mattn/go-runewidth v0.0.15 + golang.design/x/clipboard v0.7.0 golang.org/x/sys v0.18.0 ) @@ -41,6 +42,9 @@ require ( github.com/rivo/uniseg v0.4.7 // indirect github.com/tetratelabs/wazero v1.7.0 // indirect golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect + golang.org/x/exp/shiny v0.0.0-20240506185415-9bf2ced13842 // indirect + golang.org/x/image v0.14.0 // indirect + golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect golang.org/x/sync v0.6.0 // indirect golang.org/x/term v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/go.sum b/go.sum index 56f5052..7774451 100644 --- a/go.sum +++ b/go.sum @@ -82,8 +82,16 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/tetratelabs/wazero v1.7.0 h1:jg5qPydno59wqjpGrHph81lbtHzTrWzwwtD4cD88+hQ= github.com/tetratelabs/wazero v1.7.0/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y= +golang.design/x/clipboard v0.7.0 h1:4Je8M/ys9AJumVnl8m+rZnIvstSnYj1fvzqYrU3TXvo= +golang.design/x/clipboard v0.7.0/go.mod h1:PQIvqYO9GP29yINEfsEn5zSQKAz3UgXmZKzDA6dnq2E= golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw= golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ= +golang.org/x/exp/shiny v0.0.0-20240506185415-9bf2ced13842 h1:kEvPiBVeT1JJGw/3THfe1W1zvTAvU1V6pCFV0icZvQs= +golang.org/x/exp/shiny v0.0.0-20240506185415-9bf2ced13842/go.mod h1:3F+MieQB7dRYLTmnncoFbb1crS5lfQoTfDgQy6K4N0o= +golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= +golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= +golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a h1:sYbmY3FwUWCBTodZL1S3JUuOvaW6kM2o+clDzzDNBWg= +golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a/go.mod h1:Ede7gF0KGoHlj822RtphAHK1jLdrcuRBZg0sF1Q+SPc= golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= From 37dafec9f6db3c7574642d020f4a37422d7c7c6a Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Sun, 19 May 2024 17:04:49 +0200 Subject: [PATCH 02/19] feat(#58): added `--copy` flag and config setting --- config.go | 1 + interactive.go | 5 +++++ main.go | 2 +- png.go | 10 +++++++++- 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/config.go b/config.go index 09448f0..d86ebb8 100644 --- a/config.go +++ b/config.go @@ -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:""` diff --git a/interactive.go b/interactive.go index e7916cf..7ea6d8f 100644 --- a/interactive.go +++ b/interactive.go @@ -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). diff --git a/main.go b/main.go index 3675a47..6fc14cb 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/png.go b/png.go index af0348f..9f83bed 100644 --- a/png.go +++ b/png.go @@ -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 { @@ -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 @@ -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 } From d4d556c098d83ee7fd6baaf2b9929a4e69e2d62c Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Sun, 19 May 2024 17:05:24 +0200 Subject: [PATCH 03/19] test(#58): tested `--copy` flag --- freeze_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/freeze_test.go b/freeze_test.go index 5409a9f..dbb8b6a 100644 --- a/freeze_test.go +++ b/freeze_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/aymanbagabas/go-udiff" + "golang.design/x/clipboard" ) const binary = "./test/freeze-test" @@ -56,6 +57,31 @@ func TestFreezeOutput(t *testing.T) { } } +func TestFreezeCopy(t *testing.T) { + output := "artichoke-test.svg" + defer os.Remove(output) + + cmd := exec.Command(binary, "test/input/artichoke.hs", "-o", output, "--copy") + err := cmd.Run() + if err != nil { + t.Fatal(err) + } + + _, err = os.Stat(output) + if err != nil { + t.Fatal(err) + } + + err = clipboard.Init() + if err != nil { + t.Fatal(err) + } + png := clipboard.Read(clipboard.FmtImage) + if png == nil { + t.Fatal("clipboard is empty") + } +} + func TestFreezeHelp(t *testing.T) { out := bytes.Buffer{} cmd := exec.Command(binary) @@ -134,6 +160,11 @@ func TestFreezeConfigurations(t *testing.T) { flags: []string{"--language", "go", "--height", "800", "--width", "750", "--config", "full", "--window=false", "--show-line-numbers"}, output: "bubbletea", }, + { + input: "test/input/bubbletea.model", + flags: []string{"--language", "go", "--height", "800", "--width", "750", "--config", "full", "--window=false", "--show-line-numbers", "--copy"}, + output: "bubbletea-copy", + }, // { // flags: []string{"--execute", "layout", "--height", "800", "--config", "full", "--margin", "50,10"}, // output: "composite-2", From 4c4f2bdd7277c90438d1c999ad022781b9213d45 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Sun, 19 May 2024 17:06:16 +0200 Subject: [PATCH 04/19] docs(#58): update `README.md`, golden and `full.json` with `--copy` --- README.md | 9 ++++++ configurations/full.json | 5 +-- test/golden/svg/bubbletea-copy.svg | 49 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 test/golden/svg/bubbletea-copy.svg diff --git a/README.md b/README.md index 533a1de..eda601f 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ Screenshots can be customized with `--flags` or [Configuration](#configuration) - [`--font.file`](#font): File path to the font to use (embedded in the SVG). - [`--line-height`](#font): Line height relative to font size. - [`--show-line-numbers`](#line-numbers): Show line numbers. +- [`--copy`](#copy): Copy the output image to the clipboard. - [`--lines`](#line-numbers): Lines to capture (start,end). ### Language @@ -159,6 +160,14 @@ freeze main.go --output out.webp freeze main.go --output out.{svg,png,webp} ``` +### Copy + +Copy the output image to your clipboard, so you can paste it anywhere. + +```bash +freeze main.go --copy +``` + ### Font Specify the font family, font size, and font line height of the output image. diff --git a/configurations/full.json b/configurations/full.json index 40d5131..36f5fde 100644 --- a/configurations/full.json +++ b/configurations/full.json @@ -29,5 +29,6 @@ "size": 14, "ligatures": true }, - "line_height": 1.2 -} \ No newline at end of file + "line_height": 1.2, + "copy": false +} diff --git a/test/golden/svg/bubbletea-copy.svg b/test/golden/svg/bubbletea-copy.svg new file mode 100644 index 0000000..304e551 --- /dev/null +++ b/test/golden/svg/bubbletea-copy.svg @@ -0,0 +1,49 @@ + + + + + + 1 func (m model) Init() tea.Cmd { + 2     return nil + 3 } + 4 + 5 func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + 6     switch msg := msg.(type) { + 7 + 8     case tea.KeyMsg: + 9         switch msg.String() { + 10         case "ctrl+c", "q": + 11             return m, tea.Quit + 12         case "up", "k": + 13             if m.cursor > 0 { + 14                 m.cursor-- + 15             } + 16         case "down", "j": + 17             if m.cursor < len(m.choices)-1 { + 18                 m.cursor++ + 19             } + 20         case "enter", " ": + 21             _, ok := m.selected[m.cursor] + 22             if ok { + 23                 delete(m.selected, m.cursor) + 24             } else { + 25                 m.selected[m.cursor] = struct{}{} + 26             } + 27         } + 28     } + 29     return m, nil + 30 } + 31 + 32 func (m model) View() string { + 33     return // ... + 34 } + + + From eba42f9af36747147d97d1a8563b7ebfdaf737e6 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Mon, 27 May 2024 15:24:02 +0200 Subject: [PATCH 05/19] fix(test): writting image to clipboard --- freeze_test.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/freeze_test.go b/freeze_test.go index dbb8b6a..9c15c84 100644 --- a/freeze_test.go +++ b/freeze_test.go @@ -58,7 +58,7 @@ func TestFreezeOutput(t *testing.T) { } func TestFreezeCopy(t *testing.T) { - output := "artichoke-test.svg" + output := "artichoke-test.png" defer os.Remove(output) cmd := exec.Command(binary, "test/input/artichoke.hs", "-o", output, "--copy") @@ -76,6 +76,11 @@ func TestFreezeCopy(t *testing.T) { if err != nil { t.Fatal(err) } + outputFile, err := os.ReadFile(output) + if err != nil { + t.Fatal(err) + } + clipboard.Write(clipboard.FmtImage, outputFile) png := clipboard.Read(clipboard.FmtImage) if png == nil { t.Fatal("clipboard is empty") From c57172d67d6c3dba7e1d025b475bbb6794777681 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Fri, 31 May 2024 12:24:51 +0200 Subject: [PATCH 06/19] feat: added `clipboard.Read` and modified `copy` in case of conflict --- png.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/png.go b/png.go index 9f83bed..edd26bf 100644 --- a/png.go +++ b/png.go @@ -31,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, copy bool) error { +func resvgConvert(doc *etree.Document, w, h float64, output string, toClipboard bool) error { svg, err := doc.WriteToBytes() if err != nil { return err @@ -95,12 +95,13 @@ func resvgConvert(doc *etree.Document, w, h float64, output string, copy bool) e if err != nil { return err } - if copy { + if toClipboard { err = clipboard.Init() if err != nil { return err } clipboard.Write(clipboard.FmtImage, png) + clipboard.Read(clipboard.FmtImage) } return err } From 96cb243844ee60e25cb6be8cf23f5feab665fdb0 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Wed, 5 Jun 2024 18:04:06 +0200 Subject: [PATCH 07/19] test: updated test to follow `TestFreezeConfigurations` --- freeze_test.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/freeze_test.go b/freeze_test.go index 9c15c84..f27665b 100644 --- a/freeze_test.go +++ b/freeze_test.go @@ -58,29 +58,18 @@ func TestFreezeOutput(t *testing.T) { } func TestFreezeCopy(t *testing.T) { - output := "artichoke-test.png" + output := "bubbletea-copy.png" defer os.Remove(output) - cmd := exec.Command(binary, "test/input/artichoke.hs", "-o", output, "--copy") + cmd := exec.Command(binary, "test/input/bubbletea.model", "-o", output, "--language", "go", "--height", "800", "--width", "750", "--config", "full", "--window=false", "--show-line-numbers", "--copy") err := cmd.Run() if err != nil { t.Fatal(err) } - - _, err = os.Stat(output) - if err != nil { - t.Fatal(err) - } - err = clipboard.Init() if err != nil { t.Fatal(err) } - outputFile, err := os.ReadFile(output) - if err != nil { - t.Fatal(err) - } - clipboard.Write(clipboard.FmtImage, outputFile) png := clipboard.Read(clipboard.FmtImage) if png == nil { t.Fatal("clipboard is empty") From 197f8b933e35b8ed418f86488270e7f1d24178e0 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Sun, 9 Jun 2024 21:29:26 +0200 Subject: [PATCH 08/19] refactor: `copyToClipboard` function --- png.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/png.go b/png.go index edd26bf..239b8d5 100644 --- a/png.go +++ b/png.go @@ -12,7 +12,16 @@ import ( "golang.design/x/clipboard" ) -func libsvgConvert(doc *etree.Document, w, h float64, output string) error { +func copyToClipboard(svg []byte) error { + err := clipboard.Init() + if err != nil { + return err + } + clipboard.Write(clipboard.FmtImage, svg) + clipboard.Read(clipboard.FmtImage) + return err +} + _, err := exec.LookPath("rsvg-convert") if err != nil { return err @@ -96,12 +105,10 @@ func resvgConvert(doc *etree.Document, w, h float64, output string, toClipboard return err } if toClipboard { - err = clipboard.Init() + err = copyToClipboard(png) if err != nil { return err } - clipboard.Write(clipboard.FmtImage, png) - clipboard.Read(clipboard.FmtImage) } return err } From bb68f3bb9654a93764ba1e6807599aaf2d2ab4ae Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Sun, 9 Jun 2024 21:29:48 +0200 Subject: [PATCH 09/19] feat: copy when using `libsvg` --- png.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/png.go b/png.go index 239b8d5..1ebe285 100644 --- a/png.go +++ b/png.go @@ -22,6 +22,7 @@ func copyToClipboard(svg []byte) error { return err } +func libsvgConvert(doc *etree.Document, w, h float64, output string, toClipboard bool) error { _, err := exec.LookPath("rsvg-convert") if err != nil { return err @@ -37,6 +38,16 @@ func copyToClipboard(svg []byte) error { rsvgConvert := exec.Command("rsvg-convert", "-o", output) rsvgConvert.Stdin = bytes.NewReader(svg) err = rsvgConvert.Run() + if err != nil { + return err + } + + if toClipboard { + err = copyToClipboard(svg) + if err != nil { + return err + } + } return err } From f678cdadbb6f7f677565dfc9f6dc903af7bce8b4 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Sun, 9 Jun 2024 21:30:17 +0200 Subject: [PATCH 10/19] test(copy): improved performance using `svg` --- freeze_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freeze_test.go b/freeze_test.go index f27665b..f2722b2 100644 --- a/freeze_test.go +++ b/freeze_test.go @@ -58,7 +58,7 @@ func TestFreezeOutput(t *testing.T) { } func TestFreezeCopy(t *testing.T) { - output := "bubbletea-copy.png" + output := "bubbletea-copy.svg" defer os.Remove(output) cmd := exec.Command(binary, "test/input/bubbletea.model", "-o", output, "--language", "go", "--height", "800", "--width", "750", "--config", "full", "--window=false", "--show-line-numbers", "--copy") From 153ba1fedb8b94ff2e4446b0910dafc7e7b8787d Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Sun, 9 Jun 2024 21:30:45 +0200 Subject: [PATCH 11/19] feat: copy when output not only `.png` --- main.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 6fc14cb..daab3ea 100644 --- a/main.go +++ b/main.go @@ -391,7 +391,7 @@ func main() { switch { case strings.HasSuffix(config.Output, ".png"): // use libsvg conversion. - svgConversionErr := libsvgConvert(doc, imageWidth, imageHeight, config.Output) + svgConversionErr := libsvgConvert(doc, imageWidth, imageHeight, config.Output, config.Copy) if svgConversionErr == nil { printFilenameOutput(config.Output) break @@ -407,6 +407,16 @@ func main() { default: // output file specified. if config.Output != "" { + outputSvg, err := doc.WriteToBytes() + if err != nil { + printErrorFatal("Unable to write output", err) + } + if config.Copy { + err = copyToClipboard(outputSvg) + if err != nil { + printErrorFatal("Unable to copy to clipboard", err) + } + } err = doc.WriteToFile(config.Output) if err != nil { printErrorFatal("Unable to write output", err) From 15accc987e02d12c08fac8d206cce717a01aa92b Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Tue, 11 Jun 2024 19:49:31 +0200 Subject: [PATCH 12/19] added comment while figuring out a solution --- png.go | 1 + 1 file changed, 1 insertion(+) diff --git a/png.go b/png.go index 1ebe285..4729a4a 100644 --- a/png.go +++ b/png.go @@ -42,6 +42,7 @@ func libsvgConvert(doc *etree.Document, w, h float64, output string, toClipboard return err } + // TODO: Get the converted image and copy it to the clipboard. if toClipboard { err = copyToClipboard(svg) if err != nil { From 164655c5b562e2b1bd8685a2eb95111931e8ace1 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Wed, 12 Jun 2024 12:19:59 +0200 Subject: [PATCH 13/19] feat: modified to work on non-png formats as text With [gclip](https://github.com/golang-design/clipboard) when not copying a png image `gclip -copy -f freeze.svg` it will copy it as text, otherwise as an image --- freeze_test.go | 21 ++++++++++++++++++++- main.go | 28 +++++++++++++++++++++------- png.go | 24 +++++++++--------------- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/freeze_test.go b/freeze_test.go index f2722b2..e39830d 100644 --- a/freeze_test.go +++ b/freeze_test.go @@ -57,10 +57,29 @@ func TestFreezeOutput(t *testing.T) { } } -func TestFreezeCopy(t *testing.T) { +func TestFreezeCopyNonPng(t *testing.T) { output := "bubbletea-copy.svg" defer os.Remove(output) + cmd := exec.Command(binary, "test/input/bubbletea.model", "-o", output, "--language", "go", "--height", "800", "--width", "750", "--config", "full", "--window=false", "--show-line-numbers", "--copy") + err := cmd.Run() + if err != nil { + t.Fatal(err) + } + err = clipboard.Init() + if err != nil { + t.Fatal(err) + } + png := clipboard.Read(clipboard.FmtText) + if png == nil { + t.Fatal("clipboard is empty") + } +} + +func TestFreezeCopyPng(t *testing.T) { + output := "bubbletea-copy.png" + defer os.Remove(output) + cmd := exec.Command(binary, "test/input/bubbletea.model", "-o", output, "--language", "go", "--height", "800", "--width", "750", "--config", "full", "--window=false", "--show-line-numbers", "--copy") err := cmd.Run() if err != nil { diff --git a/main.go b/main.go index daab3ea..3c348cb 100644 --- a/main.go +++ b/main.go @@ -393,6 +393,16 @@ func main() { // use libsvg conversion. svgConversionErr := libsvgConvert(doc, imageWidth, imageHeight, config.Output, config.Copy) if svgConversionErr == nil { + if config.Copy { + outputPNG, err := os.ReadFile(config.Output) + if err != nil { + printErrorFatal("Unable to read output to copy to clipboard", err) + } + err = copyToClipboard(outputPNG, config.Output) + if err != nil { + printErrorFatal("Unable to copy to clipboard", err) + } + } printFilenameOutput(config.Output) break } @@ -407,20 +417,24 @@ func main() { default: // output file specified. if config.Output != "" { - outputSvg, err := doc.WriteToBytes() + _, err := doc.WriteToBytes() if err != nil { printErrorFatal("Unable to write output", err) } - if config.Copy { - err = copyToClipboard(outputSvg) - if err != nil { - printErrorFatal("Unable to copy to clipboard", err) - } - } err = doc.WriteToFile(config.Output) if err != nil { printErrorFatal("Unable to write output", err) } + if config.Copy { // TODO: figure out how to copy `.svg` and `.webp` files as images + outputSVG, err := os.ReadFile(config.Output) // gclip aka golang-desing/clipboard copies a broken image if clipboard.FmtImage + if err != nil { + printErrorFatal("Unable to read output to copy to clipboard", err) + } + err = copyToClipboard(outputSVG, config.Output) + if err != nil { + printErrorFatal("Unable to copy to clipboard", err) + } + } printFilenameOutput(config.Output) return } diff --git a/png.go b/png.go index 4729a4a..767652a 100644 --- a/png.go +++ b/png.go @@ -5,6 +5,7 @@ import ( "context" "os" "os/exec" + "strings" "github.com/beevik/etree" "github.com/charmbracelet/freeze/font" @@ -12,13 +13,17 @@ import ( "golang.design/x/clipboard" ) -func copyToClipboard(svg []byte) error { +func copyToClipboard(img []byte, file string) error { err := clipboard.Init() if err != nil { return err } - clipboard.Write(clipboard.FmtImage, svg) - clipboard.Read(clipboard.FmtImage) + format := clipboard.FmtImage + if strings.HasSuffix(file, ".svg") || strings.HasSuffix(file, ".webp") { + format = clipboard.FmtText + } + clipboard.Write(format, img) + clipboard.Read(format) return err } @@ -38,17 +43,6 @@ func libsvgConvert(doc *etree.Document, w, h float64, output string, toClipboard rsvgConvert := exec.Command("rsvg-convert", "-o", output) rsvgConvert.Stdin = bytes.NewReader(svg) err = rsvgConvert.Run() - if err != nil { - return err - } - - // TODO: Get the converted image and copy it to the clipboard. - if toClipboard { - err = copyToClipboard(svg) - if err != nil { - return err - } - } return err } @@ -117,7 +111,7 @@ func resvgConvert(doc *etree.Document, w, h float64, output string, toClipboard return err } if toClipboard { - err = copyToClipboard(png) + err = copyToClipboard(png, output) if err != nil { return err } From 9a169d595672a66e4a281eb631b37ee8101f6707 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Wed, 12 Jun 2024 13:23:26 +0200 Subject: [PATCH 14/19] refactor(copy): `--copy` -> `--output clipboard` --- README.md | 3 +- config.go | 1 - freeze_test.go | 27 ++---------- interactive.go | 5 --- main.go | 111 ++++++++++++++++++++++++------------------------- png.go | 28 ++++--------- 6 files changed, 68 insertions(+), 107 deletions(-) diff --git a/README.md b/README.md index eda601f..24d1ee9 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,6 @@ Screenshots can be customized with `--flags` or [Configuration](#configuration) - [`--font.file`](#font): File path to the font to use (embedded in the SVG). - [`--line-height`](#font): Line height relative to font size. - [`--show-line-numbers`](#line-numbers): Show line numbers. -- [`--copy`](#copy): Copy the output image to the clipboard. - [`--lines`](#line-numbers): Lines to capture (start,end). ### Language @@ -165,7 +164,7 @@ freeze main.go --output out.{svg,png,webp} Copy the output image to your clipboard, so you can paste it anywhere. ```bash -freeze main.go --copy +freeze main.go --output clipboard ``` ### Font diff --git a/config.go b/config.go index d86ebb8..09448f0 100644 --- a/config.go +++ b/config.go @@ -31,7 +31,6 @@ 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:""` diff --git a/freeze_test.go b/freeze_test.go index e39830d..e712e9b 100644 --- a/freeze_test.go +++ b/freeze_test.go @@ -57,30 +57,11 @@ func TestFreezeOutput(t *testing.T) { } } -func TestFreezeCopyNonPng(t *testing.T) { - output := "bubbletea-copy.svg" +func TestFreezeCopy(t *testing.T) { + output := "clipboard" defer os.Remove(output) - cmd := exec.Command(binary, "test/input/bubbletea.model", "-o", output, "--language", "go", "--height", "800", "--width", "750", "--config", "full", "--window=false", "--show-line-numbers", "--copy") - err := cmd.Run() - if err != nil { - t.Fatal(err) - } - err = clipboard.Init() - if err != nil { - t.Fatal(err) - } - png := clipboard.Read(clipboard.FmtText) - if png == nil { - t.Fatal("clipboard is empty") - } -} - -func TestFreezeCopyPng(t *testing.T) { - output := "bubbletea-copy.png" - defer os.Remove(output) - - cmd := exec.Command(binary, "test/input/bubbletea.model", "-o", output, "--language", "go", "--height", "800", "--width", "750", "--config", "full", "--window=false", "--show-line-numbers", "--copy") + cmd := exec.Command(binary, "test/input/bubbletea.model", "-o", output, "--language", "go", "--height", "800", "--width", "750", "--config", "full", "--window=false", "--show-line-numbers") err := cmd.Run() if err != nil { t.Fatal(err) @@ -175,7 +156,7 @@ func TestFreezeConfigurations(t *testing.T) { }, { input: "test/input/bubbletea.model", - flags: []string{"--language", "go", "--height", "800", "--width", "750", "--config", "full", "--window=false", "--show-line-numbers", "--copy"}, + flags: []string{"--language", "go", "--height", "800", "--width", "750", "--config", "full", "--window=false", "--show-line-numbers"}, output: "bubbletea-copy", }, // { diff --git a/interactive.go b/interactive.go index 7ea6d8f..e7916cf 100644 --- a/interactive.go +++ b/interactive.go @@ -67,11 +67,6 @@ 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). diff --git a/main.go b/main.go index 3c348cb..7c9fd8a 100644 --- a/main.go +++ b/main.go @@ -388,81 +388,78 @@ func main() { istty := isatty.IsTerminal(os.Stdout.Fd()) - switch { - case strings.HasSuffix(config.Output, ".png"): - // use libsvg conversion. - svgConversionErr := libsvgConvert(doc, imageWidth, imageHeight, config.Output, config.Copy) - if svgConversionErr == nil { - if config.Copy { - outputPNG, err := os.ReadFile(config.Output) + if config.Output != "" { + outputName := config.Output + if config.Output == "clipboard" { + outputName = "clipboard.png" + } + switch { + case strings.HasSuffix(outputName, ".png"): + // use libsvg conversion. + svgConversionErr := libsvgConvert(doc, imageWidth, imageHeight, outputName) + if svgConversionErr == nil { + if config.Output == "clipboard" { + outputPNG, err := os.ReadFile(outputName) + defer os.Remove(outputName) + if err != nil { + printErrorFatal("Unable to read output to copy to clipboard", err) + } + err = copyToClipboard(outputPNG) + if err != nil { + printErrorFatal("Unable to copy to clipboard", err) + } + } + printFilenameOutput(config.Output) + break + } + + // could not convert with libsvg, try resvg + svgConversionErr = resvgConvert(doc, imageWidth, imageHeight, config.Output) + if svgConversionErr != nil { + printErrorFatal("Unable to convert SVG to PNG", svgConversionErr) + } + printFilenameOutput(config.Output) + + default: + // output file specified. + if config.Output != "" { + _, err := doc.WriteToBytes() if err != nil { - printErrorFatal("Unable to read output to copy to clipboard", err) + printErrorFatal("Unable to write output", err) } - err = copyToClipboard(outputPNG, config.Output) + err = doc.WriteToFile(config.Output) if err != nil { - printErrorFatal("Unable to copy to clipboard", err) + printErrorFatal("Unable to write output", err) } + printFilenameOutput(config.Output) + return } - printFilenameOutput(config.Output) - break - } - - // could not convert with libsvg, try resvg - svgConversionErr = resvgConvert(doc, imageWidth, imageHeight, config.Output, config.Copy) - if svgConversionErr != nil { - printErrorFatal("Unable to convert SVG to PNG", svgConversionErr) - } - printFilenameOutput(config.Output) - default: - // output file specified. - if config.Output != "" { - _, err := doc.WriteToBytes() - if err != nil { - printErrorFatal("Unable to write output", err) - } - err = doc.WriteToFile(config.Output) - if err != nil { - printErrorFatal("Unable to write output", err) - } - if config.Copy { // TODO: figure out how to copy `.svg` and `.webp` files as images - outputSVG, err := os.ReadFile(config.Output) // gclip aka golang-desing/clipboard copies a broken image if clipboard.FmtImage - if err != nil { - printErrorFatal("Unable to read output to copy to clipboard", err) + // reading from stdin. + if config.Input == "" || config.Input == "-" { + if istty { + err = doc.WriteToFile(defaultOutputFilename) + printFilenameOutput(defaultOutputFilename) + } else { + _, err = doc.WriteTo(os.Stdout) } - err = copyToClipboard(outputSVG, config.Output) if err != nil { - printErrorFatal("Unable to copy to clipboard", err) + printErrorFatal("Unable to write output", err) } + return } - printFilenameOutput(config.Output) - return - } - // reading from stdin. - if config.Input == "" || config.Input == "-" { + // reading from file. if istty { - err = doc.WriteToFile(defaultOutputFilename) - printFilenameOutput(defaultOutputFilename) + config.Output = strings.TrimSuffix(filepath.Base(config.Input), filepath.Ext(config.Input)) + ".svg" + err = doc.WriteToFile(config.Output) + printFilenameOutput(config.Output) } else { _, err = doc.WriteTo(os.Stdout) } if err != nil { printErrorFatal("Unable to write output", err) } - return - } - - // reading from file. - if istty { - config.Output = strings.TrimSuffix(filepath.Base(config.Input), filepath.Ext(config.Input)) + ".svg" - err = doc.WriteToFile(config.Output) - printFilenameOutput(config.Output) - } else { - _, err = doc.WriteTo(os.Stdout) - } - if err != nil { - printErrorFatal("Unable to write output", err) } } } diff --git a/png.go b/png.go index 767652a..d0eaffe 100644 --- a/png.go +++ b/png.go @@ -5,7 +5,6 @@ import ( "context" "os" "os/exec" - "strings" "github.com/beevik/etree" "github.com/charmbracelet/freeze/font" @@ -13,21 +12,17 @@ import ( "golang.design/x/clipboard" ) -func copyToClipboard(img []byte, file string) error { +func copyToClipboard(img []byte) error { err := clipboard.Init() if err != nil { return err } - format := clipboard.FmtImage - if strings.HasSuffix(file, ".svg") || strings.HasSuffix(file, ".webp") { - format = clipboard.FmtText - } - clipboard.Write(format, img) - clipboard.Read(format) + clipboard.Write(clipboard.FmtImage, img) + clipboard.Read(clipboard.FmtImage) return err } -func libsvgConvert(doc *etree.Document, w, h float64, output string, toClipboard bool) error { +func libsvgConvert(doc *etree.Document, w, h float64, output string) error { _, err := exec.LookPath("rsvg-convert") if err != nil { return err @@ -46,7 +41,7 @@ func libsvgConvert(doc *etree.Document, w, h float64, output string, toClipboard return err } -func resvgConvert(doc *etree.Document, w, h float64, output string, toClipboard bool) error { +func resvgConvert(doc *etree.Document, w, h float64, output string) error { svg, err := doc.WriteToBytes() if err != nil { return err @@ -106,15 +101,10 @@ func resvgConvert(doc *etree.Document, w, h float64, output string, toClipboard return err } - err = os.WriteFile(output, png, 0644) - if err != nil { - return err - } - if toClipboard { - err = copyToClipboard(png, output) - if err != nil { - return err - } + if output == "clipboard" { + err = copyToClipboard(png) + } else { + err = os.WriteFile(output, png, 0644) } return err } From 01d684acc86eb14c04cf690ac39382173aa83a73 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Wed, 12 Jun 2024 13:30:12 +0200 Subject: [PATCH 15/19] refactor(copy): moved logic to function --- main.go | 11 ----------- png.go | 12 ++++++++++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/main.go b/main.go index 7c9fd8a..2d0d6c0 100644 --- a/main.go +++ b/main.go @@ -398,17 +398,6 @@ func main() { // use libsvg conversion. svgConversionErr := libsvgConvert(doc, imageWidth, imageHeight, outputName) if svgConversionErr == nil { - if config.Output == "clipboard" { - outputPNG, err := os.ReadFile(outputName) - defer os.Remove(outputName) - if err != nil { - printErrorFatal("Unable to read output to copy to clipboard", err) - } - err = copyToClipboard(outputPNG) - if err != nil { - printErrorFatal("Unable to copy to clipboard", err) - } - } printFilenameOutput(config.Output) break } diff --git a/png.go b/png.go index d0eaffe..c80146b 100644 --- a/png.go +++ b/png.go @@ -5,6 +5,7 @@ import ( "context" "os" "os/exec" + "strings" "github.com/beevik/etree" "github.com/charmbracelet/freeze/font" @@ -38,6 +39,17 @@ func libsvgConvert(doc *etree.Document, w, h float64, output string) error { rsvgConvert := exec.Command("rsvg-convert", "-o", output) rsvgConvert.Stdin = bytes.NewReader(svg) err = rsvgConvert.Run() + if err != nil { + return err + } + if strings.Contains(output, "clipboard") { + png, err := os.ReadFile(output) + defer os.Remove(output) + if err != nil { + return err + } + err = copyToClipboard(png) + } return err } From 457f7d4fd3b98f87cfe89ec9968482044016cbda Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Wed, 12 Jun 2024 13:33:54 +0200 Subject: [PATCH 16/19] fix: unnecessary if statment --- main.go | 94 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 46 insertions(+), 48 deletions(-) diff --git a/main.go b/main.go index 2d0d6c0..e99c3fc 100644 --- a/main.go +++ b/main.go @@ -388,67 +388,65 @@ func main() { istty := isatty.IsTerminal(os.Stdout.Fd()) - if config.Output != "" { - outputName := config.Output - if config.Output == "clipboard" { - outputName = "clipboard.png" + outputName := config.Output + if config.Output == "clipboard" { + outputName = "clipboard.png" + } + switch { + case strings.HasSuffix(outputName, ".png"): + // use libsvg conversion. + svgConversionErr := libsvgConvert(doc, imageWidth, imageHeight, outputName) + if svgConversionErr == nil { + printFilenameOutput(config.Output) + break } - switch { - case strings.HasSuffix(outputName, ".png"): - // use libsvg conversion. - svgConversionErr := libsvgConvert(doc, imageWidth, imageHeight, outputName) - if svgConversionErr == nil { - printFilenameOutput(config.Output) - break - } - // could not convert with libsvg, try resvg - svgConversionErr = resvgConvert(doc, imageWidth, imageHeight, config.Output) - if svgConversionErr != nil { - printErrorFatal("Unable to convert SVG to PNG", svgConversionErr) - } - printFilenameOutput(config.Output) + // could not convert with libsvg, try resvg + svgConversionErr = resvgConvert(doc, imageWidth, imageHeight, config.Output) + if svgConversionErr != nil { + printErrorFatal("Unable to convert SVG to PNG", svgConversionErr) + } + printFilenameOutput(config.Output) - default: - // output file specified. - if config.Output != "" { - _, err := doc.WriteToBytes() - if err != nil { - printErrorFatal("Unable to write output", err) - } - err = doc.WriteToFile(config.Output) - if err != nil { - printErrorFatal("Unable to write output", err) - } - printFilenameOutput(config.Output) - return + default: + // output file specified. + if config.Output != "" { + _, err := doc.WriteToBytes() + if err != nil { + printErrorFatal("Unable to write output", err) } - - // reading from stdin. - if config.Input == "" || config.Input == "-" { - if istty { - err = doc.WriteToFile(defaultOutputFilename) - printFilenameOutput(defaultOutputFilename) - } else { - _, err = doc.WriteTo(os.Stdout) - } - if err != nil { - printErrorFatal("Unable to write output", err) - } - return + err = doc.WriteToFile(config.Output) + if err != nil { + printErrorFatal("Unable to write output", err) } + printFilenameOutput(config.Output) + return + } - // reading from file. + // reading from stdin. + if config.Input == "" || config.Input == "-" { if istty { - config.Output = strings.TrimSuffix(filepath.Base(config.Input), filepath.Ext(config.Input)) + ".svg" - err = doc.WriteToFile(config.Output) - printFilenameOutput(config.Output) + err = doc.WriteToFile(defaultOutputFilename) + printFilenameOutput(defaultOutputFilename) } else { _, err = doc.WriteTo(os.Stdout) } if err != nil { printErrorFatal("Unable to write output", err) } + return + } + + // reading from file. + if istty { + config.Output = strings.TrimSuffix(filepath.Base(config.Input), filepath.Ext(config.Input)) + ".svg" + err = doc.WriteToFile(config.Output) + printFilenameOutput(config.Output) + } else { + _, err = doc.WriteTo(os.Stdout) + } + if err != nil { + printErrorFatal("Unable to write output", err) } } } From b88ebf74cf0dfff2c91d4399769cf618e31aef30 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Wed, 12 Jun 2024 13:45:01 +0200 Subject: [PATCH 17/19] feat: added `copy` output to copy to clipboard --- README.md | 1 + main.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 24d1ee9..d0d17fe 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,7 @@ Copy the output image to your clipboard, so you can paste it anywhere. ```bash freeze main.go --output clipboard +freeze main.go --output copy ``` ### Font diff --git a/main.go b/main.go index e99c3fc..596a6f1 100644 --- a/main.go +++ b/main.go @@ -389,7 +389,7 @@ func main() { istty := isatty.IsTerminal(os.Stdout.Fd()) outputName := config.Output - if config.Output == "clipboard" { + if config.Output == "clipboard" || config.Output == "copy" { outputName = "clipboard.png" } switch { From 6d74d8cfef0ae107377b12597a8b761fd08ba744 Mon Sep 17 00:00:00 2001 From: AlejandroSuero Date: Fri, 20 Sep 2024 08:46:51 +0200 Subject: [PATCH 18/19] fix: build issues after solving conflicts --- go.mod | 13 +++---------- go.sum | 16 ++++------------ png.go | 3 ++- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index 8198d08..14b7f32 100644 --- a/go.mod +++ b/go.mod @@ -19,11 +19,9 @@ require ( github.com/creack/pty v1.1.23 github.com/kanrichan/resvg-go v0.0.2-0.20231001163256-63db194ca9f5 github.com/mattn/go-isatty v0.0.20 - github.com/mattn/go-runewidth v0.0.15 - golang.design/x/clipboard v0.7.0 - golang.org/x/sys v0.18.0 github.com/mattn/go-runewidth v0.0.16 github.com/muesli/reflow v0.3.0 + golang.design/x/clipboard v0.7.0 golang.org/x/sys v0.25.0 ) @@ -47,16 +45,11 @@ require ( github.com/muesli/cancelreader v0.2.2 // indirect github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a // indirect github.com/rivo/uniseg v0.4.7 // indirect - github.com/tetratelabs/wazero v1.7.0 // indirect - golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect + github.com/tetratelabs/wazero v1.8.0 // indirect + golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e // indirect golang.org/x/exp/shiny v0.0.0-20240506185415-9bf2ced13842 // indirect golang.org/x/image v0.14.0 // indirect golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect - golang.org/x/sync v0.6.0 // indirect - golang.org/x/term v0.18.0 // indirect - golang.org/x/text v0.14.0 // indirect - github.com/tetratelabs/wazero v1.8.0 // indirect - golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/text v0.18.0 // indirect ) diff --git a/go.sum b/go.sum index 944c80a..8d85d96 100644 --- a/go.sum +++ b/go.sum @@ -85,26 +85,18 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/tetratelabs/wazero v1.7.0 h1:jg5qPydno59wqjpGrHph81lbtHzTrWzwwtD4cD88+hQ= -github.com/tetratelabs/wazero v1.7.0/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y= +github.com/tetratelabs/wazero v1.8.0 h1:iEKu0d4c2Pd+QSRieYbnQC9yiFlMS9D+Jr0LsRmcF4g= +github.com/tetratelabs/wazero v1.8.0/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs= golang.design/x/clipboard v0.7.0 h1:4Je8M/ys9AJumVnl8m+rZnIvstSnYj1fvzqYrU3TXvo= golang.design/x/clipboard v0.7.0/go.mod h1:PQIvqYO9GP29yINEfsEn5zSQKAz3UgXmZKzDA6dnq2E= -golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw= -golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ= +golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e h1:I88y4caeGeuDQxgdoFPUq097j7kNfw6uvuiNxUBfcBk= +golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ= golang.org/x/exp/shiny v0.0.0-20240506185415-9bf2ced13842 h1:kEvPiBVeT1JJGw/3THfe1W1zvTAvU1V6pCFV0icZvQs= golang.org/x/exp/shiny v0.0.0-20240506185415-9bf2ced13842/go.mod h1:3F+MieQB7dRYLTmnncoFbb1crS5lfQoTfDgQy6K4N0o= golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a h1:sYbmY3FwUWCBTodZL1S3JUuOvaW6kM2o+clDzzDNBWg= golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a/go.mod h1:Ede7gF0KGoHlj822RtphAHK1jLdrcuRBZg0sF1Q+SPc= -golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= -golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -github.com/tetratelabs/wazero v1.8.0 h1:iEKu0d4c2Pd+QSRieYbnQC9yiFlMS9D+Jr0LsRmcF4g= -github.com/tetratelabs/wazero v1.8.0/go.mod h1:yAI0XTsMBhREkM/YDAK/zNou3GoiAce1P6+rp/wQhjs= -golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e h1:I88y4caeGeuDQxgdoFPUq097j7kNfw6uvuiNxUBfcBk= -golang.org/x/exp v0.0.0-20240904232852-e7e105dedf7e/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/png.go b/png.go index 5bbb2bb..fd3d4b1 100644 --- a/png.go +++ b/png.go @@ -116,7 +116,8 @@ func resvgConvert(doc *etree.Document, w, h float64, output string) error { if output == "clipboard" { err = copyToClipboard(png) } else { - err = os.WriteFile(output, png, 0o600) + err = os.WriteFile(output, png, 0o600) + } return err //nolint: wrapcheck } From 87af18fdc0881eef70b952a600b24190e3c66e4e Mon Sep 17 00:00:00 2001 From: bashbunni Date: Thu, 5 Dec 2024 14:46:46 -0800 Subject: [PATCH 19/19] feat(clipboard): remove copy as output source --- README.md | 1 - main.go | 2 +- png.go | 9 +++------ 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d0d17fe..24d1ee9 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,6 @@ Copy the output image to your clipboard, so you can paste it anywhere. ```bash freeze main.go --output clipboard -freeze main.go --output copy ``` ### Font diff --git a/main.go b/main.go index 69a7cec..c5cd5f3 100644 --- a/main.go +++ b/main.go @@ -396,7 +396,7 @@ func main() { istty := isatty.IsTerminal(os.Stdout.Fd()) outputName := config.Output - if config.Output == "clipboard" || config.Output == "copy" { + if config.Output == "clipboard" { outputName = "clipboard.png" } switch { diff --git a/png.go b/png.go index fd3d4b1..c14c039 100644 --- a/png.go +++ b/png.go @@ -48,7 +48,7 @@ func libsvgConvert(doc *etree.Document, _, _ float64, output string) error { if err != nil { return err } - err = copyToClipboard(png) + return copyToClipboard(png) } return err //nolint: wrapcheck } @@ -114,10 +114,7 @@ func resvgConvert(doc *etree.Document, w, h float64, output string) error { } if output == "clipboard" { - err = copyToClipboard(png) - } else { - err = os.WriteFile(output, png, 0o600) + return copyToClipboard(png) } - - return err //nolint: wrapcheck + return os.WriteFile(output, png, 0o600) }