Ookii.CommandLine for C++  1.0.0
console_helper.h
Go to the documentation of this file.
1 #ifndef OOKII_CONSOLE_HELPER_H_
21 #define OOKII_CONSOLE_HELPER_H_
22 
23 #pragma once
24 
25 #if !defined(OOKII_NO_PLATFORM_HEADERS) && (!defined(OOKII_CONSOLE_NOT_INLINE) || defined(OOKII_CONSOLE_DEFINITION))
26 
27 #if _WIN32
28 #define WIN32_LEAN_AND_MEAN
29 #define NOMINMAX
30 #define NOSERVICE
31 #define NOMCX
32 #define NOIME
33 #define NOSOUND
34 #define NOCOMM
35 #define NOKANJI
36 #define NORPC
37 #define NOPROXYSTUB
38 #define NOIMAGE
39 #define NOTAPE
40 #include <Windows.h>
41 #include <io.h>
42 #else
43 #include <unistd.h>
44 #include <sys/ioctl.h>
45 #endif
46 
47 #include <iostream>
48 
49 #endif
50 
51 namespace ookii
52 {
59 #ifndef OOKII_CONSOLE_NOT_INLINE
60  inline
61 #endif
62  short get_console_width(short default_width = 80) noexcept
63 #if defined(OOKII_CONSOLE_NOT_INLINE) && !defined(OOKII_CONSOLE_DEFINITION)
64  ;
65 #else
66  {
67 #if _WIN32
68 
69  HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
70  CONSOLE_SCREEN_BUFFER_INFO info;
71  if (GetConsoleScreenBufferInfo(handle, &info))
72  return info.srWindow.Right - info.srWindow.Left + 1;
73 
74 #elif defined(TIOCGWINSZ)
75 
76  struct winsize ws;
77 
78  if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0)
79  return static_cast<short>(ws.ws_col);
80 
81 #endif
82 
83  return default_width;
84  }
85 #endif
86 
93  template<typename CharType>
95  {
96  };
97 
99  template<>
100  struct console_stream<char>
101  {
103  static inline std::ostream &cout()
104  {
105  return std::cout;
106  };
107 
109  static inline std::ostream &cerr()
110  {
111  return std::cerr;
112  }
113 
115  static inline std::istream &cin()
116  {
117  return std::cin;
118  }
119  };
120 
122  template<>
123  struct console_stream<wchar_t>
124  {
126  static inline std::wostream &cout()
127  {
128  return std::wcout;
129  };
130 
132  static inline std::wostream &cerr()
133  {
134  return std::wcerr;
135  }
136 
138  static inline std::wistream &cin()
139  {
140  return std::wcin;
141  }
142  };
143 
144 }
145 
146 #endif
Namespace containing the core Ookii.CommandLine.Cpp types.
Definition: command_line_argument.h:16
short get_console_width(short default_width=80) noexcept
Determines the width of the console.
Definition: console_helper.h:62
static std::ostream & cerr()
Provides access to cerr.
Definition: console_helper.h:109
static std::istream & cin()
Provides access to cin.
Definition: console_helper.h:115
static std::ostream & cout()
Provides access to cout.
Definition: console_helper.h:103
static std::wostream & cout()
Provides access to wcout.
Definition: console_helper.h:126
static std::wistream & cin()
Provides access to wcin.
Definition: console_helper.h:138
static std::wostream & cerr()
Provides access to wcerr.
Definition: console_helper.h:132
Template to determine the correct console streams based on the character type.
Definition: console_helper.h:95