CBMC
help_formatter.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: Help Formatter
4 
5 Author: Daniel Kroening, dkr@amazon.com
6 
7 \*******************************************************************/
8 
9 #include "help_formatter.h"
10 
11 #include "console.h"
12 
13 #include <ostream>
14 
15 void help_formattert::emit_word(statet &state, std::ostream &out)
16 {
17  if(state.word.empty())
18  return;
19 
20  // screen width exceeded when aligning?
21  if(state.column + state.word.size() > consolet::width() && state.aligning)
22  {
23  out << '\n' << std::string(first_column_width + 1, ' ');
24  state.column = first_column_width + 1;
25  }
26 
27  out << state.word;
28  state.column += state.word.size();
29  state.word.clear();
30 }
31 
32 void help_formattert::operator()(std::ostream &out) const
33 {
34  std::size_t pos = 0;
35  auto next = [this, &pos]() -> char {
36  if(pos < s.size())
37  return s[pos++];
38  else
39  return 0;
40  };
41 
42  statet state;
43 
44  while(pos < s.size())
45  {
46  auto ch = next();
47 
48  if(ch == '{')
49  {
50  emit_word(state, out);
51 
52  auto what = next();
53  // spaces in formatted text are non-breakable
54  while(pos < s.size() && (ch = next()) != '}')
55  state.word += ch;
56 
57  switch(what)
58  {
59  case 'b':
60  out << consolet::bold;
61  break;
62  case 'u':
63  out << consolet::underline;
64  break;
65  case 'y':
66  out << consolet::yellow;
67  break;
68  default:
69  break;
70  }
71 
72  emit_word(state, out);
73  out << consolet::reset;
74  }
75  else if(ch == '\t')
76  {
77  emit_word(state, out);
78  if(state.column < first_column_width)
79  {
80  out << std::string(first_column_width - state.column, ' ');
81  state.column = first_column_width;
82  }
83  state.aligning = true;
84  }
85  else if(ch == '\n')
86  {
87  emit_word(state, out);
88  out << '\n';
89  state.column = 0;
90  state.aligning = false;
91  }
92  else
93  {
94  state.word += ch;
95  if(ch == ' ')
96  emit_word(state, out);
97  }
98  }
99 
100  emit_word(state, out);
101 }
static std::ostream & yellow(std::ostream &)
Definition: console.cpp:136
static std::ostream & underline(std::ostream &)
Definition: console.cpp:168
static std::ostream & reset(std::ostream &)
Definition: console.cpp:176
static std::size_t width()
Definition: console.cpp:196
static std::ostream & bold(std::ostream &)
Definition: console.cpp:152
static const std::size_t first_column_width
void operator()(std::ostream &) const
static void emit_word(statet &, std::ostream &)
const std::string & s
Console.
Help Formatter.
literalt pos(literalt a)
Definition: literal.h:194