bumblebee-status/modules/contrib/title.py

78 lines
2.3 KiB
Python
Raw Normal View History

2020-04-25 10:32:13 +02:00
# pylint: disable=C0111,R0903
"""Displays focused i3 window title.
Requirements:
* i3ipc
Parameters:
* title.max : Maximum character length for title before truncating. Defaults to 64.
2020-04-25 10:32:27 +02:00
* title.placeholder : Placeholder text to be placed if title was truncated. Defaults to '...'.
2020-04-25 10:32:13 +02:00
* title.scroll : Boolean flag for scrolling title. Defaults to False
"""
import threading
try:
import i3ipc
except ImportError:
pass
2020-04-25 10:40:11 +02:00
no_title = 'n/a'
2020-04-25 10:32:13 +02:00
2020-04-25 10:40:11 +02:00
import core.module
import core.widget
import core.decorators
2020-04-25 10:32:13 +02:00
2020-04-25 10:40:11 +02:00
import util.format
2020-04-25 10:32:13 +02:00
2020-04-25 10:40:11 +02:00
class Module(core.module.Module):
def __init__(self, config, theme):
super().__init__(config, theme, [])
2020-04-25 10:32:13 +02:00
# parsing of parameters
2020-04-25 10:40:11 +02:00
self.__scroll = util.format.asbool(self.parameter('scroll', False))
self.__max = int(self.parameter('max', 64))
self.__placeholder = self.parameter('placeholder', '...')
self.__title = ''
2020-04-25 10:32:13 +02:00
# set output of the module
2020-04-25 10:40:11 +02:00
self.widgets([core.widget.Widget(full_text=
self.__scrolling_focused_title if self.__scroll else self.__focused_title)])
2020-04-25 10:32:13 +02:00
# create a connection with i3ipc
2020-04-25 10:40:11 +02:00
self.__i3 = i3ipc.Connection()
# event is called both on focus change and title change
self.__i3.on('window', lambda __p_i3, __p_e: self.__pollTitle())
# begin listening for events
threading.Thread(target=self.__i3.main).start()
2020-04-25 10:32:13 +02:00
# initialize the first title
2020-04-25 10:40:11 +02:00
self.__pollTitle()
2020-04-25 10:32:13 +02:00
2020-04-25 10:40:11 +02:00
def __focused_title(self, widget):
return self.__title
2020-04-25 10:32:13 +02:00
2020-04-25 10:40:11 +02:00
@core.decorators.scrollable
def __scrolling_focused_title(self, widget):
return self.__full_title
2020-04-25 10:32:13 +02:00
2020-04-25 10:40:11 +02:00
def __pollTitle(self):
2020-04-25 10:32:13 +02:00
"""Updating current title."""
try:
2020-04-25 10:40:11 +02:00
self.__full_title = self.__i3.get_tree().find_focused().name
2020-04-25 10:32:13 +02:00
except:
2020-04-25 10:40:11 +02:00
self.__full_title = no_title
if self.__full_title is None:
self.__full_title = no_title
2020-04-25 10:32:13 +02:00
2020-04-25 10:40:11 +02:00
if not self.__scroll:
2020-04-25 10:32:13 +02:00
# cut the text if it is too long
2020-04-25 10:40:11 +02:00
if len(self.__full_title) > self.__max:
self.__title = self.__full_title[0:self.__max - len(self.__placeholder)]
self.__title = '{}{}'.format(self.__title, self.__placeholder)
2020-04-25 10:32:13 +02:00
else:
2020-04-25 10:40:11 +02:00
self.__title = self.__full_title
2020-04-25 10:32:13 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4