Skip to content
This repository was archived by the owner on Mar 7, 2023. It is now read-only.

Commit 019d166

Browse files
committed
use clipboard library to copy image
1 parent fc672b2 commit 019d166

File tree

3 files changed

+17
-75
lines changed

3 files changed

+17
-75
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/alecthomas/chroma v0.8.0
77
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
88
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15
9+
github.com/skanehira/clipboard-image v0.0.0-20200806041554-0806bef063db
910
golang.org/x/image v0.0.0-20200618115811-c13761719519
1011
golang.org/x/term v0.0.0-20191110171634-ad39bd3f0407
1112
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
2727
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2828
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
2929
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
30+
github.com/skanehira/clipboard-image v0.0.0-20200806041554-0806bef063db h1:EbgK2pplPoQ5rDTr+8B+i7/TLkL+dT4W5wzD6Z69L9w=
31+
github.com/skanehira/clipboard-image v0.0.0-20200806041554-0806bef063db/go.mod h1:fkJ3gdzk9Erp35sWT85sioK4++fOfBJRs8BLMPn+GW0=
3032
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
3133
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
3234
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=

main.go

Lines changed: 14 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,15 @@ import (
1111
"io"
1212
"io/ioutil"
1313
"os"
14-
"os/exec"
1514
"path/filepath"
16-
"runtime"
1715
"strings"
1816

1917
"github.com/alecthomas/chroma"
2018
"github.com/alecthomas/chroma/formatters"
2119
"github.com/alecthomas/chroma/lexers"
2220
"github.com/alecthomas/chroma/styles"
2321
"github.com/golang/freetype/truetype"
22+
"github.com/skanehira/clipboard-image"
2423
"golang.org/x/image/font"
2524
"golang.org/x/image/math/fixed"
2625
"golang.org/x/term"
@@ -208,23 +207,6 @@ func (p *pngFormat) Format(w io.Writer, style *chroma.Style, iterator chroma.Ite
208207
}
209208

210209
func toImg(useClipboard bool, source, outFile string, lexer, style string) error {
211-
var (
212-
out *os.File
213-
err error
214-
)
215-
if useClipboard {
216-
out, err = ioutil.TempFile("", "")
217-
if err != nil {
218-
return err
219-
}
220-
defer os.Remove(out.Name())
221-
} else {
222-
out, err = os.Create(outFile)
223-
if err != nil {
224-
return err
225-
}
226-
}
227-
228210
l := lexers.Get(lexer)
229211
if l == nil {
230212
l = lexers.Analyse(source)
@@ -249,68 +231,25 @@ func toImg(useClipboard bool, source, outFile string, lexer, style string) error
249231
return err
250232
}
251233

252-
if !useClipboard {
253-
return f.Format(out, s, it)
254-
}
234+
buf := new(bytes.Buffer)
255235

256-
if err := f.Format(out, s, it); err != nil {
236+
if err := f.Format(buf, s, it); err != nil {
257237
return err
258238
}
259239

260-
out.Close()
261-
262-
return toClipboard(out.Name())
263-
}
264-
265-
func toClipboard(file string) error {
266-
switch runtime.GOOS {
267-
case "darwin":
268-
cmd := exec.Command("osascript", "-e", fmt.Sprintf("set the clipboard to (read \"%s\" as TIFF picture)", file))
269-
b, err := cmd.CombinedOutput()
270-
if err != nil {
271-
return fmt.Errorf("%s: %s", err, string(b))
272-
}
273-
return nil
274-
case "linux":
275-
f, err := os.Open(file)
276-
if err != nil {
277-
return err
278-
}
279-
defer f.Close()
280-
281-
b, err := exec.Command("file", "-b", "--mime-type", file).CombinedOutput()
282-
if err != nil {
283-
return fmt.Errorf("%s: %s", err, string(b))
284-
}
285-
286-
// b has new line
287-
cmd := exec.Command("xclip", "-selection", "clipboard", "-t", string(b[:len(b)-1]))
288-
in, err := cmd.StdinPipe()
289-
if err != nil {
290-
return err
291-
}
292-
293-
if err := cmd.Start(); err != nil {
294-
return err
295-
}
296-
297-
if _, err := io.Copy(in, f); err != nil {
298-
return err
299-
}
240+
if useClipboard {
241+
return clipboard.CopyToClipboard(buf)
242+
}
300243

301-
if err := in.Close(); err != nil {
302-
return err
303-
}
244+
tmp, err := ioutil.TempFile("", "")
245+
if err != nil {
246+
return err
247+
}
248+
defer tmp.Close()
304249

305-
return cmd.Wait()
306-
case "windows":
307-
cmd := exec.Command("PowerShell", "-Command", "Add-Type", "-AssemblyName", fmt.Sprintf("System.Windows.Forms;[Windows.Forms.Clipboard]::SetImage([System.Drawing.Image]::FromFile('%s'));", file))
308-
b, err := cmd.CombinedOutput()
309-
if err != nil {
310-
return fmt.Errorf("%s: %s", err, string(b))
311-
}
312-
return nil
250+
if _, err := io.Copy(tmp, buf); err != nil {
251+
return err
313252
}
314253

315-
return fmt.Errorf("unsupported os: %s", runtime.GOOS)
254+
return os.Rename(tmp.Name(), outFile)
316255
}

0 commit comments

Comments
 (0)