![]() |
OpenMS
|
Singleton helper for terminal-width-aware string wrapping in TOPP-tool console output. More...
#include <OpenMS/APPLICATIONS/ConsoleUtils.h>
Public Member Functions | |
| ConsoleUtils (const ConsoleUtils &)=delete | |
| Copy constructor deleted (singleton) | |
| void | operator= (ConsoleUtils const &)=delete |
| Assignment operator deleted (singleton) | |
| int | getConsoleWidth () const |
Cached console width (or std::numeric_limits<int>::max() when detection failed and wrapping is effectively disabled) | |
Static Public Member Functions | |
| static const ConsoleUtils & | getInstance () |
| Return the singleton instance (constructed on first call; thread-safe per Meyers-singleton rules) | |
| static StringList | breakStringList (const std::string &input, const Size indentation, const Size max_lines, const Size first_line_prefill=0) |
Wrap input to the current console width and return one element per output line. | |
| static std::string | breakString (const std::string &input, const Size indentation, const Size max_lines, const Size first_line_prefill=0) |
Convenience wrapper around breakStringList that joins the result with "\\n". | |
Private Member Functions | |
| ConsoleUtils () | |
| Private ctor — caches the console width once; use getInstance to access. | |
| int | readConsoleSize_ () |
Probe the host environment / terminal to determine the console width (also honours the COLUMNS env var) | |
| StringList | breakString_ (const std::string &input, const Size indentation, const Size max_lines, Size first_line_prefill) const |
Non-static implementation used by the public static wrappers; receives the cached console_width_. | |
Private Attributes | |
| int | console_width_ = std::numeric_limits<int>::max() |
Cached console width; numeric_limits<int>::max() means "could not detect → no wrapping". | |
Friends | |
| struct | ConsoleWidthTest |
Test hook: lets unit tests pin console_width_ to a deterministic value. | |
Singleton helper for terminal-width-aware string wrapping in TOPP-tool console output.
Detects the current console width on construction (so it reflects the terminal the program was launched in) and exposes helpers that wrap long strings to that width with hanging indentation — the typical pattern used by TOPP tools to format --help output, parameter descriptions, and CLI error messages.
The detected width can be overridden at run time by setting the COLUMNS environment variable before launching the program. If the width cannot be determined (e.g. when stdout is not a terminal), it falls back to std::numeric_limits<int>::max(), which effectively disables wrapping.
Construction is private; access via getInstance(). The class is non-copyable and the cached console width is read once per process — long-running tools that resize their terminal mid-run will continue to use the original width.
|
private |
Private ctor — caches the console width once; use getInstance to access.
|
delete |
Copy constructor deleted (singleton)
|
static |
Convenience wrapper around breakStringList that joins the result with "\\n".
|
private |
Non-static implementation used by the public static wrappers; receives the cached console_width_.
|
static |
Wrap input to the current console width and return one element per output line.
Behaviour:
indentation spaces, giving a left-aligned hanging block; lines wider than the console get broken at the console width.indentation == 0 falls back to the native console behaviour (raw line break at the right edge with no prefix on the next line).max_lines, excess lines are dropped and a "..." marker is inserted; the final line is always preserved so the tail of the message stays visible.first_line_prefill lets the caller advertise how many characters are already present on the current console line (e.g. when this string is being appended after a label), so the first wrap point is calculated correctly.| [in] | input | String to wrap. |
| [in] | indentation | Spaces to prepend to lines 2..N (must not exceed console width). |
| [in] | max_lines | Maximum output lines; excess lines collapse into "..." with the last line kept. |
| [in] | first_line_prefill | Number of characters already written on the current line (must not exceed console width). |
|
inline |
Cached console width (or std::numeric_limits<int>::max() when detection failed and wrapping is effectively disabled)
|
static |
Return the singleton instance (constructed on first call; thread-safe per Meyers-singleton rules)
|
delete |
Assignment operator deleted (singleton)
|
private |
Probe the host environment / terminal to determine the console width (also honours the COLUMNS env var)
|
friend |
Test hook: lets unit tests pin console_width_ to a deterministic value.
|
private |
Cached console width; numeric_limits<int>::max() means "could not detect → no wrapping".