New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrap ./pants help
based on actual terminal width
#11378
Conversation
@Eric-Arellano : This can likely be refreshed post #11536: I spent some time investigating an issue with widths not propagating, and the |
# Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
No luck. Using
|
d3521a0
to
825ce87
Compare
Ok, interesting. The rust Assuming that this is actually a backport: https://github.com/Snaipe/python-rst2ansi/blob/c6f390b45be689a5760060c990e3fe10f502e671/rst2ansi/get_terminal_size.py#L70-L125 ... it's mostly the same code, and the difference appears to be that it's using The diff below correctly renders:
diff --git a/src/python/pants/bin/daemon_pants_runner.py b/src/python/pants/bin/daemon_pants_runner.py
index dff459867..89a0ebf2b 100644
--- a/src/python/pants/bin/daemon_pants_runner.py
+++ b/src/python/pants/bin/daemon_pants_runner.py
@@ -166,6 +166,8 @@ class DaemonPantsRunner(RawFdRunner):
stdout_fileno=stdout_fileno,
stderr_fileno=stderr_fileno,
), hermetic_environment_as(**env), argv_as((command,) + args):
+ import shutil
+ logger.info(f"terminal_size: {shutil.get_terminal_size()}")
return self.single_daemonized_run(
working_directory.decode(), cancellation_latch
)
diff --git a/src/python/pants/init/logging.py b/src/python/pants/init/logging.py
index 1e580f0f5..af8f5e8f3 100644
--- a/src/python/pants/init/logging.py
+++ b/src/python/pants/init/logging.py
@@ -163,10 +163,12 @@ def initialize_stdio(global_bootstrap_options: OptionValueContainer) -> Iterator
tuple(message_regex_filters),
log_path,
)
+ sys.__stdin__, sys.__stdout__, sys.__stderr__ = sys.stdin, sys.stdout, sys.stderr
# Install a Python logger that will route through the Rust logger.
with _python_logging_setup(global_level, print_stacktrace):
yield
finally:
+ sys.__stdin__, sys.__stdout__, sys.__stderr__ = original_stdin, original_stdout, original_stderr
sys.stdin, sys.stdout, sys.stderr = original_stdin, original_stdout, original_stderr
|
Oh, so: alternatively, could use https://docs.python.org/3/library/os.html#os.get_terminal_size ?:
|
If the |
# Rust tests and lints will be skipped. Delete if not intended. [ci skip-rust] # Building wheels and fs_util will be skipped. Delete if not intended. [ci skip-build-wheels]
./pants help
based on actual terminal width./pants help
based on actual terminal width
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
@@ -34,7 +35,7 @@ def add_option(ohis, *, category=None): | |||
# No need to repeat those in the advanced section. | |||
title = f"{display_scope} options" | |||
lines.append(self.maybe_green(f"{title}\n{'-' * len(title)}\n")) | |||
lines.extend(hard_wrap(oshi.description)) | |||
lines.extend(hard_wrap(oshi.description, width=terminal_width())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Each call to terminal_width
is a syscall, so it might be good to just call it once at the top.
src/python/pants/init/logging.py
Outdated
sys.__stdin__, sys.__stdout__, sys.__stderr__ = ( | ||
original_stdin, | ||
original_stdout, | ||
original_stderr, | ||
) | ||
sys.stdin, sys.stdout, sys.stderr = original_stdin, original_stdout, original_stderr |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: A bit nicer symmetry with the above maybe:
sys.__stdin__, sys.__stdout__, sys.__stderr__ = ( | |
original_stdin, | |
original_stdout, | |
original_stderr, | |
) | |
sys.stdin, sys.stdout, sys.stderr = original_stdin, original_stdout, original_stderr | |
sys.stdin, sys.stdout, sys.stderr = original_stdin, original_stdout, original_stderr | |
sys.__stdin__, sys.__stdout__, sys.__stderr__ = sys.stdin, sys.stdout, sys.stderr |
Rather than assuming 96 columns, we can use
shutil
to query how wide the user's terminal actually is.Because we do not memoize this function, it works with terminal resizes. This is crucial when using Pantsd, otherwise the memoization would last the lifetime of the daemon instance.
[ci skip-rust]
[ci skip-build-wheels]