-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.py
More file actions
executable file
·127 lines (107 loc) · 4.2 KB
/
display.py
File metadata and controls
executable file
·127 lines (107 loc) · 4.2 KB
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
#!/usr/bin/python3
import gi
import glob
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk
import subprocess
import os
CONFIG_DIR = '{0}/.copy-cat-tail/config'.format(os.environ.get('HOME'))
CONFIG = {}
def get_config():
config = open('{0}/config'.format(CONFIG_DIR), 'r')
configList = [row.split('=') for row in config.readlines()]
configDict = {row[0]: row[1][1:-1] for row in configList}
return configDict
def get_file_data():
numFiles = len(glob.glob('{0}/copies/clip*.txt'.format(CONFIG['WORKING_DIR'])))
currentIdxFile = open('{0}/currentIdx.txt'.format(CONFIG['WORKING_DIR']), 'r')
currentIdx = int(currentIdxFile.read())
return numFiles,currentIdx
class HeaderBarWindow(Gtk.Window):
def __init__(self, numFiles, currentIdx):
super().__init__(title="HeaderBar Demo")
# setting window properties
self.set_default_size(500, 400)
self.set_border_width(0)
self.set_position(Gtk.WindowPosition.CENTER)
self.set_opacity(0.8)
# setting the files and contents
self.numFiles = numFiles
self.currentIdx = currentIdx
# composing the header
hb = Gtk.HeaderBar()
hb.set_show_close_button(True)
hb.props.title = "Copy Cat\'s Tail"
self.set_titlebar(hb)
# adding child components to header
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
Gtk.StyleContext.add_class(box.get_style_context(), "linked")
button = Gtk.Button()
button.add(
Gtk.Arrow(arrow_type=Gtk.ArrowType.LEFT, shadow_type=Gtk.ShadowType.NONE)
)
button.connect('clicked', self.move_next)
box.add(button)
self.numLabel = Gtk.Label()
box.add(self.numLabel)
button = Gtk.Button()
button.add(
Gtk.Arrow(arrow_type=Gtk.ArrowType.RIGHT, shadow_type=Gtk.ShadowType.NONE)
)
button.connect('clicked', self.move_prev)
box.add(button)
hb.pack_start(box)
# setting scroll window
scrolled_window = Gtk.ScrolledWindow()
scrolled_window.set_border_width(2)
scrolled_window.set_policy(
Gtk.PolicyType.ALWAYS, Gtk.PolicyType.ALWAYS)
# adding label to scroll window
self.label = Gtk.Label()
self.label.set_line_wrap(True)
self.label.set_max_width_chars(150)
scrolled_window.add(self.label)
self.connect("key-press-event", self.change_clip)
# adding scroll window to window
self.add(scrolled_window)
# setting values
self.set_display_text()
def get_current_text(self):
currText = self.label.get_text()
return currText.replace('"','\\"').replace('$','\\$')
def exit_window(self, widget, event):
currentText = self.get_current_text()
subprocess.run("echo -n \"{0}\" | xclip -i -selection clipboard".format(currentText), shell=True)
self.destroy()
Gtk.main_quit()
def change_clip(self, widget, event):
ctrl = (event.state & Gdk.ModifierType.CONTROL_MASK)
if ctrl and event.keyval == Gdk.KEY_Right:
self.move_prev(widget)
if ctrl and event.keyval == Gdk.KEY_Left:
self.move_next(widget)
if ctrl and event.keyval == Gdk.KEY_Down:
self.exit_window(widget, event)
def move_next(self, widget):
if self.currentIdx < self.numFiles - 1:
self.currentIdx += 1
else:
self.currentIdx = 0
self.set_display_text()
def move_prev(self, widget):
if self.currentIdx > 0:
self.currentIdx -= 1
else:
self.currentIdx = self.numFiles - 1
self.set_display_text()
def set_display_text(self):
global CONFIG
currentFile = open('{0}/copies/clip{1}.txt'.format(CONFIG['WORKING_DIR'], self.currentIdx))
self.label.set_text(currentFile.read())
self.numLabel.set_text('{0} / {1}'.format(self.numFiles - self.currentIdx, self.numFiles))
CONFIG = get_config()
numFiles, currentIdx = get_file_data()
win = HeaderBarWindow(numFiles, currentIdx)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()