gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
a Global And Modular Bsm Inference Tool
screen_print_utils.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
18 
19 /*****************************************************************
20  * To print a string directly through less, do *
21  * *
22  * print_to_screen(string, file_name); *
23  * *
24  * NOTE: this will automatically format the string for *
25  * output onto the screen -- including line breaks to *
26  * fit the screen and adding spaces to make it look good. *
27  * *
28  * NOTE: print_to_screen will print to 'more' if *
29  * 'less' is not detected. *
30  * *
31  * NOTE: In the text, all lines that begin with "#" will *
32  * be ignored. You can use "#remove_newlines" to *
33  * ignore newlines and #dont_remove_newlines to *
34  * return to default mode. See "config/scanner_plugins.dat" *
35  * *
36  * NOTE: Table formatted with table_formatter will not be *
37  * reformatted *
38  * *
39  * If you want to formatt a string for output to the *
40  * screen but don't want to actually output it, do *
41  * *
42  * std::string formatted_str = format_for_screen(string); *
43  * *
44  * NOTE: You do not have to do this if you use *
45  * print_to_screen(). *
46  * *
47  *****************************************************************/
48 
49 #ifndef __SCREEN_PRINT_UTILS__
50 #define __SCREEN_PRINT_UTILS__
51 
52 #include <string>
53 
54 namespace Gambit
55 {
56  /*********************************************/
57  /****** formats output for the screen ********/
58  /*********************************************/
59 
60  std::string format_for_screen(const std::string &input_string);
61 
62  /*************************************************/
63  /****** pipes output through less or more ********/
64  /*************************************************/
65 
66  void print_to_screen(const std::string &file_in, const std::string &name);
67 
68  /*******************************************/
69  /****** get the width of the screen ********/
70  /*******************************************/
71 
72  inline int get_screen_cols()
73  {
74  int ret = -1;
75  if (FILE* f = popen("tput cols", "r"))
76  {
77  char buffer[1024];
78  int n;
79 
80  while ((n = fread(buffer, 1, sizeof buffer, f)) > 0)
81  {
82  std::stringstream ss(std::string(buffer, n));
83  if (! (ss >> ret))
84  {
85  ret = -1;
86  }
87  }
88 
89  pclose(f);
90  }
91 
92  return ret;
93  }
94 
95  /********************************************/
96  /****** separates line to fix length ********/
97  /********************************************/
98 
99  inline std::string separate_line(std::string &line, std::string::size_type indent, std::string::size_type cols_pos)
100  {
101  std::string sub_line;
102  std::string::size_type end_pos = 0;
103  std::string::size_type end_pos_b = line.find_last_of(" ", cols_pos);
104  if (end_pos_b != std::string::npos)
105  end_pos_b = line.find_last_not_of(" ", end_pos_b);
106  std::string::size_type end_pos_a = line.find_last_of("!.?;:,$#", cols_pos-1);
107  std::string::size_type end_pos_ab = line.find_last_of("/-+=()[]{}*@&^%\\", cols_pos);
108  if (end_pos_ab == cols_pos) end_pos_ab--;
109 
110  if (end_pos_b != std::string::npos && end_pos_b > end_pos)
111  {
112  end_pos = end_pos_b;
113  }
114  if (end_pos_ab != std::string::npos && end_pos_ab > end_pos)
115  {
116  end_pos = end_pos_ab;
117  }
118  if (end_pos_a != std::string::npos && end_pos_a > end_pos)
119  {
120  end_pos = end_pos_a;
121  }
122  if (end_pos == 0 || end_pos == std::string::npos)
123  end_pos = cols_pos;
124  else
125  end_pos++;
126 
127  sub_line = line.substr(0, end_pos);
128  line = std::string(indent, ' ') + line.substr(line.find_first_not_of(" ", end_pos));
129 
130  return sub_line;
131  }
132 
133 }
134 
135 #endif
int get_screen_cols()
std::string separate_line(std::string &line, std::string::size_type indent, std::string::size_type cols_pos)
TODO: see if we can use this one:
Definition: Analysis.hpp:33
std::string format_for_screen(const std::string &input_string)
void print_to_screen(const std::string &file_in, const std::string &name)