@@ -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
210209func 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