Skip to content

Commit 28fe37b

Browse files
committed
Handling CJK presentation on station and playlist view
1 parent 5315836 commit 28fe37b

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

pyradio/radio.py

+32-12
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from .log import Log
2727
from .edit import PyRadioSearch, PyRadioEditor
2828
from .themes import *
29+
from .simple_curses_widgets import cjklen
2930
from . import player
3031
import logging
3132

@@ -528,9 +529,10 @@ def __displayBodyLine(self, lineNum, pad, station):
528529
self.ws.operation_mode == self.ws.PLAYLIST_LOAD_ERROR_MODE or \
529530
self.ws.operation_mode == self.ws.PLAYLIST_NOT_FOUND_ERROR_MODE:
530531
line = self._format_playlist_line(lineNum, pad, station)
532+
self.bodyWin.addstr(lineNum + 1, 1, line, col)
531533
else:
532-
line = "{0}. {1}".format(str(lineNum + self.startPos + 1).rjust(pad), station[0])
533-
self.bodyWin.addstr(lineNum + 1, 1, line[:body_width], col)
534+
line = self._format_station_line("{0}. {1}".format(str(lineNum + self.startPos + 1).rjust(pad), station[0]))
535+
self.bodyWin.addstr(lineNum + 1, 1, line, col)
534536

535537
def run(self):
536538
if self.ws.operation_mode == self.ws.NO_PLAYER_ERROR_MODE:
@@ -1018,36 +1020,54 @@ def _format_playlist_line(self, lineNum, pad, station):
10181020
line = "{0}. {1}".format(str(lineNum + self.startPos + 1).rjust(pad), station[0])
10191021
f_data = ' [{0}, {1}]'.format(station[2], station[1])
10201022
if version_info < (3, 0):
1021-
if len(line.decode('utf-8', 'replace')) + len(f_data.decode('utf-8', 'replace')) > self.bodyMaxX -2:
1023+
if cjklen(line.decode('utf-8', 'replace')) + cjklen(f_data.decode('utf-8', 'replace')) > self.bodyMaxX -2:
10221024
""" this is too long, try to shorten it
10231025
by removing file size """
10241026
f_data = ' [{0}]'.format(station[1])
1025-
if len(line.decode('utf-8', 'replace')) + len(f_data.decode('utf-8', 'replace')) > self.bodyMaxX - 2:
1027+
if cjklen(line.decode('utf-8', 'replace')) + cjklen(f_data.decode('utf-8', 'replace')) > self.bodyMaxX - 2:
10261028
""" still too long. start removing chars """
1027-
while len(line.decode('utf-8', 'replace')) + len(f_data.decode('utf-8', 'replace')) > self.bodyMaxX - 3:
1029+
while cjklen(line.decode('utf-8', 'replace')) + cjklen(f_data.decode('utf-8', 'replace')) > self.bodyMaxX - 3:
10281030
f_data = f_data[:-1]
10291031
f_data += ']'
10301032
""" if too short, pad f_data to the right """
1031-
if len(line.decode('utf-8', 'replace')) + len(f_data.decode('utf-8', 'replace')) < self.maxX - 2:
1032-
while len(line.decode('utf-8', 'replace')) + len(f_data.decode('utf-8', 'replace')) < self.maxX - 2:
1033+
if cjklen(line.decode('utf-8', 'replace')) + cjklen(f_data.decode('utf-8', 'replace')) < self.maxX - 2:
1034+
while cjklen(line.decode('utf-8', 'replace')) + cjklen(f_data.decode('utf-8', 'replace')) < self.maxX - 2:
10331035
line += ' '
10341036
else:
1035-
if len(line) + len(f_data) > self.bodyMaxX -2:
1037+
if cjklen(line) + cjklen(f_data) > self.bodyMaxX -2:
10361038
""" this is too long, try to shorten it
10371039
by removing file size """
10381040
f_data = ' [{0}]'.format(station[1])
1039-
if len(line) + len(f_data) > self.bodyMaxX - 2:
1041+
if cjklen(line) + cjklen(f_data) > self.bodyMaxX - 2:
10401042
""" still too long. start removing chars """
1041-
while len(line) + len(f_data) > self.bodyMaxX - 3:
1043+
while cjklen(line) + cjklen(f_data) > self.bodyMaxX - 3:
10421044
f_data = f_data[:-1]
10431045
f_data += ']'
10441046
""" if too short, pad f_data to the right """
1045-
if len(line) + len(f_data) < self.maxX - 2:
1046-
while len(line) + len(f_data) < self.maxX - 2:
1047+
if cjklen(line) + cjklen(f_data) < self.maxX - 2:
1048+
while cjklen(line) + cjklen(f_data) < self.maxX - 2:
10471049
line += ' '
10481050
line += f_data
10491051
return line
10501052

1053+
def _format_station_line(self, line):
1054+
if version_info < (3, 0):
1055+
if len(line.decode('utf-8', 'replace')) != cjklen(line.decode('utf-8', 'replace')):
1056+
while cjklen(line.decode('utf-8', 'replace')) > self.bodyMaxX - 2:
1057+
line = line[:-1]
1058+
return line
1059+
else:
1060+
return line[:self.bodyMaxX - 2]
1061+
1062+
pass
1063+
else:
1064+
if len(line) != cjklen(line):
1065+
while cjklen(line) > self.bodyMaxX - 2:
1066+
line = line[:-1]
1067+
return line
1068+
else:
1069+
return line[:self.bodyMaxX - 2]
1070+
10511071
def _print_help(self):
10521072
#logger.error('DE \n\nself.ws.operation_mode = {}'.format(self.ws.operation_mode))
10531073
if self.ws.operation_mode in self._display_help.keys():

0 commit comments

Comments
 (0)