OpenMS
IndentedStream Class Reference

Class for writing data which spans multiple lines with an indentation for each line (all except the first). More...

#include <OpenMS/FORMAT/IndentedStream.h>

Collaboration diagram for IndentedStream:
[legend]

Public Types

typedef std::ostream &(* StreamManipulator) (std::ostream &)
 Function pointer to a function that takes an ostream, and returns it, e.g. std::endl. More...
 

Public Member Functions

 IndentedStream (std::ostream &stream, const UInt indentation, const UInt max_lines)
 C'tor. More...
 
 ~IndentedStream ()
 D'tor flushes the stream. More...
 
IndentedStreamoperator<< (Colorizer &colorizer)
 
IndentedStreamoperator<< (IndentedStream &self)
 Support calling our member functions within a stream. More...
 
template<typename T >
IndentedStreamoperator<< (const T &data)
 
IndentedStreamoperator<< (StreamManipulator manip)
 Overload for function pointers, e.g. std::endl. More...
 
IndentedStreamindent (const UInt new_indent)
 

Private Attributes

std::ostream * stream_
 the underlying stream to print to More...
 
UInt indentation_
 number of spaces in prefix of each line More...
 
UInt max_lines_
 maximum number of lines a single item is split into before excess lines are replaced by '...' More...
 
UInt max_line_width_
 width of console/output More...
 
Size current_column_pos_ = 0
 length of last(=current) line More...
 

Detailed Description

Class for writing data which spans multiple lines with an indentation for each line (all except the first).

Internally, ConsoleUtils is used to determine the width of the current console.

The stream that is written to can be any ostream (including stdout or stderr).

If a single item passed to IndentedStream::operator<< spans multiple indented lines (e.g. a large string), at most max_lines will be retained (excess lines will be replaced by '...').

You can manually insert extra linebreaks by pushing '
' into the stream (they can be part of a larger string).

The class supports coloring its output if the underlying stream is either std::cout or cerr by passing a Colorizer.

Upon destruction of IndentedStream, the underlying stream is flushed.

Member Typedef Documentation

◆ StreamManipulator

typedef std::ostream&(* StreamManipulator) (std::ostream &)

Function pointer to a function that takes an ostream, and returns it, e.g. std::endl.

Constructor & Destructor Documentation

◆ IndentedStream()

IndentedStream ( std::ostream &  stream,
const UInt  indentation,
const UInt  max_lines 
)

C'tor.

Parameters
streamUnderlying stream to write to (its lifetime must exceed the one of this IndentedStream)
indentationNumber of spaces in front of each new line written to stream
max_linesShorten excessive single items to at most this many number of lines (replacing excess with '...')

◆ ~IndentedStream()

D'tor flushes the stream.

Member Function Documentation

◆ indent()

IndentedStream& indent ( const UInt  new_indent)

Support new indentation, on the fly. This will take effect when the next line break is encountered (either manual or automatic linebreak (at the right side of the console).

◆ operator<<() [1/4]

IndentedStream& operator<< ( Colorizer colorizer)

Support normal usage of Colorizer (for coloring cout/cerr). The underlying stream will receive ANSI codes unless its a redirected(!) cout/cerr. Warning: the ANSI codes are NOT considered to advance the cursor and will lead to broken formatting if the underlying stream is NOT cout/cerr. I.e. using an IndentedStream with an underlying stringstream in combination with a Colorizer will mess up the formatting.

◆ operator<<() [2/4]

IndentedStream& operator<< ( const T &  data)
inline

◆ operator<<() [3/4]

IndentedStream& operator<< ( IndentedStream self)

Support calling our member functions within a stream.

◆ operator<<() [4/4]

IndentedStream& operator<< ( StreamManipulator  manip)

Overload for function pointers, e.g. std::endl.

Member Data Documentation

◆ current_column_pos_

Size current_column_pos_ = 0
private

length of last(=current) line

◆ indentation_

UInt indentation_
private

number of spaces in prefix of each line

◆ max_line_width_

UInt max_line_width_
private

width of console/output

◆ max_lines_

UInt max_lines_
private

maximum number of lines a single item is split into before excess lines are replaced by '...'

◆ stream_

std::ostream* stream_
private

the underlying stream to print to