diff --git a/Changelog b/Changelog index b27e5c87..6ff85e9d 100644 --- a/Changelog +++ b/Changelog @@ -1,10 +1,11 @@ -2023-04-05 s-n-g +2023-04-07 s-n-g * version 0.9.2.5 * Desktop Notifications will display the image provided by the station (jpg or png). * adding config option "Use station icon" * adding a fourth optional column for pyradio playlists. This column will define a station icon url. + * adding CJK chars support for Group Headers * fixing a potential crash when randomly playing stations. * trying to limit duplicate Desktop Notifications. * adding a system theme: blue_by_boxer diff --git a/README.html b/README.html index 6781c68f..ae792d80 100644 --- a/README.html +++ b/README.html @@ -202,13 +202,14 @@
-2023-04-05 s-n-g +2023-04-07 s-n-g * version 0.9.2.5 * Desktop Notifications will display the image provided by the station (jpg or png). * adding config option "Use station icon" * adding a fourth optional column for pyradio playlists. This column will define a station icon url. + * adding CJK chars support for Group Headers * fixing a potential crash when randomly playing stations. * trying to limit duplicate Desktop Notifications. * adding a system theme: blue_by_boxer diff --git a/pyradio/cjkwrap.py b/pyradio/cjkwrap.py index 9a36ab95..a8e47eb4 100644 --- a/pyradio/cjkwrap.py +++ b/pyradio/cjkwrap.py @@ -74,6 +74,40 @@ def cjkslices(text, index): i = i + 1 return text[:i-1], text[i-1:] +def cjkljust(text, width, char= ' '): + txt_len = cjklen(text) + out = text + if width == txt_len: + return text + elif width > txt_len: + return out + (width - txt_len) * char + elif width < txt_len: + return cjkslices(text, width)[0] + +def cjkrjust(text, width, char= ' '): + txt_len = cjklen(text) + out = text + if width == txt_len: + return text + elif width > txt_len: + return (width - txt_len) * char + out + elif width < txt_len: + return cjkslices(text, width)[0] + +def cjkcenter(text, width, char= ' '): + txt_len = cjklen(text) + out = text + if width == txt_len: + return text + elif width > txt_len: + pad = int(( width - txt_len ) / 2) + out = pad * char + text + pad * char + while cjklen(out) < width: + out = char + out + return out + elif width < txt_len: + return cjkslices(text, width)[0] + class CJKWrapper(textwrap.TextWrapper): """CJK fix for the Greg Ward textwrap lib.""" @@ -161,20 +195,6 @@ def fill(text, width=70, **kwargs): w = CJKWrapper(width=width, **kwargs) return w.fill(text) -def cjkcenter(text, width, char= ' '): - txt_len = cjklen(text) - out = text - if width == txt_len: - return text - elif width > txt_len: - pad = int(( width - txt_len ) / 2) - out = pad * char + text + pad * char - while cjklen(out) < width: - out = char + out - return out - elif width < txt_len: - return cjkslices(text, width)[0] - if __name__ == '__main__': a='这显然不是巧合。美国敌视中国之情绪正在加深、加剧' print(cjklen(a) * '-') diff --git a/pyradio/simple_curses_widgets.py b/pyradio/simple_curses_widgets.py index a8795a50..e5993f91 100644 --- a/pyradio/simple_curses_widgets.py +++ b/pyradio/simple_curses_widgets.py @@ -10,7 +10,7 @@ # except: # from cjkwrap import PY3, is_wide, cjklen # from schedule import PyRadioTime -from .cjkwrap import PY3, is_wide, cjklen +from .cjkwrap import PY3, is_wide, cjklen, cjkljust from .schedule import PyRadioTime import locale locale.setlocale(locale.LC_ALL, '') # set your locale @@ -1847,7 +1847,7 @@ def _get_window(self): self._maxY = Y - 2 * self._outer_margin # logger.error('max = {}'.format(max(len(x) for x in self._items))) - self._maxX = items_max_X = max(len(x) for x in self._items) + 2 + self._maxX = items_max_X = max(cjklen(x) for x in self._items) + 2 if self._margin > 0: self._maxX = self._maxX + 2 * self._margin if self._display_count: @@ -1917,7 +1917,7 @@ def _calculate_max_height_max_width(self): if self._maxX == 0: self._maxX = X - 2 if self._auto_adjust_width: - self._maxX = len(max(self._items)) + 2 * self._margin + self._maxX = cjklen(max(self._items)) + 2 * self._margin if self._maxX > self._max_width: self._maxX = self._max_width @@ -2008,7 +2008,7 @@ def _refresh(self): # calculate start item # TODO: calculate start_pos - self._item_max_width = len(max(self._items, key=len)) + self._item_max_width = cjklen(max(self._items, key=cjklen)) active_item_length = self._maxX - 2 * self._margin - 2 if self._start_pos < 0: self._start_pos = 0 @@ -2052,18 +2052,19 @@ def _format_line(self, i, active_item_length): count_len = len(str(len(self._items))) # log_it('count_len = {}'.format(count_len)) disp_item_pref = '{}. '.format(str(item_id+1).rjust(count_len)) - disp_item_suf = self._items[item_id][:active_item_length-len(disp_item_pref)] + # disp_item_suf = self._items[item_id][:active_item_length-cjklen(disp_item_pref)] + disp_item_suf = cjkljust(self._items[item_id], self._body_maxX - len(disp_item_pref) - 2 * self._margin) disp_item = ' ' * self._margin + disp_item_pref + disp_item_suf + ' ' * self._margin else: #print('item_id = {}'.format(item_id)) item = self._items[item_id][:active_item_length] if self._align == self.LEFT: - disp_item = ' ' * self._margin + item.ljust(active_item_length) + ' ' * self._margin + disp_item = ' ' * self._margin + item.cjkljust(active_item_length) + ' ' * self._margin elif self._align == self.RIGHT: - disp_item = ' ' * self._margin + item.rjust(active_item_length) + ' ' * self._margin + disp_item = ' ' * self._margin + item.cjkrjust(active_item_length) + ' ' * self._margin else: - disp_item = ' ' * self._margin + item.center(active_item_length) + ' ' * self._margin - disp_item = disp_item.ljust(self._body_maxX) + disp_item = ' ' * self._margin + item.cjkcenter(active_item_length) + ' ' * self._margin + # disp_item = disp_item.ljust(self._body_maxX) else: # create empty lines disp_item = ' ' * self._body_maxX