From cceb032576481d4c7569d49804dc493a2c22a592 Mon Sep 17 00:00:00 2001 From: Arvedui Date: Sun, 22 Feb 2015 14:10:36 +0100 Subject: [PATCH 1/6] rename text_len to max_field_len, add max_len parameter and some logic to truncate certain fields if output len exceeds max_len --- i3pystatus/mpd.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/i3pystatus/mpd.py b/i3pystatus/mpd.py index 8946f19..f5db1e0 100644 --- a/i3pystatus/mpd.py +++ b/i3pystatus/mpd.py @@ -1,5 +1,6 @@ import socket from os.path import basename +from math import floor from i3pystatus import IntervalModule, formatp from i3pystatus.core.util import TimeWrapper @@ -34,8 +35,10 @@ class MPD(IntervalModule): ("format", "formatp string"), ("status", "Dictionary mapping pause, play and stop to output"), ("color", "The color of the text"), - ("text_len", "Defines max length for title, album and artist, if truncated ellipsis are appended as indicator"), - ("truncate_fields", "fileds that will be truncated if exceeding text_len"), + ("max_field_len", "Defines max length for in truncate_fields defined fields, if truncated, ellipsis are appended as indicator. Value of 0 disables this."), + ("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" @@ -48,7 +51,8 @@ class MPD(IntervalModule): "stop": "◾", } color = "#FFFFFF" - text_len = 25 + max_field_len = 25 + max_len = 100 truncate_fields = ("title", "album", "artist") on_leftclick = "switch_playpause" 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: fdict["filename"] = '.'.join( basename(currentsong["file"]).split('.')[:-1]) else: 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 = { "full_text": formatp(self.format, **fdict).strip(), "color": self.color, From 87b57ce838ac0d2efea4dc46ed872b3ec023391e Mon Sep 17 00:00:00 2001 From: Arvedui Date: Sun, 22 Feb 2015 14:14:58 +0100 Subject: [PATCH 2/6] some clarification in docs about the truncation order --- i3pystatus/mpd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/i3pystatus/mpd.py b/i3pystatus/mpd.py index f5db1e0..baa524b 100644 --- a/i3pystatus/mpd.py +++ b/i3pystatus/mpd.py @@ -35,8 +35,8 @@ class MPD(IntervalModule): ("format", "formatp string"), ("status", "Dictionary mapping pause, play and stop to output"), ("color", "The color of the text"), - ("max_field_len", "Defines max length for in truncate_fields defined fields, if truncated, ellipsis are appended as indicator. Value of 0 disables this."), - ("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."), + ("max_field_len", "Defines max length for in truncate_fields defined fields, if truncated, ellipsis are appended as indicator. It's applied *before* max_len. Value of 0 disables this."), + ("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. It's applied *after* max_field_len. 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"), ) From 2bcef67c836b2a8a4f78fe000ffb61a1b3582a89 Mon Sep 17 00:00:00 2001 From: Arvedui Date: Sun, 22 Feb 2015 14:17:03 +0100 Subject: [PATCH 3/6] some changes to prevent recomputation of the output string if no truncation is done --- i3pystatus/mpd.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/i3pystatus/mpd.py b/i3pystatus/mpd.py index baa524b..51d369d 100644 --- a/i3pystatus/mpd.py +++ b/i3pystatus/mpd.py @@ -105,7 +105,8 @@ class MPD(IntervalModule): 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()) + full_text = formatp(self.format, **fdict).strip() + full_text_len = len(full_text) if full_text_len > self.max_len: shrink = floor((self.max_len - full_text_len) / len(self.truncate_fields)) - 1 @@ -113,8 +114,10 @@ class MPD(IntervalModule): for key in self.truncate_fields: fdict[key] = fdict[key][:shrink] + "…" + full_text = formatp(self.format, **fdict).strip() + self.output = { - "full_text": formatp(self.format, **fdict).strip(), + "full_text": full_text, "color": self.color, } From d0692798b706c5d1d644ea2f59ca981f974bfd37 Mon Sep 17 00:00:00 2001 From: Arvedui Date: Sun, 22 Feb 2015 14:29:48 +0100 Subject: [PATCH 4/6] fix a documentation issue --- i3pystatus/mpd.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/i3pystatus/mpd.py b/i3pystatus/mpd.py index 51d369d..d9b3b33 100644 --- a/i3pystatus/mpd.py +++ b/i3pystatus/mpd.py @@ -37,7 +37,7 @@ class MPD(IntervalModule): ("color", "The color of the text"), ("max_field_len", "Defines max length for in truncate_fields defined fields, if truncated, ellipsis are appended as indicator. It's applied *before* max_len. Value of 0 disables this."), ("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. It's applied *after* max_field_len. 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"), + ("truncate_fields", "fields that will be truncated if exceeding max_field_len or max_len."), ) From 68813026e632b478e812731eabd95b9101800390 Mon Sep 17 00:00:00 2001 From: Arvedui Date: Sun, 22 Feb 2015 14:54:01 +0100 Subject: [PATCH 5/6] add functionalaty for deactivate truncation in mpd module --- i3pystatus/mpd.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/i3pystatus/mpd.py b/i3pystatus/mpd.py index d9b3b33..496ca5c 100644 --- a/i3pystatus/mpd.py +++ b/i3pystatus/mpd.py @@ -101,13 +101,14 @@ class MPD(IntervalModule): else: 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] + "…" + if self.max_field_len > 0: + 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 = formatp(self.format, **fdict).strip() full_text_len = len(full_text) - if full_text_len > self.max_len: + if full_text_len > self.max_len and self.max_len > 0: shrink = floor((self.max_len - full_text_len) / len(self.truncate_fields)) - 1 From 787e8d262370b8daf721b3a41c56673d1842d0ea Mon Sep 17 00:00:00 2001 From: Arvedui Date: Sun, 22 Feb 2015 15:03:32 +0100 Subject: [PATCH 6/6] fixed build failure --- i3pystatus/mpd.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/i3pystatus/mpd.py b/i3pystatus/mpd.py index 496ca5c..95d5bab 100644 --- a/i3pystatus/mpd.py +++ b/i3pystatus/mpd.py @@ -109,8 +109,8 @@ class MPD(IntervalModule): full_text = formatp(self.format, **fdict).strip() full_text_len = len(full_text) if full_text_len > self.max_len and self.max_len > 0: - shrink = floor((self.max_len - full_text_len) - / len(self.truncate_fields)) - 1 + shrink = floor((self.max_len - full_text_len) / + len(self.truncate_fields)) - 1 for key in self.truncate_fields: fdict[key] = fdict[key][:shrink] + "…"