-
Notifications
You must be signed in to change notification settings - Fork 74
feat(#58): add copy to clipboard flag --copy
#97
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
base: main
Are you sure you want to change the base?
Changes from 23 commits
236cd0a
37dafec
d4d556c
4c4f2bd
eba42f9
c57172d
96cb243
197f8b9
bb68f3b
f678cda
153ba1f
15accc9
164655c
9a169d5
01d684a
457f7d4
b88ebf7
724101b
6d74d8c
87af18f
66788be
f2263b6
8233445
74cf67b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,5 +29,6 @@ | |
| "size": 14, | ||
| "ligatures": true | ||
| }, | ||
| "line_height": 1.2 | ||
| } | ||
| "line_height": 1.2, | ||
| "copy": false | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,12 +5,24 @@ | |
| "context" | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| "github.com/beevik/etree" | ||
| "github.com/charmbracelet/freeze/font" | ||
| "github.com/kanrichan/resvg-go" | ||
| "golang.design/x/clipboard" | ||
|
Check failure on line 13 in png.go
|
||
| ) | ||
|
|
||
| func copyToClipboard(img []byte) error { | ||
| err := clipboard.Init() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| clipboard.Write(clipboard.FmtImage, img) | ||
| clipboard.Read(clipboard.FmtImage) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't think you need this read here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From when I was testing it, it seemed that if the |
||
| return err | ||
| } | ||
|
|
||
| func libsvgConvert(doc *etree.Document, _, _ float64, output string) error { | ||
| _, err := exec.LookPath("rsvg-convert") | ||
| if err != nil { | ||
|
|
@@ -27,6 +39,17 @@ | |
| rsvgConvert := exec.Command("rsvg-convert", "-o", output) | ||
| rsvgConvert.Stdin = bytes.NewReader(svg) | ||
| err = rsvgConvert.Run() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if strings.HasPrefix(output, "clipboard") { | ||
| png, err := os.ReadFile(output) | ||
| defer os.Remove(output) // nolint: errcheck | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return copyToClipboard(png) | ||
| } | ||
| return err //nolint: wrapcheck | ||
| } | ||
|
|
||
|
|
@@ -90,9 +113,8 @@ | |
| 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) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't we then copy the svg as a text file instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can try do it that way, but if you want to copy the image to share when pasting, for example in a GitHub comment or in another app. If it is a text file, it won't recognise it as an image. Maybe if pasting it in a web browser to open the image there it will work.
It can be checked if the desired output is
.svgthat it copies it as text. But in the case that you want to share the image, it will have to be done as mentioned above.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, you could still paste the SVG into a html file for example... not sure about other places
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I get your point, it's useful for that case. I'll look into adding the copy as text when the desired output is
.svg.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No I think it should be png. Save to file for svg since that's most likely to be used with another program or in cases where we need a file to exist for it (e.g. HTML, or use in Figma, Illustrator, etc). Png is a more convenient format that lends itself well to being pasted anywhere.