bumblebee-status/bumblebee_status/modules/contrib/title.py

91 lines
2.7 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
2022-03-07 04:20:30 +01:00
* title.short : Boolean flag for short title. Defaults to False
contributed by `UltimatePancake <https://github.com/UltimatePancake>`_ - many thanks!
2020-04-25 10:32:13 +02:00
"""
import threading
try:
import i3ipc
except ImportError:
pass
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.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
self.__scroll = util.format.asbool(self.parameter("scroll", False))
2022-03-07 04:20:30 +01:00
self.__short = util.format.asbool(self.parameter("short", 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
self.add_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())
2020-04-25 10:40:11 +02:00
# 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:
2022-03-07 04:20:30 +01:00
focused = self.__i3.get_tree().find_focused().name
self.__full_title = focused.split(
"-")[-1].strip() if self.__short else focused
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
2020-04-25 10:32:13 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4