-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi3wm_help.py
More file actions
122 lines (99 loc) · 3.65 KB
/
i3wm_help.py
File metadata and controls
122 lines (99 loc) · 3.65 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
#!/usr/bin/python3
#
#Parse the i3 config file, collect and parse shortcuts and display on screen
#
#Author Joachim van de Haterd <joachim@derjoachim.nl>
#
import string
import os
import gi
import collections
gi.require_version('Gtk','3.0')
from gi.repository import Gtk
class CellRendererTextWindow(Gtk.Window):
def __init__(self,dict_config, path):
Gtk.Window.__init__(self, title='Help ' + path)
self.set_border_width(10)
self.grid = Gtk.Grid()
self.grid.set_column_homogeneous(True)
self.grid.set_row_homogeneous(True)
self.add(self.grid)
hbox = Gtk.Box(spacing=6)
self.add(hbox)
self.set_default_size(1120,720)
self.liststore = Gtk.ListStore(str,str)
for sc, dscrptn in dict_config.items():
self.liststore.append([sc, dscrptn])
treeview = Gtk.TreeView(model=self.liststore)
sct = Gtk.CellRendererText()
column_text = Gtk.TreeViewColumn('Shortcut', sct, text=0)
treeview.append_column(column_text)
dtext = Gtk.CellRendererText()
desc_column_text = Gtk.TreeViewColumn('Description', dtext, text=1)
treeview.append_column(desc_column_text)
self.add(treeview)
button = Gtk.Button('Close')
button.connect('clicked', self.on_close_clicked)
self.scrollable_treelist = Gtk.ScrolledWindow()
self.scrollable_treelist.set_vexpand(True)
self.grid.attach(self.scrollable_treelist, 0, 0, 12, 16)
self.grid.attach_next_to(button, self.scrollable_treelist,
Gtk.PositionType.BOTTOM, 1, 1)
self.scrollable_treelist.add(treeview)
self.show_all()
def on_close_clicked(self, button):
Gtk.main_quit()
""" possible config file paths as per the i3 man page """
def getConfigPath():
paths = ['~/.config/i3/config',
'/etc/xdg/i3/config',
'~/.i3/config',
'/etc/i3/config']
configpath = False
for my_path in paths:
tmp_path = os.path.expanduser(my_path)
if os.path.isfile(tmp_path):
configpath = tmp_path
break
return configpath
"""
Open the path as found in getConfigPath and parse the shortcuts into a
dictionary
attributes config_path the full path to the config file
"""
def parseConfigFile(config_path):
myshortcuts = {}
myvars = {}
with open(config_path, encoding='utf-8') as my_file:
for my_line in my_file:
words = my_line.split(' ')
if words[0] == 'set' and words[1].startswith('$'):
myvars[words[1]] = ' '.join(words[2:]).rstrip()
elif words[0] == 'bindsym':
pos = 1
if words[1].startswith('--'):
pos = 2
tmpkeys = words[pos].split('+')
for pos, subkey in enumerate(tmpkeys):
if subkey.startswith('$'):
tmpkeys[pos] = myvars[subkey]
key = '+'.join(tmpkeys)
myshortcuts[key] = ' '.join(words[2:]).rstrip()
return myshortcuts
"""
Sort the config dictionary by shortcut
TODO: Mod+foo and Mod+Shift+Foo should be grouped
"""
def sortbyshortcut(dcfg):
return collections.OrderedDict(sorted(dcfg.items()))
current_config_path = getConfigPath()
if current_config_path == False:
print('No i3wm configuration file was found')
""" TODO Better error handling"""
else:
myconfig = parseConfigFile(current_config_path)
myconfig = sortbyshortcut(myconfig)
win = CellRendererTextWindow(myconfig, current_config_path)
win.connect('delete-event', Gtk.main_quit)
win.show_all()
Gtk.main()