arg_router  1.4.0
C++ command line argument parsing and routing
terminal.hpp
1 // Copyright (C) 2022-2023 by Camden Mannett.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
4 
5 #pragma once
6 
7 #include <cstdint>
8 
9 #if defined(__linux__) || defined(__APPLE__)
10 # include <sys/ioctl.h>
11 #elif _WIN32
13 #endif
14 
17 {
18 #ifdef UNIT_TEST_BUILD
19 extern std::size_t test_columns_value;
20 #endif
21 
26 inline std::size_t columns()
27 {
28 #ifdef UNIT_TEST_BUILD
29  return test_columns_value;
30 #else
31 # if defined(__linux__) || defined(__APPLE__)
32  // NOLINTBEGIN(*-member-init, *-vararg)
33  struct winsize w;
34  if (::ioctl(0, TIOCGWINSZ, &w) != 0) {
35  return 0;
36  }
37  // NOLINTEND(*-member-init, *-vararg)
38  return w.ws_col;
39 # elif _WIN32
40  CONSOLE_SCREEN_BUFFER_INFO csbi;
41  if (!GetConsoleScreenBufferInfo(GetStdHandle(STD_ERROR_HANDLE), &csbi)) {
42  return 0;
43  }
44  return csbi.srWindow.Right - csbi.srWindow.Left + 1;
45 # endif
46 #endif
47 }
48 } // namespace arg_router::utility::terminal