1 // Copyright 2021 Hans Dembinski
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt
5 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #ifndef BOOST_HISTOGRAM_DETAIL_TERM_INFO_HPP
8 #define BOOST_HISTOGRAM_DETAIL_TERM_INFO_HPP
12 #if defined __has_include
13 #if __has_include(<sys/ioctl.h>) && __has_include(<unistd.h>)
14 #include <sys/ioctl.h>
18 #include <boost/config.hpp>
29 env_t(const char* key) {
30 #if defined(BOOST_MSVC) // msvc complains about using std::getenv
31 _dupenv_s(&data_, &size_, key);
33 data_ = std::getenv(key);
34 if (data_) size_ = std::strlen(data_);
39 #if defined(BOOST_MSVC)
44 bool contains(const char* s) {
45 const std::size_t n = std::strlen(s);
46 if (size_ < n) return false;
47 return std::strstr(data_, s);
50 operator bool() { return size_ > 0; }
52 explicit operator int() { return size_ ? std::atoi(data_) : 0; }
54 const char* data() const { return data_; }
58 std::size_t size_ = 0;
62 // return false only if LANG exists and does not contain the string UTF
65 if (env) b = env.contains("UTF") || env.contains("utf");
71 #if defined TIOCGWINSZ
73 ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
74 w = (std::max)(static_cast<int>(ws.ws_col), 0); // not sure if ws_col can be less than 0
77 const int col = (std::max)(static_cast<int>(env), 0);
78 // if both t and w are set, COLUMNS may be used to restrict width
79 return w == 0 ? col : (std::min)(col, w);
81 } // namespace term_info
84 } // namespace histogram