Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#58): add copy to clipboard flag --copy #97

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
236cd0a
feat(#58): added `golang.design/x/clipboard` package
AlejandroSuero May 19, 2024
37dafec
feat(#58): added `--copy` flag and config setting
AlejandroSuero May 19, 2024
d4d556c
test(#58): tested `--copy` flag
AlejandroSuero May 19, 2024
4c4f2bd
docs(#58): update `README.md`, golden and `full.json` with `--copy`
AlejandroSuero May 19, 2024
eba42f9
fix(test): writting image to clipboard
AlejandroSuero May 27, 2024
c57172d
feat: added `clipboard.Read` and modified `copy` in case of conflict
AlejandroSuero May 31, 2024
96cb243
test: updated test to follow `TestFreezeConfigurations`
AlejandroSuero Jun 5, 2024
197f8b9
refactor: `copyToClipboard` function
AlejandroSuero Jun 9, 2024
bb68f3b
feat: copy when using `libsvg`
AlejandroSuero Jun 9, 2024
f678cda
test(copy): improved performance using `svg`
AlejandroSuero Jun 9, 2024
153ba1f
feat: copy when output not only `.png`
AlejandroSuero Jun 9, 2024
15accc9
added comment while figuring out a solution
AlejandroSuero Jun 11, 2024
164655c
feat: modified to work on non-png formats as text
AlejandroSuero Jun 12, 2024
9a169d5
refactor(copy): `--copy` -> `--output clipboard`
AlejandroSuero Jun 12, 2024
01d684a
refactor(copy): moved logic to function
AlejandroSuero Jun 12, 2024
457f7d4
fix: unnecessary if statment
AlejandroSuero Jun 12, 2024
b88ebf7
feat: added `copy` output to copy to clipboard
AlejandroSuero Jun 12, 2024
724101b
Merge branch 'main' into feature/add-copy-to-clipboard
AlejandroSuero Sep 20, 2024
6d74d8c
fix: build issues after solving conflicts
AlejandroSuero Sep 20, 2024
87af18f
feat(clipboard): remove copy as output source
bashbunni Dec 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,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 --output clipboard
```

### Font

Specify the font family, font size, and font line height of the output image.
Expand Down
5 changes: 3 additions & 2 deletions configurations/full.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"size": 14,
"ligatures": true
},
"line_height": 1.2
}
"line_height": 1.2,
"copy": false
}
25 changes: 25 additions & 0 deletions freeze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing"

"github.com/aymanbagabas/go-udiff"
"golang.design/x/clipboard"
)

const binary = "./test/freeze-test"
Expand Down Expand Up @@ -58,6 +59,25 @@ func TestFreezeOutput(t *testing.T) {
}
}

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")
err := cmd.Run()
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)
Expand Down Expand Up @@ -136,6 +156,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"},
output: "bubbletea-copy",
},
// {
// flags: []string{"--execute", "layout", "--height", "800", "--config", "full", "--margin", "50,10"},
// output: "composite-2",
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/mattn/go-isatty v0.0.20
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
)

Expand All @@ -46,6 +47,9 @@ require (
github.com/rivo/uniseg v0.4.7 // 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.8.0 // indirect
golang.org/x/text v0.18.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,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.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-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.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=
Expand Down
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,14 @@ func main() {

istty := isatty.IsTerminal(os.Stdout.Fd())

outputName := config.Output
if config.Output == "clipboard" {
outputName = "clipboard.png"
}
switch {
case strings.HasSuffix(config.Output, ".png"):
case strings.HasSuffix(outputName, ".png"):
// use libsvg conversion.
svgConversionErr := libsvgConvert(doc, imageWidth, imageHeight, config.Output)
svgConversionErr := libsvgConvert(doc, imageWidth, imageHeight, outputName)
if svgConversionErr == nil {
printFilenameOutput(config.Output)
break
Expand All @@ -414,6 +418,10 @@ func main() {
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)
Expand Down
30 changes: 26 additions & 4 deletions png.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@ import (
"context"
"os"
"os/exec"
"strings"

"github.com/beevik/etree"
"github.com/charmbracelet/freeze/font"
"github.com/kanrichan/resvg-go"
"golang.design/x/clipboard"
)

func copyToClipboard(img []byte) error {
err := clipboard.Init()
if err != nil {
return err
}
clipboard.Write(clipboard.FmtImage, img)
clipboard.Read(clipboard.FmtImage)
return err
}

func libsvgConvert(doc *etree.Document, _, _ float64, output string) error {
_, err := exec.LookPath("rsvg-convert")
if err != nil {
Expand All @@ -27,6 +39,17 @@ func libsvgConvert(doc *etree.Document, _, _ 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
}
return copyToClipboard(png)
}
return err //nolint: wrapcheck
}

Expand Down Expand Up @@ -90,9 +113,8 @@ func resvgConvert(doc *etree.Document, w, h float64, output string) error {
return err //nolint: wrapcheck
}

err = os.WriteFile(output, png, 0o600)
if err != nil {
return err //nolint: wrapcheck
if output == "clipboard" {
return copyToClipboard(png)
}
return err //nolint: wrapcheck
return os.WriteFile(output, png, 0o600)
}
49 changes: 49 additions & 0 deletions test/golden/svg/bubbletea-copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.