-
Notifications
You must be signed in to change notification settings - Fork 0
/
clip_cmd.go
54 lines (46 loc) · 1.26 KB
/
clip_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package syscmd
import (
"fmt"
"github.com/gookit/gcli/v3"
"github.com/gookit/goutil"
"github.com/gookit/goutil/sysutil/clipboard"
"github.com/inhere/kite-go/internal/apputil"
)
// clipCmdOpts struct
type clipCmdOpts struct {
Verb bool `flag:"verbose;display real exec command line;;;v"`
Read bool `flag:"read contents from clipboard, default operate;;;r"`
Write string `flag:"write contents to clipboard, allow: @in;false;;w"`
Output string `flag:"read contents and write to the output;;stdout;o"`
}
// NewClipboardCmd command
func NewClipboardCmd() *gcli.Command {
var clipOpts = &clipCmdOpts{}
return &gcli.Command{
Name: "clipboard",
Aliases: []string{"clip"},
Desc: "write or read the clipboard contents",
Config: func(c *gcli.Command) {
goutil.MustOK(c.UseSimpleRule().FromStruct(clipOpts))
},
Func: func(c *gcli.Command, _ []string) error {
cb := clipboard.New().WithVerbose(clipOpts.Verb)
if clipOpts.Write != "" {
str, err := apputil.ReadSource(clipOpts.Write)
if err != nil {
return err
}
if _, err = cb.WriteString(str); err != nil {
return err
}
return cb.Flush()
}
// read contents
str, err := cb.ReadString()
if err == nil {
fmt.Println(str)
}
return err
},
}
}