| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-FileCopyrightText: 2022-2023 Dennis Gläser <dennis.glaeser@iws.uni-stuttgart.de> | ||
| 2 | // SPDX-License-Identifier: MIT | ||
| 3 | /*! | ||
| 4 | * \file | ||
| 5 | * \ingroup Common | ||
| 6 | * \copydoc GridFormat::Indentation. | ||
| 7 | */ | ||
| 8 | #ifndef GRIDFORMAT_COMMON_INDENTATION_HPP_ | ||
| 9 | #define GRIDFORMAT_COMMON_INDENTATION_HPP_ | ||
| 10 | |||
| 11 | #include <string> | ||
| 12 | #include <ostream> | ||
| 13 | |||
| 14 | namespace GridFormat { | ||
| 15 | |||
| 16 | /*! | ||
| 17 | * \ingroup Common | ||
| 18 | * \brief Helper class for formatting output with indentations. | ||
| 19 | */ | ||
| 20 | class Indentation { | ||
| 21 | public: | ||
| 22 | struct Options { | ||
| 23 | unsigned int width = 4; | ||
| 24 | unsigned int level = 0; | ||
| 25 | }; | ||
| 26 | |||
| 27 | 1 | Indentation() : Indentation(Options{}) {} | |
| 28 | 10014 | explicit Indentation(Options opts) | |
| 29 |
1/2✓ Branch 1 taken 10014 times.
✗ Branch 2 not taken.
|
20028 | : _width{std::string(opts.width, ' ')} |
| 30 |
1/2✓ Branch 1 taken 10014 times.
✗ Branch 2 not taken.
|
20028 | , _indent{std::string(opts.width*opts.level, ' ')} |
| 31 | 10014 | {} | |
| 32 | |||
| 33 | 42 | const std::string& get() const { return _indent; } | |
| 34 | |||
| 35 | 89461 | Indentation& operator++() { push_(); return *this; } | |
| 36 | 89461 | Indentation& operator--() { pop_(); return *this; } | |
| 37 | |||
| 38 |
1/2✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | Indentation operator++(int) { auto cpy = *this; ++(*this); return cpy; } |
| 39 |
1/2✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | Indentation operator--(int) { auto cpy = *this; --(*this); return cpy; } |
| 40 | |||
| 41 | 308730 | friend std::ostream& operator<<(std::ostream& s, const Indentation& i) { | |
| 42 | 308730 | s << i._indent; | |
| 43 | 308730 | return s; | |
| 44 | } | ||
| 45 | |||
| 46 | private: | ||
| 47 | 89461 | void push_() { _indent += _width; } | |
| 48 |
1/2✓ Branch 4 taken 89461 times.
✗ Branch 5 not taken.
|
89461 | void pop_() { _indent.erase(std::max(_indent.length() - _width.length(), std::size_t{0})); } |
| 49 | |||
| 50 | std::string _width; | ||
| 51 | std::string _indent; | ||
| 52 | }; | ||
| 53 | |||
| 54 | } // end namespace GridFormat | ||
| 55 | |||
| 56 | #endif // GRIDFORMAT_COMMON_INDENTATION_HPP_ | ||
| 57 |