SST 15.0
Structural Simulation Toolkit
smartTextFormatter.h
1// Copyright 2009-2025 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2025, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_UTIL_SMARTTEXTFORMATTER_H
13#define SST_CORE_UTIL_SMARTTEXTFORMATTER_H
14
15#include <string>
16#include <vector>
17
18namespace SST::Util {
19
20/**
21 Class to format text for console output. It will wrap lines at
22 word boundaries based on the terminal width. It can also handle
23 tab and indent using various escape sequences described in the
24 append() function.
25
26 NOTE: The API is in flux and won't be final until the SST 15
27 release.
28 */
30{
31
32 /**
33 List of tab stops
34 */
35 std::vector<int> tab_stops_;
36
37 /**
38 Width of the terminal. Lines will be wrapped at this width
39 */
40 int terminal_width_;
41
42 /**
43 The output string that is being built up with calls to append
44 */
45 std::string output_;
46
47 /**
48 Used to hold the current whitespace between words
49 */
50 std::string spaces_;
51
52 /**
53 Used to hold the current word
54 */
55 std::string word_;
56
57 /**
58 Position (column) that the next character will write at in the
59 current line
60 */
61 int current_position_ = 0;
62
63 /**
64 True if the last character was a vertical tab. This is needed
65 because two vertical tabs in a row will pop the most recent
66 indent
67 */
68 bool last_char_vert_tab_ = false;
69
70 /**
71 Stack of indents. Add an indent at the currentPosition by
72 using \v. Pop the most recent indent with \v\v.
73 */
74 std::vector<size_t> indent_ = { 0 };
75
76public:
77 /**
78 Constructor for SmartTextFormatter
79
80 @param tabStops Vector of column indices that will serve as tab
81 stops
82
83 @param repeat If set to value greater than zero, the last
84 "repeat" tab stops will repeat the input pattern through the
85 whole width of the terminal (i.e., the differences from the tab
86 stop before will be used to generate new tab stops until the
87 end of the line).
88 */
89 SmartTextFormatter(const std::vector<int>& tabStops, int repeat = 0);
90
91 /**
92 Clear the formatter
93 */
94 void clear();
95
96 /**
97 Sets the tabstops for the formatter. This can be done at
98 anytime and will take immediate effect.
99
100 @param stops Vector of column indexes that will serve as tab
101 stops
102
103 @param repeat If set to value greater than zero, the last
104 "repeat" tab stops will repeat the input pattern through the
105 whole width of the terminal (i.e., the differences from the tab
106 stop before will be used to generate new tab stops until the
107 end of the line).
108 */
109 void setTabStops(const std::vector<int>& stops, int repeat = 0);
110
111 /**
112 Append a string to the formatter. The formatter will add the
113 string to the output (which can be retrieved through the str()
114 function) and add newline characters between words to wrap text
115 at the terminal boundary. The width of the terminal is
116 determined automatically and will default to 80 if the width is
117 indeterminate (for example, the output is piped to a file).
118
119 The input string can also include the following escape
120 sequences, which will behave as described:
121
122 \t - Will advance the output to the next tab stop. If the
123 current line is already beyond the current tab stop, the
124 formatter will just insert a newline
125
126 \v - Will push a new indent at the current column of the output
127 line to the indent stack
128
129 \v\v - Will pop the most recent indent from the stack
130
131 \n - Normal new line character. In addition to adding a new
132 line, it will also clear the indent stack
133
134 \r - Will insert a new line character, but advance the output
135 to the current indent position
136
137 @param input Input string to add to the formatted output
138 */
139 void append(const std::string& input);
140
141
142 /**
143 Return the current output of the formatter. Any trailing spaces
144 will be left off.
145 */
146 std::string str();
147
148private:
149 /**
150 Will return the number of spaces needed to get to next tab stop
151 */
152 int nextTabStop(int position);
153
154 int getTerminalWidth();
155};
156
157
158} // namespace SST::Util
159
160#endif // SST_CORE_UTIL_SMARTTEXTFORMATTER_H
SmartTextFormatter(const std::vector< int > &tabStops, int repeat=0)
Constructor for SmartTextFormatter.
Definition smartTextFormatter.cc:36
void clear()
Clear the formatter.
Definition smartTextFormatter.cc:24
void append(const std::string &input)
Append a string to the formatter.
Definition smartTextFormatter.cc:80
std::string str()
Return the current output of the formatter.
Definition smartTextFormatter.cc:200
void setTabStops(const std::vector< int > &stops, int repeat=0)
Sets the tabstops for the formatter.
Definition smartTextFormatter.cc:44