forked from savedra1/clipse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (113 loc) · 3.4 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
package main
import (
"flag"
"fmt"
"os"
"strconv"
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"
"github.com/atotto/clipboard"
)
var (
version = "v1.0.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.")
copy = 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.")
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's history.")
)
func main() {
flag.Parse()
historyFilePath, clipseDir, displayServer, imgEnabled, err := config.Init()
utils.HandleError(err)
switch {
case flag.NFlag() == 0:
handleNoFlags(historyFilePath)
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(historyFilePath)
case *copy:
handleCopy()
case *paste:
handlePaste()
case *listen:
handleListen()
case *listenShell:
handleListenShell(historyFilePath, clipseDir, displayServer, imgEnabled)
case *kill:
handleKill()
case *clear:
handleClear(historyFilePath)
default:
fmt.Printf("Command not recognized. See %s --help for usage instructions.", os.Args[0])
}
}
func handleNoFlags(historyFilePath string) {
_ = shell.KillExistingFG() // err ignored to mitigate panic when no existinmg clipse ps
if len(os.Args) > 1 {
_, err := strconv.Atoi(os.Args[1]) // check for valid PPID by attempting conversion to an int
// above line causes canic so cannot catch this error effictively
if err != nil {
fmt.Printf("Invalid PPID supplied: %s\nPPID must be integer. use var `$PPID`", os.Args[1])
return
}
}
_, err := tea.NewProgram(app.NewModel()).Run()
utils.HandleError(err)
}
func handleAdd(historyFilePath string) {
var input string
if len(os.Args) < 3 {
input = utils.GetStdin()
} else {
input = os.Args[2]
}
err := config.AddClipboardItem(historyFilePath, input, "null")
utils.HandleError(err)
}
func handleListen() {
shell.KillExisting()
shell.RunNohupListener() // hardcoded as const
}
func handleListenShell(historyFilePath, clipseDir, displayServer string, imgEnabled bool) {
err := handlers.RunListener(historyFilePath, clipseDir, displayServer, imgEnabled)
utils.HandleError(err)
}
func handleKill() {
shell.KillAll(os.Args[0])
}
func handleClear(historyFilePath string) {
clipboard.WriteAll("")
err := config.ClearHistory(historyFilePath)
utils.HandleError(err)
}
func handleCopy() {
var input string
if len(os.Args) < 3 {
input = utils.GetStdin()
} else {
input = os.Args[2]
}
err := clipboard.WriteAll(input)
utils.HandleError(err)
}
func handlePaste() {
currentItem, err := clipboard.ReadAll()
utils.HandleError(err)
if currentItem != "" {
fmt.Println(currentItem)
}
}