123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- #ifndef BOOST_HISTOGRAM_DETAIL_TERM_INFO_HPP
- #define BOOST_HISTOGRAM_DETAIL_TERM_INFO_HPP
- #include <algorithm>
- #if defined __has_include
- #if __has_include(<sys/ioctl.h>) && __has_include(<unistd.h>)
- #include <sys/ioctl.h>
- #include <unistd.h>
- #endif
- #endif
- #include <boost/config.hpp>
- #include <cstdlib>
- #include <cstring>
- namespace boost {
- namespace histogram {
- namespace detail {
- namespace term_info {
- class env_t {
- public:
- env_t(const char* key) {
- #if defined(BOOST_MSVC)
- _dupenv_s(&data_, &size_, key);
- #else
- data_ = std::getenv(key);
- if (data_) size_ = std::strlen(data_);
- #endif
- }
- ~env_t() {
- #if defined(BOOST_MSVC)
- std::free(data_);
- #endif
- }
- bool contains(const char* s) {
- const std::size_t n = std::strlen(s);
- if (size_ < n) return false;
- return std::strstr(data_, s);
- }
- operator bool() { return size_ > 0; }
- explicit operator int() { return size_ ? std::atoi(data_) : 0; }
- const char* data() const { return data_; }
- private:
- char* data_;
- std::size_t size_ = 0;
- };
- inline bool utf8() {
-
- env_t env("LANG");
- bool b = true;
- if (env) b = env.contains("UTF") || env.contains("utf");
- return b;
- }
- inline int width() {
- int w = 0;
- #if defined TIOCGWINSZ
- struct winsize ws;
- ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
- w = (std::max)(static_cast<int>(ws.ws_col), 0);
- #endif
- env_t env("COLUMNS");
- const int col = (std::max)(static_cast<int>(env), 0);
-
- return w == 0 ? col : (std::min)(col, w);
- }
- }
- }
- }
- }
- #endif
|