-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathmain.go
206 lines (168 loc) · 5.11 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"flag"
"fmt"
"os"
"github.com/atotto/clipboard"
tea "github.com/charmbracelet/bubbletea"
"github.com/savedra1/clipse/app"
"github.com/savedra1/clipse/config"
"github.com/savedra1/clipse/handlers"
"github.com/savedra1/clipse/shell"
"github.com/savedra1/clipse/utils"
)
var (
version = "v1.1.0"
help = flag.Bool("help", false, "Show help message.")
v = flag.Bool("v", false, "Show app version.")
add = flag.Bool("a", false, "Add the following arg to the clipboard history.")
copyInput = flag.Bool("c", false, "Copy the input to your systems clipboard.")
paste = flag.Bool("p", false, "Prints the current clipboard content.")
listen = flag.Bool("listen", false, "Start background process for monitoring clipboard activity on wayland/x11/macOS.")
listenShell = flag.Bool("listen-shell", false, "Starts a clipboard monitor process in the current shell.")
kill = flag.Bool("kill", false, "Kill any existing background processes.")
clear = flag.Bool("clear", false, "Remove all contents from the clipboard history except for pinned items.")
clearAll = flag.Bool("clear-all", false, "Remove all contents the clipboard history including pinned items.")
clearImages = flag.Bool("clear-images", false, "Removes all images from the clipboard history including pinned images.")
clearText = flag.Bool("clear-text", false, "Removes all text from the clipboard history including pinned text entries.")
forceClose = flag.Bool("fc", false, "Forces the terminal session to quick by taking the $PPID var as an arg. EG `clipse -fc $PPID`")
wlStore = flag.Bool("wl-store", false, "Store data from the stdin directly using the wl-clipboard API.")
outputAll = flag.String("output-all", "", "Print clipboard text content to stdout, each entry separated by a newline, possible values: (raw, unescaped)")
)
func main() {
flag.Parse()
logPath, displayServer, imgEnabled, err := config.Init()
utils.HandleError(err)
utils.SetUpLogger(logPath)
switch {
case flag.NFlag() == 0:
if len(os.Args) > 2 {
fmt.Println("Too many args provided. See usage:")
flag.PrintDefaults()
return
}
launchTUI()
case flag.NFlag() > 1:
fmt.Printf("Too many flags provided. Use %s --help for more info.", os.Args[0])
case *help:
flag.PrintDefaults()
case *v:
fmt.Println(os.Args[0], version)
case *add:
handleAdd()
case *paste:
handlePaste()
case *copyInput:
handleCopy()
case *listen:
handleListen(displayServer)
case *listenShell:
handleListenShell(displayServer, imgEnabled)
case *kill:
handleKill()
case *clear, *clearAll, *clearImages, *clearText:
handleClear()
case *forceClose:
handleForceClose()
case *wlStore:
handlers.StoreWLData()
case *outputAll != "":
handleOutputAll(*outputAll)
default:
fmt.Printf("Command not recognized. See %s --help for usage instructions.", os.Args[0])
}
}
func launchTUI() {
shell.KillExistingFG()
_, err := tea.NewProgram(app.NewModel()).Run()
utils.HandleError(err)
}
func handleAdd() {
var input string
switch {
case len(os.Args) < 3:
input = utils.GetStdin()
default:
input = os.Args[2]
}
utils.HandleError(config.AddClipboardItem(input, "null"))
}
func handleListen(displayServer string) {
if err := shell.KillExisting(); err != nil {
fmt.Printf("ERROR: failed to kill existing listener process: %s", err)
utils.LogERROR(fmt.Sprintf("failed to kill existing listener process: %s", err))
}
shell.RunNohupListener(displayServer)
}
func handleListenShell(displayServer string, imgEnabled bool) {
utils.HandleError(handlers.RunListener(displayServer, imgEnabled))
}
func handleKill() {
shell.KillAll(os.Args[0])
}
func handleClear() {
if err := clipboard.WriteAll(""); err != nil {
utils.LogERROR(fmt.Sprintf("failed to reset clipboard buffer value: %s", err))
}
var clearType string
switch {
case *clearImages:
clearType = "images"
case *clearAll:
clearType = "all"
case *clearText:
clearType = "text"
default:
clearType = "default"
}
utils.HandleError(config.ClearHistory(clearType))
}
func handleCopy() {
var input string
switch {
case len(os.Args) < 3:
input = utils.GetStdin()
default:
input = os.Args[2]
}
if input != "" {
fmt.Println(input)
utils.HandleError(clipboard.WriteAll(input))
}
}
func handlePaste() {
currentItem, err := clipboard.ReadAll()
utils.HandleError(err)
if currentItem != "" {
fmt.Println(currentItem)
}
}
func handleForceClose() {
if len(os.Args) < 3 {
fmt.Printf("No PPID provided. Usage: %s' -fc $PPID'", os.Args[0])
return
}
if len(os.Args) > 3 {
fmt.Printf("Too many args. Usage: %s' -fc $PPID'", os.Args[0])
return
}
if !utils.IsInt(os.Args[2]) {
fmt.Printf("Invalid PPID supplied: %s\nPPID must be integer. use var `$PPID` as the arg.", os.Args[2])
return
}
launchTUI()
}
func handleOutputAll(format string) {
items := config.TextItems()
if format == "raw" {
for _, v := range items {
fmt.Printf("%q\n", v.Value)
}
} else if format == "unescaped" {
for _, v := range items {
fmt.Println(v.Value)
}
} else {
fmt.Printf("Invalid argument to -output-all\nSee %s --help for usage", os.Args[0])
}
}