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 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 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
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
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: 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
}
31 changes: 31 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 @@ -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)
Expand Down Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
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)
}
AlejandroSuero marked this conversation as resolved.
Show resolved Hide resolved
return err
}
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.