OpenMS
Loading...
Searching...
No Matches
ConsoleUtils Class Reference

Singleton helper for terminal-width-aware string wrapping in TOPP-tool console output. More...

#include <OpenMS/APPLICATIONS/ConsoleUtils.h>

Collaboration diagram for ConsoleUtils:
[legend]

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 ConsoleUtilsgetInstance ()
 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.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ ConsoleUtils() [1/2]

ConsoleUtils ( )
private

Private ctor — caches the console width once; use getInstance to access.

◆ ConsoleUtils() [2/2]

ConsoleUtils ( const ConsoleUtils )
delete

Copy constructor deleted (singleton)

Member Function Documentation

◆ breakString()

static std::string breakString ( const std::string &  input,
const Size  indentation,
const Size  max_lines,
const Size  first_line_prefill = 0 
)
static

Convenience wrapper around breakStringList that joins the result with "\\n".

◆ breakString_()

StringList breakString_ ( const std::string &  input,
const Size  indentation,
const Size  max_lines,
Size  first_line_prefill 
) const
private

Non-static implementation used by the public static wrappers; receives the cached console_width_.

◆ breakStringList()

static StringList breakStringList ( const std::string &  input,
const Size  indentation,
const Size  max_lines,
const Size  first_line_prefill = 0 
)
static

Wrap input to the current console width and return one element per output line.

Behaviour:

  • Lines 2..N are prefixed by 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).
  • If the wrapped output would exceed 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.
Parameters
[in]inputString to wrap.
[in]indentationSpaces to prepend to lines 2..N (must not exceed console width).
[in]max_linesMaximum output lines; excess lines collapse into "..." with the last line kept.
[in]first_line_prefillNumber of characters already written on the current line (must not exceed console width).
Returns
One element per wrapped output line.

◆ getConsoleWidth()

int getConsoleWidth ( ) const
inline

Cached console width (or std::numeric_limits<int>::max() when detection failed and wrapping is effectively disabled)

◆ getInstance()

static const ConsoleUtils & getInstance ( )
static

Return the singleton instance (constructed on first call; thread-safe per Meyers-singleton rules)

◆ operator=()

void operator= ( ConsoleUtils const &  )
delete

Assignment operator deleted (singleton)

◆ readConsoleSize_()

int readConsoleSize_ ( )
private

Probe the host environment / terminal to determine the console width (also honours the COLUMNS env var)

Friends And Related Symbol Documentation

◆ ConsoleWidthTest

friend struct ConsoleWidthTest
friend

Test hook: lets unit tests pin console_width_ to a deterministic value.

Member Data Documentation

◆ console_width_

int console_width_ = std::numeric_limits<int>::max()
private

Cached console width; numeric_limits<int>::max() means "could not detect → no wrapping".