-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyclip.py
261 lines (199 loc) · 5.54 KB
/
easyclip.py
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import pyperclip
import time
import threading
import os
from tkinter import *
from tkinter import messagebox
# Clipboard Array
copy_array = []
# position of copy_array
counter = 0
# copy_array GUI location for increment and decrement
array_current_position = 0
thread_kill = True
# root for Tkinter
root = Tk(className="EasyClip")
# Text box to print copy_array value
T = Text(root, height=7, width=32, borderwidth=0)
# Text box to print position of clip
N = Text(root, height=1, width=2, borderwidth=0)
# Value of copy_array at a certain point
Fact = ""
def gui():
"""
function for GUI.
Handles grid and positioning.
"""
global array_current_position
global T
global N
global root
global Fact
root.wait_visibility(root)
# root.wm_attributes('-alpha', 0.9)
# root.configure(bg='#293134')
img = PhotoImage(file='~/.local/share/icons/easyclip/easyclip.png')
root.tk.call('wm', 'iconphoto', root._w, img)
# T.config(font=("Courier", 10))
T.grid(row=0, columnspan=30, sticky='nesw')
# T.configure(bg='#293134', fg="#e0e2e4", bd=-2)
N.grid(row=1, columnspan=2)
# N.configure(bg='#293134', fg="#e0e2e4")
l = Label(root, text="w=Next s=Previous c=Copy q=minimize")
# Create label
l.config(font=("Courier", 10))
l.grid(row=2, columnspan=2)
Fact = copy_array[array_current_position]
# Create button for next text.
b1 = Button(root, text="Previous", command=increment, borderwidth=0)
b1.grid(row=3, column=0, sticky='nesw')
# Create an Exit button.
# b2 = Button(root, text="Exit", command=on_closing)
b2 = Button(root, text="Exit", command=exit1, borderwidth=0)
b2.grid(row=4, column=1, sticky='nesw')
b3 = Button(root, text="Next", command=decrement, borderwidth=0)
b3.grid(row=3, column=1, sticky='nesw')
b4 = Button(root, text="Copy", command=copy_from_gui_value, borderwidth=0)
b4.grid(row=4, column=0, sticky='nesw')
# Insert The Fact.
T.insert(END, Fact)
N.insert(END, array_current_position+1)
T.config(state=DISABLED)
mainloop()
def exit1():
"""
exit function.
"""
global thread_kill
global root
root.destroy()
thread_kill = False
def update_on_new_copy():
"""
Update GUI on each new copy
"""
global array_current_position
global Fact
array_current_position = len(copy_array) - 1
T.config(state=NORMAL)
T.delete(1.0, END)
Fact = copy_array[array_current_position]
N.delete(1.0, END)
N.insert(1.0, len(copy_array) - array_current_position)
T.insert(END, Fact)
T.config(state=DISABLED)
def decrement():
"""
Update GUI on button click
"""
global array_current_position
global Fact
if array_current_position == len(copy_array) - 1:
return
T.config(state=NORMAL)
T.delete(1.0, END)
array_current_position = array_current_position + 1
Fact = copy_array[array_current_position]
T.insert(END, Fact)
N.delete(1.0, END)
N.insert(1.0, len(copy_array) - array_current_position)
T.config(state=DISABLED)
def increment():
"""
Update GUI on button click
"""
global array_current_position
global Fact
if array_current_position <= 0:
return
T.config(state=NORMAL)
T.delete(1.0, END)
array_current_position = array_current_position - 1
Fact = copy_array[array_current_position]
T.insert(END, Fact)
N.delete(1.0, END)
N.insert(1.0, len(copy_array) - array_current_position)
T.config(state=DISABLED)
def update_clipboard():
"""
Function to keep watching for clipboard update.
Sleep time 0.5 second by default.
Updates only when current and last clip do not match.
"""
global counter
global thread_kill
copy_array.append(pyperclip.paste())
while thread_kill:
# print("loop") # Debug
# print(counter) # Debug
time.sleep(0.3)
if copy_array[counter] != pyperclip.paste():
counter += 1
copy_array.append(pyperclip.paste())
update_on_new_copy()
# print("changed") # Debug
# print("changed") # Debug
# print("changed") # Debug
# print(*copy_array, sep=", ") # Debug
# print("############################################") # Debug
def copy_from_gui_value():
"""
copy value from the GUI value
"""
# print(Fact) # Debug
pyperclip.copy(Fact)
def key_increment(event):
"""
keypress event W
"""
increment()
root.bind('<w>', key_increment)
def key_decrement(event):
"""
Keypress event S
"""
decrement()
root.bind('<s>', key_decrement)
def func_gui(event):
"""
Keypress event Q
Minimize the window
"""
root.wm_state('iconic')
root.bind('<q>', func_gui)
def copy_from_gui(event):
"""
keypress event C
"""
copy_from_gui_value()
root.wm_state('iconic')
root.bind('<c>', copy_from_gui)
def on_closing():
"""
Ask before closing the window
"""
if messagebox.askokcancel("Quit", "Do you want to quit?"):
exit1()
# root.protocol("WM_DELETE_WINDOW", on_closing)
root.protocol("WM_DELETE_WINDOW", exit1)
def main():
"""
Call everything here
"""
# Thread for making the clipboard update process in the background
download_thread = threading.Thread(target=update_clipboard)
download_thread.start()
gui()
"""
Process and Error Handling
"""
# Detach from terminal
if os.fork():
sys.exit()
"""
class DevNull:
def write(self, msg):
pass
sys.stderr = DevNull()
"""
main()