OpenMS
IndentedStream.h
Go to the documentation of this file.
1 // Copyright (c) 2002-2023, The OpenMS Team -- 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 
13 using namespace std;
14 
15 namespace OpenMS
16 {
17  class Colorizer;
18 
35  class OPENMS_DLLAPI IndentedStream
36  {
37  public:
45  IndentedStream(std::ostream& stream, const UInt indentation, const UInt max_lines);
46 
49 
55 
58 
59  template<typename T>
60  IndentedStream& operator<<(const T& data)
61  {
62  // convert data to string
63  std::stringstream str_data;
64  str_data << data;
65 
66  auto result = ConsoleUtils::breakStringList(str_data.str(), indentation_, max_lines_, current_column_pos_);
67  if (result.empty())
68  {
69  return *this;
70  }
71 
72  if (result.size() == 1)
73  { // no new linebreak. advance our position
74  current_column_pos_ += result.back().size();
75  }
76  else
77  { // new line: this is our new position
78  current_column_pos_ = result.back().size();
79  }
80 
81 
82  // push result into stream
83  *stream_ << result[0];
84  for (size_t i = 1; i < result.size(); ++i)
85  {
86  *stream_ << '\n';
87  *stream_ << result[i];
88  }
89 
90  return *this;
91  }
92 
94  typedef std::ostream& (*StreamManipulator)(std::ostream&);
95 
97  IndentedStream& operator<<(StreamManipulator manip);
98 
101  IndentedStream& indent(const UInt new_indent);
102 
103  private:
104  std::ostream* stream_;
108  Size current_column_pos_ = 0;
109  };
110 
111 } // 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:36
std::ostream * stream_
the underlying stream to print to
Definition: IndentedStream.h:104
IndentedStream & operator<<(Colorizer &colorizer)
IndentedStream & operator<<(IndentedStream &self)
Support calling our member functions within a stream.
~IndentedStream()
D'tor flushes the stream.
UInt max_lines_
maximum number of lines a single item is split into before excess lines are replaced by '....
Definition: IndentedStream.h:106
IndentedStream(std::ostream &stream, const UInt indentation, const UInt max_lines)
C'tor.
IndentedStream & operator<<(StreamManipulator manip)
Overload for function pointers, e.g. std::endl.
IndentedStream & indent(const UInt new_indent)
UInt indentation_
number of spaces in prefix of each line
Definition: IndentedStream.h:105
IndentedStream & operator<<(const T &data)
Definition: IndentedStream.h:60
UInt max_line_width_
width of console/output
Definition: IndentedStream.h:107
unsigned int UInt
Unsigned integer type.
Definition: Types.h:68
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:101
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:22