rename text_len to max_field_len, add max_len parameter and some logic to truncate certain fields if output len exceeds max_len

This commit is contained in:
Arvedui 2015-02-22 14:10:36 +01:00
parent 0e7e04f075
commit cceb032576

View File

@ -1,5 +1,6 @@
import socket import socket
from os.path import basename from os.path import basename
from math import floor
from i3pystatus import IntervalModule, formatp from i3pystatus import IntervalModule, formatp
from i3pystatus.core.util import TimeWrapper from i3pystatus.core.util import TimeWrapper
@ -34,8 +35,10 @@ class MPD(IntervalModule):
("format", "formatp string"), ("format", "formatp string"),
("status", "Dictionary mapping pause, play and stop to output"), ("status", "Dictionary mapping pause, play and stop to output"),
("color", "The color of the text"), ("color", "The color of the text"),
("text_len", "Defines max length for title, album and artist, if truncated ellipsis are appended as indicator"), ("max_field_len", "Defines max length for in truncate_fields defined fields, if truncated, ellipsis are appended as indicator. Value of 0 disables this."),
("truncate_fields", "fileds that will be truncated if exceeding text_len"), ("max_len", "Defines max length for the hole string, if exceeding fields specefied in truncate_fields are truncated equaly. If truncated, ellipsis are appended as indicator. Value of 0 disables this."),
("truncate_fields", "fields that will be truncated if exceeding max_field_len or max_len, whatever catches first takes effect"),
) )
host = "localhost" host = "localhost"
@ -48,7 +51,8 @@ class MPD(IntervalModule):
"stop": "", "stop": "",
} }
color = "#FFFFFF" color = "#FFFFFF"
text_len = 25 max_field_len = 25
max_len = 100
truncate_fields = ("title", "album", "artist") truncate_fields = ("title", "album", "artist")
on_leftclick = "switch_playpause" on_leftclick = "switch_playpause"
on_rightclick = "next_song" on_rightclick = "next_song"
@ -91,15 +95,24 @@ class MPD(IntervalModule):
} }
for key in self.truncate_fields:
if len(fdict[key]) > self.text_len:
fdict[key] = fdict[key][:self.text_len - 1] + ""
if not fdict["title"] and "filename" in fdict: if not fdict["title"] and "filename" in fdict:
fdict["filename"] = '.'.join( fdict["filename"] = '.'.join(
basename(currentsong["file"]).split('.')[:-1]) basename(currentsong["file"]).split('.')[:-1])
else: else:
fdict["filename"] = "" fdict["filename"] = ""
for key in self.truncate_fields:
if len(fdict[key]) > self.max_field_len:
fdict[key] = fdict[key][:self.max_field_len - 1] + ""
full_text_len = len(formatp(self.format, **fdict).strip())
if full_text_len > self.max_len:
shrink = floor((self.max_len - full_text_len)
/ len(self.truncate_fields)) - 1
for key in self.truncate_fields:
fdict[key] = fdict[key][:shrink] + ""
self.output = { self.output = {
"full_text": formatp(self.format, **fdict).strip(), "full_text": formatp(self.format, **fdict).strip(),
"color": self.color, "color": self.color,