Ookii.CommandLine for C++  2.0.0
console_helper.h
Go to the documentation of this file.
1 #ifndef OOKII_CONSOLE_HELPER_H_
4 #define OOKII_CONSOLE_HELPER_H_
5 
6 #pragma once
7 
8 #include "platform_helper.h"
9 #include <iostream>
10 
11 namespace ookii
12 {
19  inline short get_console_width(short default_width = 80) noexcept
20  {
21  auto width = details::get_console_width();
22  if (width)
23  {
24  return *width;
25  }
26 
27  return default_width;
28  }
29 
31  enum class standard_stream
32  {
34  input,
36  output,
38  error
39  };
40 
47  template<typename CharType>
49  {
50  };
51 
53  template<>
54  struct console_stream<char>
55  {
57  static inline std::ostream &cout()
58  {
59  return std::cout;
60  };
61 
63  static inline std::ostream &cerr()
64  {
65  return std::cerr;
66  }
67 
69  static inline std::istream &cin()
70  {
71  return std::cin;
72  }
73  };
74 
76  template<>
77  struct console_stream<wchar_t>
78  {
80  static inline std::wostream &cout()
81  {
82  return std::wcout;
83  };
84 
86  static inline std::wostream &cerr()
87  {
88  return std::wcerr;
89  }
90 
92  static inline std::wistream &cin()
93  {
94  return std::wcin;
95  }
96  };
97 
98  namespace details
99  {
100  inline FILE *get_stream_file(standard_stream stream)
101  {
102  switch (stream)
103  {
105  return stdin;
106 
108  return stderr;
109 
110  default:
111  return stdout;
112  }
113  }
114 
115 #ifdef _WIN32
116 #ifdef OOKII_PLATFORM_FUNC_HAS_BODY
117  OOKII_PLATFORM_FUNC(HANDLE get_stream_handle(standard_stream stream))
118  {
119  switch (stream)
120  {
122  return GetStdHandle(STD_INPUT_HANDLE);
123 
125  return GetStdHandle(STD_ERROR_HANDLE);
126 
127  default:
128  return GetStdHandle(STD_OUTPUT_HANDLE);
129  }
130  }
131 #endif
132 
133  inline int get_stream_fd(standard_stream stream)
134  {
135  return _fileno(get_stream_file(stream));
136  }
137 
138  OOKII_PLATFORM_FUNC(bool is_console(standard_stream stream))
139 #ifdef OOKII_PLATFORM_FUNC_HAS_BODY
140  {
141  return _isatty(get_stream_fd(stream));
142  }
143 #endif
144 
145 #else
146  inline int get_stream_fd(standard_stream stream)
147  {
148  return fileno(get_stream_file(stream));
149  }
150 
151  OOKII_PLATFORM_FUNC(bool is_console(standard_stream stream))
152 #ifdef OOKII_PLATFORM_FUNC_HAS_BODY
153  {
154  return isatty(get_stream_fd(stream));
155  }
156 #endif
157 #endif
158  }
159 
161  enum class vt_result
162  {
164  failed,
166  no_change,
168  success
169  };
170 
184 #ifdef _WIN32
185  OOKII_PLATFORM_FUNC(vt_result set_console_vt_support(standard_stream stream, bool enable) noexcept)
186 #ifdef OOKII_PLATFORM_FUNC_HAS_BODY
187  {
188  auto handle = details::get_stream_handle(stream);
189  DWORD mode;
190  if (!GetConsoleMode(handle, &mode))
191  {
192  return vt_result::failed;
193  }
194 
195  auto flag = stream == standard_stream::input
196  ? ENABLE_VIRTUAL_TERMINAL_INPUT
197  : ENABLE_VIRTUAL_TERMINAL_PROCESSING;
198 
199  auto old_mode = mode;
200  if (enable)
201  {
202  mode |= flag;
203  }
204  else
205  {
206  mode &= ~flag;
207  }
208 
209  if (mode == old_mode)
210  {
211  return vt_result::no_change;
212  }
213 
214  if (!SetConsoleMode(handle, mode))
215  {
216  return vt_result::failed;
217  }
218 
219  return vt_result::success;
220  }
221 #endif
222 #else
223  inline vt_result set_console_vt_support([[maybe_unused]] standard_stream stream, [[maybe_unused]] bool enable) noexcept
224  {
225  return vt_result::no_change;
226  }
227 #endif
228 
229 }
230 
231 #endif
Namespace containing the core Ookii.CommandLine.Cpp types.
Definition: command_line_argument.h:16
standard_stream
Represents one of the standard console streams.
Definition: console_helper.h:32
@ output
The standard output stream.
@ input
The standard input stream.
@ error
The standard error stream.
vt_result
Indicates the result of the set_console_vt_support operation.
Definition: console_helper.h:162
@ no_change
No action was taken, because the value already matched the requested setting.
@ success
Virtual terminal sequences were successfully enabled.
@ failed
Virtual terminal sequences could not be enabled.
vt_result set_console_vt_support([[maybe_unused]] standard_stream stream, [[maybe_unused]] bool enable) noexcept
Enables or disables console support for virtual terminal sequences.
Definition: console_helper.h:223
short get_console_width(short default_width=80) noexcept
Determines the width of the console.
Definition: console_helper.h:19
Provides platform-specific functionality depending on the target platform for compilation.
static std::ostream & cerr()
Provides access to cerr.
Definition: console_helper.h:63
static std::istream & cin()
Provides access to cin.
Definition: console_helper.h:69
static std::ostream & cout()
Provides access to cout.
Definition: console_helper.h:57
static std::wostream & cout()
Provides access to wcout.
Definition: console_helper.h:80
static std::wistream & cin()
Provides access to wcin.
Definition: console_helper.h:92
static std::wostream & cerr()
Provides access to wcerr.
Definition: console_helper.h:86
Template to determine the correct console streams based on the character type.
Definition: console_helper.h:49