Files
dotfiles/.i3/lemonbar/i3_lemonbar_common.py

86 lines
2.2 KiB
Python
Executable File

import subprocess, os
import re # regexp
from enum import Enum
import i3_lemonbar_config as config
kill_on_unfocus = []
# Loggers, initialized in main function
logger = None
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
class bar_mode(Enum):
power, normal, control = range(-1,2) # Don't cycle through power
def cycle(self):
try:
return bar_mode(self.value + 1)
except ValueError:
return bar_mode(0)
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-?]*[ -/]*[@-~]')
def strip_ansi_unicode(s):
# ANSI escape sequences are for colors in terminal and similar
strip_ansi = ansi_escape.sub('', s)
strip_unicode = (strip_ansi.encode('ascii', 'ignore')).decode('utf-8')
return strip_unicode
def create_new_fifo(fifo_file):
"""
Create new fifo file, removing old one if it exists
"""
try:
os.remove(fifo_file)
logger.debug('''Removed old fifo file''')
except OSError:
logger.debug('''No old fifo file, good''')
try:
os.mkfifo(fifo_file)
except OSError:
logger.error('''Failed, couldn't create fifo file''')
os.remove(config.pid_file) # Clean up own PID file
sys.exit(0)