24 Commits
v1.9 ... v2.0

Author SHA1 Message Date
Pranav Srinivas Kumar
b85eadb47c Closes #80 2020-12-08 09:15:05 -06:00
Pranav
312c49bf9f Merge pull request #78 from csparker247/77-time-remaining-overflow
Mitigate overflow when calculating ETA
2020-11-16 12:27:33 -06:00
Seth Parker
d11ffd91c6 Round time estimate up to nearest nanosecond 2020-11-16 11:27:16 -05:00
Seth Parker
e01c2950f2 Use floats for remaining time math 2020-11-16 10:54:05 -05:00
Seth Parker
c3ff1fbd91 Mitigate overflow when calculating eta 2020-11-12 13:21:42 -05:00
Pranav Srinivas Kumar
af7c004832 Updated single_include after changing percent calculation in progress spinner 2020-10-26 07:59:48 -05:00
Pranav Srinivas Kumar
77d910d0cf Fixed percentage calculation for progress spinner for user-defined option::MaxProgress 2020-10-26 07:56:44 -05:00
Pranav Srinivas Kumar
f126fde1b4 Fixed missing endif in single header version 2020-09-16 20:06:05 -05:00
Pranav
a7a3f08370 Merge pull request #73 from jcnoronha/bugfix/terminate_def
Terminate def in termcolor
2020-09-16 05:35:47 -05:00
j.noronha
05955650d6 Terminate def in termcolor 2020-09-16 21:16:45 +12:00
Pranav Srinivas Kumar
3d48e2bd25 Closes #72 2020-09-15 08:54:21 -05:00
Pranav
41650d005a Merge pull request #69 from duncanmcn/mingw_compilation
Change include guard to win32
2020-07-22 05:14:10 -05:00
duncanmcn
430a73b7b6 Change include guard to win32 2020-07-22 14:53:47 +12:00
Pranav Srinivas Kumar
f87de6ccbd Fixed missing include for termcolor in dynamic progress and multi progress 2020-07-21 10:20:07 -05:00
Pranav Srinivas Kumar
04fddb9014 Removed redundant includes of termcolor.hpp and updated single include. Closes #68 2020-07-21 10:14:52 -05:00
Pranav
1e622293aa Update indicators.hpp 2020-07-05 13:32:06 -05:00
Pranav
755900d524 Update terminal_size.hpp
Closes #67
2020-07-05 13:31:45 -05:00
Pranav
b43da291f6 Update README.md 2020-06-21 11:53:00 -05:00
Pranav
d9a5219d09 Merge pull request #66 from godbyk/bugfix/nested-namespace
Elevate #includes from being nested in the namespace.
2020-06-17 08:35:52 -05:00
Pranav
51e3460ad0 Merge pull request #65 from godbyk/feature/workaround-missing-codecvt
Support older compilers with missing <codecvt> header.
2020-06-17 08:35:13 -05:00
Kevin M. Godby
74c36cbb93 Support older compilers with missing <codecvt> header. 2020-06-16 13:29:02 -05:00
Kevin M. Godby
3cef73d30d Elevate #includes from being nested in the namespace. 2020-06-16 13:23:45 -05:00
Pranav Srinivas Kumar
50f89e150d Updated single header file version with fix for #63 2020-06-01 08:04:13 -05:00
Pranav Srinivas Kumar
ccd9c5d99d Closes 63 2020-06-01 08:03:40 -05:00
13 changed files with 114 additions and 675 deletions

View File

@@ -15,6 +15,7 @@
<img src="https://img.shields.io/badge/version-1.9-blue.svg?cacheSeconds=2592000" alt="version"/>
</p>
<p align="center">
<img src="img/demo.gif"/>
</p>

View File

@@ -167,7 +167,7 @@ private:
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}
@@ -200,7 +200,10 @@ private:
if (saved_start_time) {
auto eta = std::chrono::nanoseconds(
progress_ > 0 ? static_cast<long long>(elapsed.count() * max_progress / progress_) : 0);
progress_ > 0
? static_cast<long long>(std::ceil(float(elapsed.count()) *
max_progress / progress_))
: 0);
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
details::write_duration(os, remaining);
} else {
@@ -216,7 +219,7 @@ private:
os << " " << get_value<details::ProgressBarOption::postfix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}

