lib: More result log beautification

This commit adds more tweaks to shell command output in order to make
it nicer. The biggest patch is in Result.__summarize(), which makes
it more versatile, and allows removal of some code in SSHClient.

App sees some independent, minor result format beautification.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-06-18 09:48:48 +02:00
commit 1e0dee5908
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
3 changed files with 60 additions and 37 deletions

View file

@ -49,7 +49,7 @@ class Result:
ret = ''.join(chr(b) if 32 <= b <= 126 else '.' for b in chunk)
if (not annotate) or truncate is None or len(stdxxx) <= truncate:
return f'{label}"{ret}"' if quote else label + ret
ret = '{labe}"{ret} ..."' if quote else '{labe}{ret} ...'
ret = f'{label}"{ret} ..."' if quote else f'{label}{ret} ...'
ret += f' + {len(stdxxx) - truncate} more bytes'
return ret
@ -59,9 +59,10 @@ class Result:
wd: str | None = None,
verbose = True
) -> str:
if not verbose:
ret = f'{self.__status}: '
else:
def __status_str(cmd: list[str] | None, wd: str | None, verbose: bool) -> str:
if not verbose:
return str(self.__status)
from .util import pretty_cmd
if cmd is None:
cmd = self.__cmd
@ -70,17 +71,46 @@ class Result:
if wd is None:
wd = self.__wd
call = f'"{pretty_cmd(cmd, wd)}" '
ret = (
f'Command {call}has not yet exited' if self.__status is None else
f'Command {call}has exited with status {self.__status}'
)
label, stdxxx, truncate = (
('stdout', self.__stdout, 40) if self.status == 0
else ('stderr', self.__stderr, None)
)
ret += self.__try_decode(
stdxxx, quote = True, truncate = truncate, annotate = True, label = label
)
if self.__status is None:
return f'Command {call}has not yet exited'
return f'Command {call}has exited with status {self.__status}'
def __out(truncate: int) -> list[str]:
ret: list[str] = []
for label, stdxxx, tr in [
('stdout', self.__stdout, truncate),
('stderr', self.__stderr, None),
]:
if stdxxx is None:
continue
ret.append(
self.__try_decode(
stdxxx,
quote = True,
truncate = tr,
annotate = True,
label = label,
)
)
return ret
max_width = 120
# Try a one-liner first
status_str = __status_str(cmd, wd, verbose)
out = __out(40)
ret = f'{status_str}'
if out:
ret += ', ' + ', '.join(out)
if len(ret) > max_width:
ret = f'{status_str}'
if out:
# Now that we're reaking into multiple lines anyway, they may
# just as well be longer
out = __out(max_width)
ret += ':\n' + '\n'.join([' ' + line for line in out])
return ret
def __repr__(self) -> str: