|
| 1 | +// Copyright 2021 The golang.design Initiative authors. |
| 2 | +// All rights reserved. Use of this source code is governed |
| 3 | +// by a GNU GPL-3 license that can be found in the LICENSE file. |
| 4 | +// |
| 5 | +// Written by Changkun Ou <changkun.de> |
| 6 | + |
| 7 | +package main // go install golang.design/x/clipboard/cmd/gclip@latest |
| 8 | + |
| 9 | +import ( |
| 10 | + "errors" |
| 11 | + "flag" |
| 12 | + "fmt" |
| 13 | + "io" |
| 14 | + "os" |
| 15 | + "path/filepath" |
| 16 | + |
| 17 | + "golang.design/x/clipboard" |
| 18 | +) |
| 19 | + |
| 20 | +func usage() { |
| 21 | + fmt.Fprintf(os.Stderr, `gclip is a command that provides clipboard interaction. |
| 22 | +
|
| 23 | +usage: gclip [-copy|-paste] [-f <file>] |
| 24 | +
|
| 25 | +options: |
| 26 | +`) |
| 27 | + flag.PrintDefaults() |
| 28 | + fmt.Fprintf(os.Stderr, ` |
| 29 | +examples: |
| 30 | +gclip -paste paste from clipboard and prints the content |
| 31 | +gclip -paste -f x.txt paste from clipboard and save as text to x.txt |
| 32 | +gclip -paste -f x.png paste from clipboard and save as image to x.png |
| 33 | +
|
| 34 | +cat x.txt | gclip -copy copy content from x.txt to clipboard |
| 35 | +gclip -copy -f x.txt copy content from x.txt to clipboard |
| 36 | +gclip -copy -f x.png copy x.png as image data to clipboard |
| 37 | +`) |
| 38 | + os.Exit(2) |
| 39 | +} |
| 40 | + |
| 41 | +var ( |
| 42 | + in = flag.Bool("copy", false, "copy data to clipboard") |
| 43 | + out = flag.Bool("paste", false, "paste data from clipboard") |
| 44 | + file = flag.String("f", "", "source or destination to a given file path") |
| 45 | +) |
| 46 | + |
| 47 | +var ( |
| 48 | + errUnsupported = errors.New("unsupported MIME format") |
| 49 | +) |
| 50 | + |
| 51 | +func main() { |
| 52 | + flag.Usage = usage |
| 53 | + flag.Parse() |
| 54 | + if *out { |
| 55 | + if err := pst(); err != nil { |
| 56 | + usage() |
| 57 | + } |
| 58 | + return |
| 59 | + } |
| 60 | + if *in { |
| 61 | + if err := cpy(); err != nil { |
| 62 | + usage() |
| 63 | + } |
| 64 | + return |
| 65 | + } |
| 66 | + usage() |
| 67 | +} |
| 68 | + |
| 69 | +func cpy() error { |
| 70 | + t := clipboard.MIMEText |
| 71 | + ext := filepath.Ext(*file) |
| 72 | + |
| 73 | + switch ext { |
| 74 | + case ".png": |
| 75 | + t = clipboard.MIMEImage |
| 76 | + case ".txt": |
| 77 | + fallthrough |
| 78 | + default: |
| 79 | + t = clipboard.MIMEText |
| 80 | + } |
| 81 | + |
| 82 | + var ( |
| 83 | + b []byte |
| 84 | + err error |
| 85 | + ) |
| 86 | + if *file != "" { |
| 87 | + b, err = os.ReadFile(*file) |
| 88 | + if err != nil { |
| 89 | + fmt.Fprintf(os.Stderr, "failed to read given file: %v", err) |
| 90 | + return err |
| 91 | + } |
| 92 | + } else { |
| 93 | + b, err = io.ReadAll(os.Stdin) |
| 94 | + if err != nil { |
| 95 | + fmt.Fprintf(os.Stderr, "failed to read from stdin: %v", err) |
| 96 | + return err |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + clipboard.Write(t, b) |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func pst() (err error) { |
| 105 | + var b []byte |
| 106 | + |
| 107 | + b = clipboard.Read(clipboard.MIMEText) |
| 108 | + if b == nil { |
| 109 | + b = clipboard.Read(clipboard.MIMEImage) |
| 110 | + } |
| 111 | + |
| 112 | + if *file != "" && b != nil { |
| 113 | + err = os.WriteFile(*file, b, os.ModePerm) |
| 114 | + if err != nil { |
| 115 | + fmt.Fprintf(os.Stderr, "failed to write data to file %s: %v", *file, err) |
| 116 | + } |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + for len(b) > 0 { |
| 121 | + n, err := os.Stdout.Write(b) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + b = b[n:] |
| 126 | + } |
| 127 | + return nil |
| 128 | +} |
0 commit comments