lemonbar: Move last actions to modules and remove command dict

This commit is contained in:
2019-11-14 19:08:45 +01:00
committed by kuben
parent b8bf0c0f61
commit 1367be34be
3 changed files with 40 additions and 42 deletions

View File

@@ -13,6 +13,7 @@ health_logger = None
screen_send_cmd = lambda s : None # TODO temporary until move actions to modules
write_queue = None # TODO Actually, why is this a queue? Just last element is important
# TODO bar mode can be moved to i3Module
class bar_mode(Enum):
power, normal, control = range(-1,2) # Don't cycle through power
@@ -24,40 +25,12 @@ class bar_mode(Enum):
mode = bar_mode.normal
def i3msg_comm(args):
subprocess.Popen(args.split())
def set_mode(args):
global mode
new_mode = args.split()[1]
if new_mode == "cycle":
mode = mode.cycle()
for m in bar_mode:
if new_mode == m.name:
mode = m
break
def bluetooth(args):
btcargs = args.split()[1:]
btcargs = [a.replace('pxc550', '00:16:94:22:29:0E') for a in btcargs]
inp = ' '.join(btcargs)
screen_send_cmd('bluetoothctl ' + inp)
show_secs = False
# Keymaps
def_keymap = 'pl'
keymaps = {'firefox': 'se'}
cur_class = ''
def set_lang(args):
global keymaps
lang = args.split()[1]
keymaps[cur_class] = lang
commands_dict = {'i3-msg': i3msg_comm
,'mode': set_mode
,'setlang': set_lang
,'bluetooth': bluetooth
}
# Helper functions
ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]')

View File

@@ -152,15 +152,10 @@ def exec_commands():
common.logger.debug('Trying reading: "{0}"'.format(data.strip('\n')))
try:
for key,func in common.commands_dict.items():
l = len(key)
if data[:l] == key:
func(data)
break
modules.do_action(data.split()[0])
modules.do_action(data)
except:
common.logger.debug('Exception occured executing command\n Line in: {}\n Data: {}'.format(data, data[l:].split()))
common.logger.debug('Exception occured executing command\n Line in: {}'.format(data))
if len(data) == 0:
common.logger.debug("Lemonbar output closed")
break

View File

@@ -48,10 +48,16 @@ class LemonModule(threading.Thread):
def register_action(self, keyword, do_action):
self.actions[keyword] = do_action
def do_action(self, keyword):
if (keyword in self.actions):
func = self.actions[keyword]
func()
def do_action(self, command):
action_arr = command.split()
action = action_arr[0]
if (action in self.actions):
func = self.actions[action]
if len(action_arr) == 1:
# Function has no arguments
func()
else:
func(action_arr[1:])
def format_load(data, module, alert):
# Helper function to format network modules
@@ -229,13 +235,13 @@ class ConkySlowModule(LemonModule):
p = subprocess.Popen(['xterm', '-class', 'FLOAT_TERM', '-e', 'htop'])
common.kill_on_unfocus.append(p.pid)
def lang_comm(args):
def lang_comm(self):
subprocess.Popen(['sh', '/home/kuba/.i3/scripts/lang.sh', 'next'])
def dpms_comm(args):
def dpms_comm(self):
subprocess.Popen(['sh', '/home/kuba/.i3/scripts/dpmsctl.sh'])
def adj_br(args):
def adj_br(self):
subprocess.Popen(['/home/kuba/.i3/scripts/adjbr.sh', '-b', config.fifo_file_executor])
class i3Module(LemonModule):
@@ -261,6 +267,9 @@ class i3Module(LemonModule):
parser.g_parser.register_unit(self.workspaces)
parser.g_parser.register_unit(self.title)
self.register_action('i3-msg' , self.i3msg_comm)
self.register_action('mode' , self.set_mode)
# Add callbacks for parsing
self.i3_ws_obj.change_callbacks.append(self.parse_displays)
self.i3_ws_obj.change_callbacks.append(self.parse_workspaces)
@@ -406,6 +415,20 @@ class i3Module(LemonModule):
common.logger.debug('Setting default keymap {} for {}'.format(new_km, wclass))
subprocess.call(['/home/kuba/.i3/scripts/lang.sh', 'qset', new_km])
def i3msg_comm(self, args):
# TODO this is not needed anymore. Use i3ws object directly
subprocess.Popen(['i3-msg'] + args)
def set_mode(self, args):
new_mode = args[0]
if new_mode == "cycle":
common.mode = common.mode.cycle()
for m in common.bar_mode:
if new_mode == m.name:
common.mode = m
break
class ScreenModule(LemonModule):
# Start, stop and send commands to screen instance
@@ -445,6 +468,8 @@ class ScreenModule(LemonModule):
parser.g_parser.register_unit(self.response)
parser.g_parser.register_unit(self.controls)
self.register_action('bluetooth', self.bluetooth)
def _stop_module(self):
self.send_colon('kill')
self.status_handle.close()
@@ -483,6 +508,11 @@ class ScreenModule(LemonModule):
# Send colon command to our screen instance
self.send(['-X', 'colon', cmd + '\n'])
def bluetooth(self, args):
btcargs = [a.replace('pxc550', '00:16:94:22:29:0E') for a in args]
inp = ' '.join(btcargs)
common.screen_send_cmd('bluetoothctl ' + inp)
class PowerOptionsModule(LemonModule):
def __init__(self):