Skip to content

Commit

Permalink
rich: io: do not insert blank lines during output stream init
Browse files Browse the repository at this point in the history
When redirecting commands output to files, do not insert
blank lines (append mode) when initializing the redirection
stream: in case the command fails, the empty line will remain
which will separate nothing.

This may be annoying e.g. when running a script to generate
boards documentation (think cheat sheet): commands may involve
DT paths (e.g. devices) that are not always defined,
printing the DTSh error on stderr, and inserting little blanks
into the generated documentation ;-)
  • Loading branch information
dottspina committed Nov 8, 2024
1 parent 6ee2ebf commit 8cac477
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/dtsh/rich/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,12 @@ def __init__(self, path: str, append: bool) -> None:
DTShRedirect.Error: Invalid path or permission errors.
"""
super().__init__()
self._append = append

# Early initialize the redirection stream and fail now on
# OS errors: we won't run a DTSh command whose result no one
# will ever see.
try:
self._append = append
self._out = open( # pylint: disable=consider-using-with
path,
"r+" if append else "w",
Expand All @@ -243,11 +247,6 @@ def __init__(self, path: str, append: bool) -> None:
except OSError as e:
raise DTShRedirect.Error(e.strerror) from e

if self._append:
# Insert a blank line into the recorded output
# as a commands separator when we append.
self.write()

def _mk_html_format(self) -> str:
font_family = _dtshconf.pref_html_font_family
if font_family:
Expand All @@ -270,7 +269,12 @@ def flush(self) -> None:
Overrides DTShOutput.flush().
"""
# Text and bakcround colors.
if self._append:
# When appending to an existing content (output file),
# insert a blank line before the last command output.
self.write()

# Text and background colors.
theme = DTSH_EXPORT_THEMES.get(
_dtshconf.pref_html_theme, DEFAULT_TERMINAL_THEME
)
Expand Down Expand Up @@ -346,8 +350,12 @@ def __init__(self, path: str, append: bool) -> None:
DTShRedirect.Error: Invalid path or permission errors.
"""
super().__init__()
self._append = append

# Early initialize the redirection stream and fail now on
# OS errors: we won't run a DTSh command whose result no one
# will ever see.
try:
self._append = append
self._out = open( # pylint: disable=consider-using-with
path,
"r+" if append else "w",
Expand All @@ -363,11 +371,6 @@ def __init__(self, path: str, append: bool) -> None:
# cropping, up to the configured maximum.
self._width = 0

if self._append:
# Insert a blank line into the recorded output
# as a commands separator when we append.
self.write()

def write(self, *args: Any, **kwargs: Any) -> None:
"""Record/capture output.
Expand All @@ -377,6 +380,16 @@ def write(self, *args: Any, **kwargs: Any) -> None:
*args: Positional arguments, Console.print() semantic.
**kwargs: Keyword arguments, Console.print() semantic.
"""
if self._append and not self._width:
# When appending to an existing content (output file),
# insert a blank line before we start to atually
# capture the last command output.
#
# NOTE: we can't do that on flush, it will be too late,
# the capture starts right bellow (that's how we can compute
# the actual width of the command output).
super().write()

# Write output to console using the maximum width.
super().write(*args, **kwargs)

Expand All @@ -403,7 +416,7 @@ def flush(self) -> None:
Overrides DTShOutput.flush().
"""
# Text and bakcround colors.
# Text and background colors.
theme = DTSH_EXPORT_THEMES.get(
_dtshconf.pref_svg_theme, DEFAULT_TERMINAL_THEME
)
Expand Down

0 comments on commit 8cac477

Please sign in to comment.