Skip to content

Commit

Permalink
Add -o parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
MMulthaupt committed Oct 10, 2019
1 parent 28072da commit 389c247
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
5 changes: 4 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# 0.1.0 (2019-10-09)
# v0.2.0 (2019-10-10)
- Add `-o` parameter to output clipboard instead.

# v0.1.0 (2019-10-09)
- Initial release.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,27 @@ Copy stdin contents to clipboard:
echo foo | cb
```

Copy stdin contents to clipboard, truncating 1 trailing line break if it exists:
Omit exactly one trailing line break if it exists:
```sh
echo foo | cb -n
# or
echo -n foo | cb
```

Manually write the clipboard:
```sh
cb
Hello World!^D # ^D = Ctrl+D (send EOF)
```

Write clipboard to stdout:
```sh
cb -o
```

Write clipboard to file:
```sh
cb -o file.txt
```

## Installation
Expand Down
31 changes: 25 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@ import (
)

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

var cb string
var err error
var filePath string
if flag.NArg() == 0 {
data, ioErr := ioutil.ReadAll(os.Stdin)
cb, err = string(data), ioErr
if *outputClipboard {
cb, err = clipboard.ReadAll()
} else {
data, ioErr := ioutil.ReadAll(os.Stdin)
cb, err = string(data), ioErr
}
} else if flag.NArg() == 1 {
filePath := flag.Arg(0)
cb, err = readTextFile(filePath)
filePath = flag.Arg(0)
if *outputClipboard {
cb, err = clipboard.ReadAll()
} else {
cb, err = readTextFile(filePath)
}
} else {
fatalf("provide file path or input on stdin")
}
Expand All @@ -32,7 +42,16 @@ func main() {
cb = cb[:len(cb)-1]
}

err = clipboard.WriteAll(cb)
if *outputClipboard {
if filePath == "" {
_, err = fmt.Printf(cb)
} else {
err = ioutil.WriteFile(filePath, []byte(cb), 0755)
}
} else {
err = clipboard.WriteAll(cb)
}

if err != nil {
fatalf("%v", err)
}
Expand Down

0 comments on commit 389c247

Please sign in to comment.