OpenMS
Loading...
Searching...
No Matches
IndentedStream.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Chris Bielow $
6// $Authors: Chris Bielow $
7// --------------------------------------------------------------------------
8
10
11#include <sstream>
12
13namespace OpenMS
14{
15 class Colorizer;
16
33 class OPENMS_DLLAPI IndentedStream
34 {
35 public:
43 IndentedStream(std::ostream& stream, const UInt indentation, const UInt max_lines);
44
47
53
56
57 template<typename T>
58 IndentedStream& operator<<(const T& data)
59 {
60 // convert data to string
61 std::stringstream str_data;
62 str_data << data;
63
64 auto result = ConsoleUtils::breakStringList(str_data.str(), indentation_, max_lines_, current_column_pos_);
65 if (result.empty())
66 {
67 return *this;
68 }
69
70 if (result.size() == 1)
71 { // no new linebreak. advance our position
72 current_column_pos_ += result.back().size();
73 }
74 else
75 { // new line: this is our new position
76 current_column_pos_ = result.back().size();
77 }
78
79
80 // push result into stream
81 *stream_ << result[0];
82 for (size_t i = 1; i < result.size(); ++i)
83 {
84 *stream_ << '\n';
85 *stream_ << result[i];
86 }
87
88 return *this;
89 }
90
92 typedef std::ostream& (*StreamManipulator)(std::ostream&);
93
95 IndentedStream& operator<<(StreamManipulator manip);
96
99 IndentedStream& indent(const UInt new_indent);
100
101 private:
102 std::ostream* stream_;
106 Size current_column_pos_ = 0;
107 };
108
109} // namespace OpenMS
Color and style the fonts shown on cout/cerr (or other streams)
Definition Colorizer.h:71
Class for writing data which spans multiple lines with an indentation for each line (all except the f...
Definition IndentedStream.h:34
std::ostream * stream_
the underlying stream to print to
Definition IndentedStream.h:102
IndentedStream & operator<<(Colorizer &colorizer)
~IndentedStream()
D'tor flushes the stream.
IndentedStream & indent(const UInt new_indent)
IndentedStream & operator<<(const T &data)
Definition IndentedStream.h:58
UInt max_lines_
maximum number of lines a single item is split into before excess lines are replaced by '....
Definition IndentedStream.h:104
IndentedStream(std::ostream &stream, const UInt indentation, const UInt max_lines)
C'tor.
UInt indentation_
number of spaces in prefix of each line
Definition IndentedStream.h:103
IndentedStream & operator<<(StreamManipulator manip)
Overload for function pointers, e.g. std::endl.
IndentedStream & operator<<(IndentedStream &self)
Support calling our member functions within a stream.
UInt max_line_width_
width of console/output
Definition IndentedStream.h:105
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition Types.h:97
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19