Skip to content

Commit 4335b15

Browse files
committed
all: add cmd/gclip
1 parent 52a3cc3 commit 4335b15

File tree

3 files changed

+162
-5
lines changed

3 files changed

+162
-5
lines changed

Dockerfile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
# Written by Changkun Ou <changkun.de>
66

77
FROM golang:1.16rc1
8-
RUN apt-get update \
9-
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
8+
RUN apt-get update && apt-get install -y \
109
xvfb libx11-dev \
1110
&& apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
1211
WORKDIR /app

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import "golang.design/x/clipboard"
88

99
## Dependency
1010

11-
- Linux users: `apt install -y libx11-dev`
12-
- macOS users: no dependency
11+
- Linux users: require X: `apt install -y libx11-dev` or `xorg-dev`
12+
- macOS users: require Cgo, no dependency
13+
- Windows users: unsupported yet
1314

14-
## Usage
15+
## API Usage
1516

1617
```go
1718
// write texts to the clipboard
@@ -27,6 +28,35 @@ clipboard.Write(clipboard.MIMEImage, []byte("image data"))
2728
clipboard.Read(clipboard.MIMEImage)
2829
```
2930

31+
## Command Usage
32+
33+
```sh
34+
go install golang.design/x/clipboard/cmd/gclip@latest
35+
```
36+
37+
```
38+
gclip is a command that provides clipboard interaction.
39+
40+
usage: gclip [-copy|-paste] [-f <file>]
41+
42+
options:
43+
-copy
44+
copy data to clipboard
45+
-f string
46+
source or destination to a given file path
47+
-paste
48+
paste data from clipboard
49+
50+
examples:
51+
gclip -paste paste from clipboard and prints the content
52+
gclip -paste -f x.txt paste from clipboard and save as text to x.txt
53+
gclip -paste -f x.png paste from clipboard and save as image to x.png
54+
55+
cat x.txt | gclip -copy copy content from x.txt to clipboard
56+
gclip -copy -f x.txt copy content from x.txt to clipboard
57+
gclip -copy -f x.png copy x.png as image data to clipboard
58+
```
59+
3060
## Notes
3161

3262
To put image data to system clipboard, you could:

cmd/gclip/main.go

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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

Comments
 (0)