Skip to content

Commit 389c247

Browse files
committed
Add -o parameter
1 parent 28072da commit 389c247

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

CHANGES.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
# 0.1.0 (2019-10-09)
1+
# v0.2.0 (2019-10-10)
2+
- Add `-o` parameter to output clipboard instead.
3+
4+
# v0.1.0 (2019-10-09)
25
- Initial release.

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,27 @@ Copy stdin contents to clipboard:
1010
echo foo | cb
1111
```
1212

13-
Copy stdin contents to clipboard, truncating 1 trailing line break if it exists:
13+
Omit exactly one trailing line break if it exists:
1414
```sh
1515
echo foo | cb -n
16+
# or
17+
echo -n foo | cb
18+
```
19+
20+
Manually write the clipboard:
21+
```sh
22+
cb
23+
Hello World!^D # ^D = Ctrl+D (send EOF)
24+
```
25+
26+
Write clipboard to stdout:
27+
```sh
28+
cb -o
29+
```
30+
31+
Write clipboard to file:
32+
```sh
33+
cb -o file.txt
1634
```
1735

1836
## Installation

main.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,27 @@ import (
1010
)
1111

1212
func main() {
13-
flagTruncNewLine := flag.Bool("n", false, "Set to truncate one trailing line break if it exists.")
13+
flagTruncNewLine := flag.Bool("n", false, "Set to omit exactly one trailing line break from the clipboard should it exist.")
14+
outputClipboard := flag.Bool("o", false, "Set to output the clipboard instead of setting it.")
1415
flag.Parse()
1516

1617
var cb string
1718
var err error
19+
var filePath string
1820
if flag.NArg() == 0 {
19-
data, ioErr := ioutil.ReadAll(os.Stdin)
20-
cb, err = string(data), ioErr
21+
if *outputClipboard {
22+
cb, err = clipboard.ReadAll()
23+
} else {
24+
data, ioErr := ioutil.ReadAll(os.Stdin)
25+
cb, err = string(data), ioErr
26+
}
2127
} else if flag.NArg() == 1 {
22-
filePath := flag.Arg(0)
23-
cb, err = readTextFile(filePath)
28+
filePath = flag.Arg(0)
29+
if *outputClipboard {
30+
cb, err = clipboard.ReadAll()
31+
} else {
32+
cb, err = readTextFile(filePath)
33+
}
2434
} else {
2535
fatalf("provide file path or input on stdin")
2636
}
@@ -32,7 +42,16 @@ func main() {
3242
cb = cb[:len(cb)-1]
3343
}
3444

35-
err = clipboard.WriteAll(cb)
45+
if *outputClipboard {
46+
if filePath == "" {
47+
_, err = fmt.Printf(cb)
48+
} else {
49+
err = ioutil.WriteFile(filePath, []byte(cb), 0755)
50+
}
51+
} else {
52+
err = clipboard.WriteAll(cb)
53+
}
54+
3655
if err != nil {
3756
fatalf("%v", err)
3857
}

0 commit comments

Comments
 (0)