forked from mirekys/i3-lemonbar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBar.py
More file actions
120 lines (106 loc) · 4.69 KB
/
Bar.py
File metadata and controls
120 lines (106 loc) · 4.69 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
from os import pipe, fork, close, dup2, killpg, environ, execve, wait, setpgid, getpid
from sys import exit, stderr
from re import compile as comp
from signal import SIGTERM
class Bar(object):
def __init__(self):
self.id = ''
self.transparency = True
#self.bar_command = '/bin/bar'
self.bar_command = '/bin/lemonbar'
self.bar_args = ['-a', '20', '-g']
#self.config = dict()
self.colors = dict()
self.sep_right = ' | '
self.sep_left = ' | '
self.geometry = 'x14'
def start_bar(self):
brside, bwside = pipe()
bar_pid = fork()
if not bar_pid:
srside, swside = pipe()
close(bwside)
dup2(brside, 0)
dup2(swside, 1)
setpgid(getpid(), 0)
sh_pid = fork()
if not sh_pid:
close(swside)
dup2(srside, 0)
execve('/bin/sh', ['/bin/sh'], environ)
print("test", file=stderr)
print("failed to exec sh program!", file=stderr)
exit(1)
close(srside)
execve(self.bar_command, [self.bar_command] + self.bar_args + ['|', 'sh'], environ)
print("failed to exec bar program!", file=stderr)
exit(1)
close(brside)
dup2(bwside, 1)
return bar_pid #TODO: do not kill sh if possible
def stop_bar(self, wbak, bar_pid):
dup2(wbak, 1)
if bar_pid:
killpg(bar_pid, SIGTERM)
wait()
else: print("could'nt stop bar")
def parse_color(self, color):
hex_color = "[A-Fa-f0-9]"
norm_color = comp("^#"+hex_color+"{6}$")
trans_color_1 = comp("^\[("+hex_color+"{2})\]#("+hex_color+"{6})$")
trans_color_2 = comp("^#"+hex_color+"{2}("+hex_color+"{6})$")
if (match:=trans_color_1.match(color)):
return '#'+match.group(1)+match.group(2) if self.transparency else '#'+match.group(2)
elif (match:=trans_color_2.match(color)):
return color if self.transparency else '#'+match.group(1)
elif norm_color.match(color):
return color
return '#f0f0f0'
def set_config(self, i3):
#status_command, stiped numbers and name, mode, tray padding, modifier, verbose, workspace buttons and hidden state are ignored. TODO: implement them.
config = i3.get_bar_config(self.id)
self.bar_args += [self.geometry]
if config.font:
for font in config.font.split(','):
self.bar_args += ['-f'] + [font]
if config.colors['background']:
self.colors['gen_bg'] = config.colors['background'][-6:]
self.bar_args += ['-B'] + [self.parse_color(config.colors['background'])]
if config.colors['statusline']:
self.bar_args += ['-F'] + [self.parse_color(config.colors['statusline'])]
if not config.position == 'top':
self.bar_args += ['-b']
if config.separator_symbol:
separators = config.separator_symbol.split(',')
self.sep_left = separators[0]
if len(separators) == 2:
self.sep_right = separators[1]
else:
self.sep_right = separators[0]
for key, value in config.colors.items():
if key in ['statusline', 'background']:
continue
# elif key == 'focused_workspace_border':
# self.colors['foc_sep'] = self.parse_color(value)
elif key == 'focused_workspace_bg':
self.colors['foc_bg'] = self.parse_color(value)
elif key == 'focused_workspace_text':
self.colors['foc_fg'] = self.parse_color(value)
# elif key == 'inactive_workspace_border':
# self.colors['in_sep'] = self.parse_color(value)
elif key == 'inactive_workspace_bg':
self.colors['in_bg'] = self.parse_color(value)
elif key == 'inactive_workspace_text':
self.colors['in_fg'] = self.parse_color(value)
# elif key == 'urgent_workspace_border':
# self.colors['ur_sep'] = self.parse_color(value)
elif key == 'urgent_workspace_bg':
self.colors['ur_bg'] = self.parse_color(value)
elif key == 'urgent_workspace_text':
self.colors['ur_fg'] = self.parse_color(value)
# elif key == 'binding_mode_border':
# self.colors['bd_sep'] = self.parse_color(value)
elif key == 'binding_mode_bg':
self.colors['bd_bg'] = self.parse_color(value)
elif key == 'binding_mode_text':
self.colors['bd_fg'] = self.parse_color(value)