Skip to content

Commit eea541f

Browse files
committed
Refactor wrap and indent functionality
1 parent fb98909 commit eea541f

File tree

3 files changed

+104
-31
lines changed

3 files changed

+104
-31
lines changed

src/manage/commands.py

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -569,27 +569,13 @@ def show_usage(cls):
569569
if usage_ljust % 4:
570570
usage_ljust += 4 - (usage_ljust % 4)
571571
usage_ljust = max(usage_ljust, 16) + 1
572-
sp = " " * usage_ljust
573572

574573
LOGGER.print("!G!Usage:!W!")
575574
for k, d in usage_docs:
576-
if k.endswith("\n") and len(logging.strip_colour(k)) >= usage_ljust:
577-
LOGGER.print(k.rstrip())
578-
r = sp
579-
else:
580-
k = k.rstrip()
581-
r = k.ljust(usage_ljust + len(k) - len(logging.strip_colour(k)))
582-
for b in d.split(" "):
583-
if len(r) >= logging.CONSOLE_MAX_WIDTH:
584-
LOGGER.print(r.rstrip())
585-
r = sp
586-
r += b + " "
587-
if r.rstrip():
588-
LOGGER.print(r)
589-
590-
LOGGER.print()
591-
LOGGER.print("Find additional information at !B!%s!W!.", HELP_URL)
592-
LOGGER.print()
575+
for s in logging.wrap_and_indent(d, indent=usage_ljust, hang=k.rstrip()):
576+
LOGGER.print(s)
577+
578+
LOGGER.print("\nFind additional information at !B!%s!W!.\n", HELP_URL)
593579

594580
@classmethod
595581
def help_text(cls):
@@ -907,7 +893,7 @@ def execute(self):
907893
self.show_welcome(copyright=False)
908894
if not self.args:
909895
self.show_usage()
910-
LOGGER.print(BaseCommand.help_text(commands_only))
896+
LOGGER.print(BaseCommand.help_text())
911897
for a in self.args:
912898
try:
913899
cls = COMMANDS[a.lower()]
@@ -993,7 +979,7 @@ def execute(self):
993979
first_run(self)
994980
if not self.explicit:
995981
self.show_usage()
996-
if self.confirm and not self.ask_ny(f"View online help?"):
982+
if self.confirm and not self.ask_ny("View online help?"):
997983
import os
998984
os.startfile(HELP_URL)
999985

src/manage/logging.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,40 @@ def strip_colour(msg):
4848
return msg
4949

5050

51+
def _len_without_codes(s, codes_subbed=False):
52+
n = len(s)
53+
for k, v in COLOURS.items():
54+
if not codes_subbed:
55+
n -= len(k) * s.count(k)
56+
n -= len(v) * s.count(v)
57+
return n
58+
59+
60+
def wrap_and_indent(s, indent=0, width=None, hang="", codes_subbed=False):
61+
if width is None:
62+
width = CONSOLE_MAX_WIDTH
63+
64+
bits = [" " * indent]
65+
if hang:
66+
cchw = _len_without_codes(hang, codes_subbed=codes_subbed)
67+
if cchw <= indent - 1:
68+
bits = [hang + " " * (indent - cchw)]
69+
else:
70+
yield hang
71+
cch = indent
72+
for w in s.split(" "):
73+
cchw = _len_without_codes(w, codes_subbed=codes_subbed)
74+
if len(bits) > 1 and cch + cchw > width:
75+
yield "".join(bits).rstrip()
76+
bits = [" " * indent]
77+
cch = indent
78+
bits.append(w)
79+
bits.append(" ")
80+
cch += cchw + 1
81+
if bits:
82+
yield "".join(bits).rstrip()
83+
84+
5185
def supports_colour(stream):
5286
if os.getenv("PYTHON_COLORS", "").lower() in ("0", "no", "false"):
5387
return False
@@ -62,7 +96,7 @@ def supports_colour(stream):
6296
if type(stream).__name__ != "_WindowsConsoleIO":
6397
return False
6498
try:
65-
# Allows us to import logging on its own
99+
# Lazy import to allow us to import logging on its own
66100
from _native import fd_supports_vt100
67101
return fd_supports_vt100(stream.fileno())
68102
except Exception:
@@ -187,16 +221,10 @@ def print(self, msg=None, *args, always=False, level=INFO, colours=True, wrap=Fa
187221
else:
188222
msg = ""
189223
if wrap:
190-
while len(msg) > CONSOLE_MAX_WIDTH:
191-
part = msg[:CONSOLE_MAX_WIDTH]
192-
n = 0
193-
while len(part) > n:
194-
n = len(part)
195-
part = msg[:CONSOLE_MAX_WIDTH + 5 * part.count("\033")]
196-
pre, sep, rest = part.rpartition(" ")
197-
print(pre, **kwargs, file=self.print_console)
198-
msg = rest + msg[len(part):]
199-
print(msg, **kwargs, file=self.print_console)
224+
for s in wrap_and_indent(msg, codes_subbed=True):
225+
print(s, **kwargs, file=self.print_console)
226+
else:
227+
print(msg, **kwargs, file=self.print_console)
200228

201229
def print_raw(self, *msg, **kwargs):
202230
kwargs["always"] = True

tests/test_logging.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import pytest
2+
3+
from manage import logging
4+
5+
6+
def test_wrap_and_indent():
7+
r = list(logging.wrap_and_indent("12345678 12345 123 1 123456 1234567890",
8+
width=8))
9+
assert r == [
10+
"12345678",
11+
"12345",
12+
"123 1",
13+
"123456",
14+
"1234567890",
15+
]
16+
17+
r = list(logging.wrap_and_indent("12345678 12345 123 1 123456 1234567890",
18+
indent=4, width=8))
19+
assert r == [
20+
" 12345678",
21+
" 12345",
22+
" 123",
23+
" 1",
24+
" 123456",
25+
" 1234567890",
26+
]
27+
28+
r = list(logging.wrap_and_indent("12345678 12345 123 1 123456 1234567890",
29+
indent=4, width=8, hang="AB"))
30+
assert r == [
31+
"AB 12345678",
32+
" 12345",
33+
" 123",
34+
" 1",
35+
" 123456",
36+
" 1234567890",
37+
]
38+
39+
r = list(logging.wrap_and_indent("12345678 12345 123 1 123456 1234567890",
40+
indent=4, width=8, hang="ABC"))
41+
assert r == [
42+
"ABC 12345678",
43+
" 12345",
44+
" 123",
45+
" 1",
46+
" 123456",
47+
" 1234567890",
48+
]
49+
50+
r = list(logging.wrap_and_indent("12345678 12345 123 1 123456 1234567890",
51+
indent=3, width=8, hang="ABCD"))
52+
assert r == [
53+
"ABC",
54+
" 12345678",
55+
" 12345",
56+
" 123 1",
57+
" 123456",
58+
" 1234567890",
59+
]

0 commit comments

Comments
 (0)