View File

@@ -1,6 +1,5 @@
#pragma once
#include <indicators/termcolor.hpp>
namespace indicators {
enum class Color { grey, red, green, yellow, blue, magenta, cyan, white, unspecified };

View File

@@ -1,7 +1,10 @@
#pragma once
#include <clocale>
#if __has_include(<codecvt>)
#include <codecvt>
#define INDICATORS_HAVE_CODECVT 1
#endif
#include <cstdlib>
#include <locale>
#include <string>
@@ -9,6 +12,7 @@
namespace unicode {
#if INDICATORS_HAVE_CODECVT
namespace details {
/*
@@ -295,4 +299,16 @@ static inline int display_width(const std::wstring &input) {
return details::mk_wcswidth(input.c_str(), input.size());
}
#else
static inline int display_width(const std::string &input) {
return input.length();
}
static inline int display_width(const std::wstring &input) {
return input.length();
}
#endif
} // namespace unicode

View File

@@ -4,6 +4,7 @@
#include <functional>
#include <indicators/color.hpp>
#include <indicators/setting.hpp>
#include <indicators/details/stream_helper.hpp>
#include <iostream>
#include <mutex>
#include <vector>

View File

@@ -163,7 +163,7 @@ private:
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}
@@ -172,7 +172,7 @@ private:
os << " " << get_value<details::ProgressBarOption::postfix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}

View File

@@ -8,6 +8,7 @@
#include <indicators/color.hpp>
#include <indicators/cursor_movement.hpp>
#include <indicators/details/stream_helper.hpp>
namespace indicators {

View File

@@ -219,7 +219,7 @@ private:
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}
@@ -255,9 +255,10 @@ private:
if (saved_start_time) {
auto eta = std::chrono::nanoseconds(
progress_ > 0 ? static_cast<long long>(elapsed_.count() *
max_progress / progress_)
: 0);
progress_ > 0
? static_cast<long long>(std::ceil(float(elapsed_.count()) *
max_progress / progress_))
: 0);
auto remaining = eta > elapsed_ ? (eta - elapsed_) : (elapsed_ - eta);
details::write_duration(os, remaining);
} else {
@@ -273,7 +274,7 @@ private:
os << " " << get_value<details::ProgressBarOption::postfix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}

View File

@@ -181,7 +181,7 @@ public:
os << get_value<details::ProgressBarOption::spinner_states>()
[index_ % get_value<details::ProgressBarOption::spinner_states>().size()];
if (get_value<details::ProgressBarOption::show_percentage>()) {
os << " " << std::min(progress_, size_t(max_progress)) << "%";
os << " " << std::size_t(progress_ / double(max_progress) * 100) << "%";
}
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
@@ -195,7 +195,10 @@ public:
else
os << " [";
auto eta = std::chrono::nanoseconds(
progress_ > 0 ? static_cast<long long>(elapsed.count() * max_progress / progress_) : 0);
progress_ > 0
? static_cast<long long>(std::ceil(float(elapsed.count()) *
max_progress / progress_))
: 0);
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
details::write_duration(os, remaining);
os << "]";

View File

@@ -34,9 +34,14 @@
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
# include <unistd.h>
#elif defined(TERMCOLOR_OS_WINDOWS)
#if defined(_MSC_VER)
#if !defined(NOMINMAX)
#define NOMINMAX
#endif
# include <io.h>
# include <windows.h>
#endif
#endif
#include <iostream>

View File

@@ -2,26 +2,32 @@
#pragma once
#include <utility>
namespace indicators {
#if defined(_MSC_VER)
#if defined(_WIN32)
#include <windows.h>
namespace indicators {
static inline std::pair<size_t, size_t> terminal_size() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;
int cols, rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
return {static_cast<size_t>(rows), static_cast<size_t>(cols)};
}
size_t terminal_width() { return terminal_size().second; }
static inline size_t terminal_width() { return terminal_size().second; }
} // namespace indicators
#else
#include <sys/ioctl.h> //ioctl() and TIOCGWINSZ
#include <unistd.h> // for STDOUT_FILENO
namespace indicators {
static inline std::pair<size_t, size_t> terminal_size() {
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
@@ -29,6 +35,8 @@ static inline std::pair<size_t, size_t> terminal_size() {
}
static inline size_t terminal_width() { return terminal_size().second; }
} // namespace indicators
#endif
} // namespace indicators

View File

@@ -1,7 +1,6 @@
#include <chrono>
#include <indicators/cursor_control.hpp>
#include <indicators/indeterminate_progress_bar.hpp>
#include <indicators/termcolor.hpp>
#include <thread>
int main() {

View File

@@ -1,642 +1,5 @@
#pragma once
// #include <indicators/termcolor.hpp>
//!
//! termcolor
//! ~~~~~~~~~
//!
//! termcolor is a header-only c++ library for printing colored messages
//! to the terminal. Written just for fun with a help of the Force.
//!
//! :copyright: (c) 2013 by Ihor Kalnytskyi
//! :license: BSD, see LICENSE for details
//!
#ifndef TERMCOLOR_HPP_
#define TERMCOLOR_HPP_
// the following snippet of code detects the current OS and
// defines the appropriate macro that is used to wrap some
// platform specific things
#if defined(_WIN32) || defined(_WIN64)
# define TERMCOLOR_OS_WINDOWS
#elif defined(__APPLE__)
# define TERMCOLOR_OS_MACOS
#elif defined(__unix__) || defined(__unix)
# define TERMCOLOR_OS_LINUX
#else
# error unsupported platform
#endif
// This headers provides the `isatty()`/`fileno()` functions,
// which are used for testing whether a standart stream refers
// to the terminal. As for Windows, we also need WinApi funcs
// for changing colors attributes of the terminal.
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
# include <unistd.h>
#elif defined(TERMCOLOR_OS_WINDOWS)
# include <io.h>
# include <windows.h>
#endif
#include <iostream>
#include <cstdio>
namespace termcolor
{
// Forward declaration of the `_internal` namespace.
// All comments are below.
namespace _internal
{
// An index to be used to access a private storage of I/O streams. See
// colorize / nocolorize I/O manipulators for details.
static int colorize_index = std::ios_base::xalloc();
inline FILE* get_standard_stream(const std::ostream& stream);
inline bool is_colorized(std::ostream& stream);
inline bool is_atty(const std::ostream& stream);
#if defined(TERMCOLOR_OS_WINDOWS)
inline void win_change_attributes(std::ostream& stream, int foreground, int background=-1);
#endif
}
inline
std::ostream& colorize(std::ostream& stream)
{
stream.iword(_internal::colorize_index) = 1L;
return stream;
}
inline
std::ostream& nocolorize(std::ostream& stream)
{
stream.iword(_internal::colorize_index) = 0L;
return stream;
}
inline
std::ostream& reset(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[00m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream, -1, -1);
#endif
}
return stream;
}
inline
std::ostream& bold(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[1m";
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
inline
std::ostream& dark(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[2m";
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
inline
std::ostream& italic(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[3m";
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
inline
std::ostream& underline(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[4m";
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
inline
std::ostream& blink(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[5m";
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
inline
std::ostream& reverse(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[7m";
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
inline
std::ostream& concealed(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[8m";
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
inline
std::ostream& crossed(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[9m";
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
template <uint8_t code> inline
std::ostream& color(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
char command[12];
std::snprintf(command, sizeof(command), "\033[38;5;%dm", code);
stream << command;
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
template <uint8_t code> inline
std::ostream& on_color(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
char command[12];
std::snprintf(command, sizeof(command), "\033[48;5;%dm", code);
stream << command;
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
template <uint8_t r, uint8_t g, uint8_t b> inline
std::ostream& color(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
char command[20];
std::snprintf(command, sizeof(command), "\033[38;2;%d;%d;%dm", r, g, b);
stream << command;
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
template <uint8_t r, uint8_t g, uint8_t b> inline
std::ostream& on_color(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
char command[20];
std::snprintf(command, sizeof(command), "\033[48;2;%d;%d;%dm", r, g, b);
stream << command;
#elif defined(TERMCOLOR_OS_WINDOWS)
#endif
}
return stream;
}
inline
std::ostream& grey(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[30m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream,
0 // grey (black)
);
#endif
}
return stream;
}
inline
std::ostream& red(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[31m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream,
FOREGROUND_RED
);
#endif
}
return stream;
}
inline
std::ostream& green(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[32m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream,
FOREGROUND_GREEN
);
#endif
}
return stream;
}
inline
std::ostream& yellow(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[33m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream,
FOREGROUND_GREEN | FOREGROUND_RED
);
#endif
}
return stream;
}
inline
std::ostream& blue(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[34m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream,
FOREGROUND_BLUE
);
#endif
}
return stream;
}
inline
std::ostream& magenta(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[35m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream,
FOREGROUND_BLUE | FOREGROUND_RED
);
#endif
}
return stream;
}
inline
std::ostream& cyan(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[36m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream,
FOREGROUND_BLUE | FOREGROUND_GREEN
);
#endif
}
return stream;
}
inline
std::ostream& white(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[37m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream,
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
);
#endif
}
return stream;
}
inline
std::ostream& on_grey(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[40m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream, -1,
0 // grey (black)
);
#endif
}
return stream;
}
inline
std::ostream& on_red(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[41m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream, -1,
BACKGROUND_RED
);
#endif
}
return stream;
}
inline
std::ostream& on_green(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[42m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream, -1,
BACKGROUND_GREEN
);
#endif
}
return stream;
}
inline
std::ostream& on_yellow(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[43m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream, -1,
BACKGROUND_GREEN | BACKGROUND_RED
);
#endif
}
return stream;
}
inline
std::ostream& on_blue(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[44m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream, -1,
BACKGROUND_BLUE
);
#endif
}
return stream;
}
inline
std::ostream& on_magenta(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[45m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream, -1,
BACKGROUND_BLUE | BACKGROUND_RED
);
#endif
}
return stream;
}
inline
std::ostream& on_cyan(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[46m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream, -1,
BACKGROUND_GREEN | BACKGROUND_BLUE
);
#endif
}
return stream;
}
inline
std::ostream& on_white(std::ostream& stream)
{
if (_internal::is_colorized(stream))
{
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
stream << "\033[47m";
#elif defined(TERMCOLOR_OS_WINDOWS)
_internal::win_change_attributes(stream, -1,
BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED
);
#endif
}
return stream;
}
//! Since C++ hasn't a way to hide something in the header from
//! the outer access, I have to introduce this namespace which
//! is used for internal purpose and should't be access from
//! the user code.
namespace _internal
{
//! Since C++ hasn't a true way to extract stream handler
//! from the a given `std::ostream` object, I have to write
//! this kind of hack.
inline
FILE* get_standard_stream(const std::ostream& stream)
{
if (&stream == &std::cout)
return stdout;
else if ((&stream == &std::cerr) || (&stream == &std::clog))
return stderr;
return nullptr;
}
// Say whether a given stream should be colorized or not. It's always
// true for ATTY streams and may be true for streams marked with
// colorize flag.
inline
bool is_colorized(std::ostream& stream)
{
return is_atty(stream) || static_cast<bool>(stream.iword(colorize_index));
}
//! Test whether a given `std::ostream` object refers to
//! a terminal.
inline
bool is_atty(const std::ostream& stream)
{
FILE* std_stream = get_standard_stream(stream);
// Unfortunately, fileno() ends with segmentation fault
// if invalid file descriptor is passed. So we need to
// handle this case gracefully and assume it's not a tty
// if standard stream is not detected, and 0 is returned.
if (!std_stream)
return false;
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
return ::isatty(fileno(std_stream));
#elif defined(TERMCOLOR_OS_WINDOWS)
return ::_isatty(_fileno(std_stream));
#endif
}
#if defined(TERMCOLOR_OS_WINDOWS)
//! Change Windows Terminal colors attribute. If some
//! parameter is `-1` then attribute won't changed.
inline void win_change_attributes(std::ostream& stream, int foreground, int background)
{
// yeah, i know.. it's ugly, it's windows.
static WORD defaultAttributes = 0;
// Windows doesn't have ANSI escape sequences and so we use special
// API to change Terminal output color. That means we can't
// manipulate colors by means of "std::stringstream" and hence
// should do nothing in this case.
if (!_internal::is_atty(stream))
return;
// get terminal handle
HANDLE hTerminal = INVALID_HANDLE_VALUE;
if (&stream == &std::cout)
hTerminal = GetStdHandle(STD_OUTPUT_HANDLE);
else if (&stream == &std::cerr)
hTerminal = GetStdHandle(STD_ERROR_HANDLE);
// save default terminal attributes if it unsaved
if (!defaultAttributes)
{
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(hTerminal, &info))
return;
defaultAttributes = info.wAttributes;
}
// restore all default settings
if (foreground == -1 && background == -1)
{
SetConsoleTextAttribute(hTerminal, defaultAttributes);
return;
}
// get current settings
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(hTerminal, &info))
return;
if (foreground != -1)
{
info.wAttributes &= ~(info.wAttributes & 0x0F);
info.wAttributes |= static_cast<WORD>(foreground);
}
if (background != -1)
{
info.wAttributes &= ~(info.wAttributes & 0xF0);
info.wAttributes |= static_cast<WORD>(background);
}
SetConsoleTextAttribute(hTerminal, info.wAttributes);
}
#endif // TERMCOLOR_OS_WINDOWS
} // namespace _internal
} // namespace termcolor
#undef TERMCOLOR_OS_WINDOWS
#undef TERMCOLOR_OS_MACOS
#undef TERMCOLOR_OS_LINUX
#endif // TERMCOLOR_HPP_
namespace indicators {
enum class Color { grey, red, green, yellow, blue, magenta, cyan, white, unspecified };
@@ -688,9 +51,14 @@ enum class ProgressType { incremental, decremental };
#if defined(TERMCOLOR_OS_MACOS) || defined(TERMCOLOR_OS_LINUX)
# include <unistd.h>
#elif defined(TERMCOLOR_OS_WINDOWS)
#if defined(_MSC_VER)
#if !defined(NOMINMAX)
#define NOMINMAX
#endif
# include <io.h>
# include <windows.h>
#endif
#endif
#include <iostream>
@@ -1292,26 +660,32 @@ namespace termcolor
#pragma once
#include <utility>
namespace indicators {
#if defined(_MSC_VER)
#if defined(_WIN32)
#include <windows.h>
namespace indicators {
static inline std::pair<size_t, size_t> terminal_size() {
CONSOLE_SCREEN_BUFFER_INFO csbi;
int columns, rows;
int cols, rows;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
return {static_cast<size_t>(rows), static_cast<size_t>(cols)};
}
size_t terminal_width() { return terminal_size().second; }
static inline size_t terminal_width() { return terminal_size().second; }
} // namespace indicators
#else
#include <sys/ioctl.h> //ioctl() and TIOCGWINSZ
#include <unistd.h> // for STDOUT_FILENO
namespace indicators {
static inline std::pair<size_t, size_t> terminal_size() {
struct winsize size;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
@@ -1319,9 +693,12 @@ static inline std::pair<size_t, size_t> terminal_size() {
}
static inline size_t terminal_width() { return terminal_size().second; }
#endif
} // namespace indicators
#endif
/*
Activity Indicators for Modern C++
https://github.com/p-ranav/indicators
@@ -1627,7 +1004,10 @@ static inline void move_left(int cols) { std::cout << "\033[" << cols << "D"; }
// #include <indicators/display_width.hpp>
#include <clocale>
#if __has_include(<codecvt>)
#include <codecvt>
#define INDICATORS_HAVE_CODECVT 1
#endif
#include <cstdlib>
#include <locale>
#include <string>
@@ -1635,6 +1015,7 @@ static inline void move_left(int cols) { std::cout << "\033[" << cols << "D"; }
namespace unicode {
#if INDICATORS_HAVE_CODECVT
namespace details {
/*
@@ -1921,6 +1302,18 @@ static inline int display_width(const std::wstring &input) {
return details::mk_wcswidth(input.c_str(), input.size());
}
#else
static inline int display_width(const std::string &input) {
return input.length();
}
static inline int display_width(const std::wstring &input) {
return input.length();
}
#endif
} // namespace unicode
// #include <indicators/setting.hpp>
@@ -2359,7 +1752,7 @@ private:
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}
@@ -2395,9 +1788,10 @@ private:
if (saved_start_time) {
auto eta = std::chrono::nanoseconds(
progress_ > 0 ? static_cast<long long>(elapsed_.count() *
max_progress / progress_)
: 0);
progress_ > 0
? static_cast<long long>(std::ceil(float(elapsed_.count()) *
max_progress / progress_))
: 0);
auto remaining = eta > elapsed_ ? (eta - elapsed_) : (elapsed_ - eta);
details::write_duration(os, remaining);
} else {
@@ -2413,7 +1807,7 @@ private:
os << " " << get_value<details::ProgressBarOption::postfix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}
@@ -2663,7 +2057,7 @@ private:
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}
@@ -2696,7 +2090,10 @@ private:
if (saved_start_time) {
auto eta = std::chrono::nanoseconds(
progress_ > 0 ? static_cast<long long>(elapsed.count() * max_progress / progress_) : 0);
progress_ > 0
? static_cast<long long>(std::ceil(float(elapsed.count()) *
max_progress / progress_))
: 0);
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
details::write_duration(os, remaining);
} else {
@@ -2712,7 +2109,7 @@ private:
os << " " << get_value<details::ProgressBarOption::postfix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}
@@ -2943,7 +2340,7 @@ private:
std::stringstream os;
os << get_value<details::ProgressBarOption::prefix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}
@@ -2952,7 +2349,7 @@ private:
os << " " << get_value<details::ProgressBarOption::postfix_text>();
const auto result = os.str();
const auto result_size = result.size();
const auto result_size = unicode::display_width(result);
return {result, result_size};
}
@@ -3022,6 +2419,7 @@ public:
// #include <indicators/color.hpp>
// #include <indicators/cursor_movement.hpp>
// #include <indicators/details/stream_helper.hpp>
namespace indicators {
@@ -3096,6 +2494,7 @@ public:
#include <functional>
// #include <indicators/color.hpp>
// #include <indicators/setting.hpp>
// #include <indicators/details/stream_helper.hpp>
#include <iostream>
#include <mutex>
#include <vector>
@@ -3387,7 +2786,7 @@ public:
os << get_value<details::ProgressBarOption::spinner_states>()
[index_ % get_value<details::ProgressBarOption::spinner_states>().size()];
if (get_value<details::ProgressBarOption::show_percentage>()) {
os << " " << std::min(progress_, size_t(max_progress)) << "%";
os << " " << std::size_t(progress_ / double(max_progress) * 100) << "%";
}
if (get_value<details::ProgressBarOption::show_elapsed_time>()) {
@@ -3401,7 +2800,10 @@ public:
else
os << " [";
auto eta = std::chrono::nanoseconds(
progress_ > 0 ? static_cast<long long>(elapsed.count() * max_progress / progress_) : 0);
progress_ > 0
? static_cast<long long>(std::ceil(float(elapsed.count()) *
max_progress / progress_))
: 0);
auto remaining = eta > elapsed ? (eta - elapsed) : (elapsed - eta);
details::write_duration(os, remaining);
os << "]";