Skip to content

Commit 4b27c01

Browse files
refactor: Replace wrapLines() with Python's textwrap.wrap() (bit-team#1702)
Close bit-team#1699 Thanks to Tom Hunter aka @cornpaffies for contribution.
1 parent bd8a768 commit 4b27c01

File tree

2 files changed

+5
-47
lines changed

2 files changed

+5
-47
lines changed

common/logger.py

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ def closelog():
5757

5858

5959
def _do_syslog(message: str, level: int) -> str:
60-
for line in wrapLine(message):
61-
syslog.syslog(level, '{}{}: {}'.format(
62-
SYSLOG_MESSAGE_PREFIX, _level_names[level], line))
60+
syslog.syslog(level, '{}{}: {}'.format(
61+
SYSLOG_MESSAGE_PREFIX, _level_names[level], message))
6362

6463

6564
def error(msg, parent=None, traceDepth=0):
@@ -142,43 +141,3 @@ def _debugHeader(parent, traceDepth):
142141

143142
func = frame.f_code.co_name
144143
return '[%s/%s:%s %s%s]' % (fmodule, fname, line, fclass, func)
145-
146-
147-
# This function was moved from tools.py to here to solve a circular
148-
# import dependency between "tools" and "logger".
149-
def wrapLine(msg, size=950, delimiters='\t ', new_line_indicator = 'CONTINUE: '):
150-
"""
151-
Wrap line ``msg`` into multiple lines with each shorter than ``size``. Try
152-
to break the line on ``delimiters``. New lines will start with
153-
``new_line_indicator``.
154-
155-
Args:
156-
msg (str): string that should get wrapped
157-
size (int): maximum length of returned strings
158-
delimiters (str): try to break ``msg`` on these characters
159-
new_line_indicator (str): start new lines with this string
160-
161-
Yields:
162-
str: lines with max ``size`` length
163-
"""
164-
165-
# TODO Use "textwrap.wrap" instead (https://docs.python.org/3/library/textwrap.html)
166-
# (which may change the output formatting and may affect unit tests then)
167-
# To avoid duplicated argument values in calls this function could
168-
# act as a wrapper-
169-
170-
if len(new_line_indicator) >= size - 1:
171-
new_line_indicator = ''
172-
while msg:
173-
if len(msg) <= size:
174-
yield(msg)
175-
break
176-
else:
177-
line = ''
178-
for look in range(size-1, size//2, -1):
179-
if msg[look] in delimiters:
180-
line, msg = msg[:look+1], new_line_indicator + msg[look+1:]
181-
break
182-
if not line:
183-
line, msg = msg[:size], new_line_indicator + msg[size:]
184-
yield(line)

qt/qtsystrayicon.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import os
2121
import subprocess
2222
import signal
23+
import textwrap
2324

2425
# TODO Is this really required? If the client is not configured for X11
2526
# it may use Wayland or something else...
@@ -177,10 +178,8 @@ def updateInfo(self):
177178
self.last_message = message
178179
if self.decode:
179180
message = (message[0], self.decode.log(message[1]))
180-
self.menuStatusMessage.setText('\n'.join(logger.wrapLine(message[1], \
181-
size = 80, \
182-
delimiters = '', \
183-
new_line_indicator = '') \
181+
self.menuStatusMessage.setText('\n'.join(textwrap.wrap(message[1], \
182+
width = 80) \
184183
))
185184
self.status_icon.setToolTip(message[1])
186185

0 commit comments

Comments
 (0)