Revision 6248f75f
Added by david.sorber over 2 years ago
| external/indicators.hpp | ||
|---|---|---|
|
|
||
|
#ifndef INDICATORS_COLOR
|
||
|
#define INDICATORS_COLOR
|
||
|
|
||
|
namespace indicators {
|
||
|
enum class Color { grey, red, green, yellow, blue, magenta, cyan, white, unspecified };
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|
||
|
|
||
|
|
||
|
#ifndef INDICATORS_FONT_STYLE
|
||
|
#define INDICATORS_FONT_STYLE
|
||
|
|
||
|
namespace indicators {
|
||
|
enum class FontStyle { bold, dark, italic, underline, blink, reverse, concealed, crossed };
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|
||
|
|
||
|
|
||
|
#ifndef INDICATORS_PROGRESS_TYPE
|
||
|
#define INDICATORS_PROGRESS_TYPE
|
||
|
|
||
|
namespace indicators {
|
||
|
enum class ProgressType { incremental, decremental };
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|
||
|
//!
|
||
|
//! 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_
|
||
|
|
||
|
#include <iostream>
|
||
|
#include <cstdio>
|
||
|
|
||
|
// Detect target's platform and set some macros in order to wrap platform
|
||
|
// specific code this library depends on.
|
||
|
#if defined(_WIN32) || defined(_WIN64)
|
||
|
# define TERMCOLOR_TARGET_WINDOWS
|
||
|
#elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))
|
||
|
# define TERMCOLOR_TARGET_POSIX
|
||
|
#endif
|
||
|
|
||
|
// If implementation has not been explicitly set, try to choose one based on
|
||
|
// target platform.
|
||
|
#if !defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) && !defined(TERMCOLOR_USE_WINDOWS_API) && !defined(TERMCOLOR_USE_NOOP)
|
||
|
# if defined(TERMCOLOR_TARGET_POSIX)
|
||
|
# define TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES
|
||
|
# define TERMCOLOR_AUTODETECTED_IMPLEMENTATION
|
||
|
# elif defined(TERMCOLOR_TARGET_WINDOWS)
|
||
|
# define TERMCOLOR_USE_WINDOWS_API
|
||
|
# define TERMCOLOR_AUTODETECTED_IMPLEMENTATION
|
||
|
# endif
|
||
|
#endif
|
||
|
|
||
|
// These headers provide isatty()/fileno() functions, which are used for
|
||
|
// testing whether a standard stream refers to the terminal.
|
||
|
#if defined(TERMCOLOR_TARGET_POSIX)
|
||
|
# include <unistd.h>
|
||
|
#elif defined(TERMCOLOR_TARGET_WINDOWS)
|
||
|
#if defined(_MSC_VER)
|
||
|
#if !defined(NOMINMAX)
|
||
|
#define NOMINMAX
|
||
|
#endif
|
||
|
#endif
|
||
|
# include <io.h>
|
||
|
# include <windows.h>
|
||
|
#endif
|
||
|
|
||
|
|
||
|
namespace termcolor
|
||
|
{
|
||
|
// Forward declaration of the `_internal` namespace.
|
||
|
// All comments are below.
|
||
|
namespace _internal
|
||
|
{
|
||
|
inline int colorize_index();
|
||
|
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_TARGET_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[00m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[1m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& dark(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[2m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& italic(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[3m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& underline(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[4m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream, -1, COMMON_LVB_UNDERSCORE);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& blink(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[5m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& reverse(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[7m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& concealed(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[8m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& crossed(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[9m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
template <uint8_t code> inline
|
||
|
std::ostream& color(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
char command[12];
|
||
|
std::snprintf(command, sizeof(command), "\033[38;5;%dm", code);
|
||
|
stream << command;
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
template <uint8_t code> inline
|
||
|
std::ostream& on_color(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
char command[12];
|
||
|
std::snprintf(command, sizeof(command), "\033[48;5;%dm", code);
|
||
|
stream << command;
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
#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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
char command[20];
|
||
|
std::snprintf(command, sizeof(command), "\033[38;2;%d;%d;%dm", r, g, b);
|
||
|
stream << command;
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
#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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
char command[20];
|
||
|
std::snprintf(command, sizeof(command), "\033[48;2;%d;%d;%dm", r, g, b);
|
||
|
stream << command;
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& grey(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[30m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[31m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[32m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[33m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[34m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[35m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[36m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[37m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream,
|
||
|
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
|
||
|
inline
|
||
|
std::ostream& bright_grey(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[90m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream,
|
||
|
0 | FOREGROUND_INTENSITY // grey (black)
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& bright_red(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[91m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream,
|
||
|
FOREGROUND_RED | FOREGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& bright_green(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[92m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream,
|
||
|
FOREGROUND_GREEN | FOREGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& bright_yellow(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[93m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream,
|
||
|
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& bright_blue(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[94m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream,
|
||
|
FOREGROUND_BLUE | FOREGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& bright_magenta(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[95m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream,
|
||
|
FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& bright_cyan(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[96m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream,
|
||
|
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& bright_white(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[97m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream,
|
||
|
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
|
||
|
inline
|
||
|
std::ostream& on_grey(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[40m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[41m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[42m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[43m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[44m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[45m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[46m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_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_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[47m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream, -1,
|
||
|
BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
|
||
|
inline
|
||
|
std::ostream& on_bright_grey(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[100m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream, -1,
|
||
|
0 | BACKGROUND_INTENSITY // grey (black)
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& on_bright_red(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[101m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream, -1,
|
||
|
BACKGROUND_RED | BACKGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& on_bright_green(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[102m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream, -1,
|
||
|
BACKGROUND_GREEN | BACKGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& on_bright_yellow(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[103m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream, -1,
|
||
|
BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& on_bright_blue(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[104m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream, -1,
|
||
|
BACKGROUND_BLUE | BACKGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& on_bright_magenta(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[105m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream, -1,
|
||
|
BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& on_bright_cyan(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[106m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream, -1,
|
||
|
BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY
|
||
|
);
|
||
|
#endif
|
||
|
}
|
||
|
return stream;
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
std::ostream& on_bright_white(std::ostream& stream)
|
||
|
{
|
||
|
if (_internal::is_colorized(stream))
|
||
|
{
|
||
|
#if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES)
|
||
|
stream << "\033[107m";
|
||
|
#elif defined(TERMCOLOR_USE_WINDOWS_API)
|
||
|
_internal::win_change_attributes(stream, -1,
|
||
|
BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY
|
||
|
);
|
||
|
#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
|
||
|
{
|
||
|
// An index to be used to access a private storage of I/O streams. See
|
||
|
// colorize / nocolorize I/O manipulators for details. Due to the fact
|
||
|
// that static variables ain't shared between translation units, inline
|
||
|
// function with local static variable is used to do the trick and share
|
||
|
// the variable value between translation units.
|
||
|
inline int colorize_index()
|
||
|
{
|
||
|
static int colorize_index = std::ios_base::xalloc();
|
||
|
return colorize_index;
|
||
|
}
|
||
|
|
||
|
//! 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_TARGET_POSIX)
|
||
|
return ::isatty(fileno(std_stream));
|
||
|
#elif defined(TERMCOLOR_TARGET_WINDOWS)
|
||
|
return ::_isatty(_fileno(std_stream));
|
||
|
#else
|
||
|
return false;
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
#if defined(TERMCOLOR_TARGET_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_TARGET_WINDOWS
|
||
|
|
||
|
} // namespace _internal
|
||
|
|
||
|
} // namespace termcolor
|
||
|
|
||
|
|
||
|
#undef TERMCOLOR_TARGET_POSIX
|
||
|
#undef TERMCOLOR_TARGET_WINDOWS
|
||
|
|
||
|
#if defined(TERMCOLOR_AUTODETECTED_IMPLEMENTATION)
|
||
|
# undef TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES
|
||
|
# undef TERMCOLOR_USE_WINDOWS_API
|
||
|
#endif
|
||
|
|
||
|
#endif // TERMCOLOR_HPP_
|
||
|
|
||
|
|
||
|
|
||
|
#ifndef INDICATORS_TERMINAL_SIZE
|
||
|
#define INDICATORS_TERMINAL_SIZE
|
||
|
#include <utility>
|
||
|
|
||
|
|
||
|
#if defined(_WIN32)
|
||
|
#include <windows.h>
|
||
|
|
||
|
namespace indicators {
|
||
|
|
||
|
static inline std::pair<size_t, size_t> terminal_size() {
|
||
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||
|
int cols, rows;
|
||
|
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
|
||
|
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)};
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
return {static_cast<size_t>(size.ws_row), static_cast<size_t>(size.ws_col)};
|
||
|
}
|
||
|
|
||
|
static inline size_t terminal_width() { return terminal_size().second; }
|
||
|
|
||
|
} // namespace indicators
|
||
|
|
||
|
#endif
|
||
|
|
||
|
#endif
|
||
|
|
||
|
|
||
|
/*
|
||
|
Activity Indicators for Modern C++
|
||
|
https://github.com/p-ranav/indicators
|
||
|
|
||
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
||
|
SPDX-License-Identifier: MIT
|
||
|
Copyright (c) 2019 Dawid Pilarski <dawid.pilarski@panicsoftware.com>.
|
||
|
|
||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
of this software and associated documentation files (the "Software"), to deal
|
||
|
in the Software without restriction, including without limitation the rights
|
||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
copies of the Software, and to permit persons to whom the Software is
|
||
|
furnished to do so, subject to the following conditions:
|
||
|
|
||
|
The above copyright notice and this permission notice shall be included in all
|
||
|
copies or substantial portions of the Software.
|
||
|
|
||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||
|
SOFTWARE.
|
||
|
*/
|
||
|
#ifndef INDICATORS_SETTING
|
||
|
#define INDICATORS_SETTING
|
||
|
|
||
|
#include <cstddef>
|
||
|
// #include <indicators/color.hpp>
|
||
|
// #include <indicators/font_style.hpp>
|
||
|
// #include <indicators/progress_type.hpp>
|
||
|
#include <string>
|
||
|
#include <tuple>
|
||
|
#include <type_traits>
|
||
|
#include <utility>
|
||
|
#include <vector>
|
||
|
|
||
|
namespace indicators {
|
||
|
|
||
|
namespace details {
|
||
|
|
||
|
template <bool condition> struct if_else;
|
||
|
|
||
|
template <> struct if_else<true> { using type = std::true_type; };
|
||
|
|
||
|
template <> struct if_else<false> { using type = std::false_type; };
|
||
|
|
||
|
template <bool condition, typename True, typename False> struct if_else_type;
|
||
|
|
||
|
template <typename True, typename False> struct if_else_type<true, True, False> {
|
||
|
using type = True;
|
||
|
};
|
||
|
|
||
|
template <typename True, typename False> struct if_else_type<false, True, False> {
|
||
|
using type = False;
|
||
|
};
|
||
|
|
||
|
template <typename... Ops> struct conjuction;
|
||
|
|
||
|
template <> struct conjuction<> : std::true_type {};
|
||
|
|
||
|
template <typename Op, typename... TailOps>
|
||
|
struct conjuction<Op, TailOps...>
|
||
|
: if_else_type<!Op::value, std::false_type, conjuction<TailOps...>>::type {};
|
||
|
|
||
|
template <typename... Ops> struct disjunction;
|
||
|
|
||
|
template <> struct disjunction<> : std::false_type {};
|
||
|
|
||
|
template <typename Op, typename... TailOps>
|
||
|
struct disjunction<Op, TailOps...>
|
||
|
: if_else_type<Op::value, std::true_type, disjunction<TailOps...>>::type {};
|
||
|
|
||
|
enum class ProgressBarOption {
|
||
|
bar_width = 0,
|
||
|
prefix_text,
|
||
|
postfix_text,
|
||
|
start,
|
||
|
end,
|
||
|
fill,
|
||
|
lead,
|
||
|
remainder,
|
||
|
max_postfix_text_len,
|
||
|
completed,
|
||
|
show_percentage,
|
||
|
show_elapsed_time,
|
||
|
show_remaining_time,
|
||
|
saved_start_time,
|
||
|
foreground_color,
|
||
|
spinner_show,
|
||
|
spinner_states,
|
||
|
font_styles,
|
||
|
hide_bar_when_complete,
|
||
|
min_progress,
|
||
|
max_progress,
|
||
|
progress_type,
|
||
|
stream
|
||
|
};
|
||
|
|
||
|
template <typename T, ProgressBarOption Id> struct Setting {
|
||
|
template <typename... Args,
|
||
|
typename = typename std::enable_if<std::is_constructible<T, Args...>::value>::type>
|
||
|
explicit Setting(Args &&... args) : value(std::forward<Args>(args)...) {}
|
||
|
Setting(const Setting &) = default;
|
||
|
Setting(Setting &&) = default;
|
||
|
|
||
|
static constexpr auto id = Id;
|
||
|
using type = T;
|
||
|
|
||
|
T value{};
|
||
|
};
|
||
|
|
||
|
template <typename T> struct is_setting : std::false_type {};
|
||
|
|
||
|
template <ProgressBarOption Id, typename T> struct is_setting<Setting<T, Id>> : std::true_type {};
|
||
|
|
||
|
template <typename... Args>
|
||
|
struct are_settings : if_else<conjuction<is_setting<Args>...>::value>::type {};
|
||
|
|
||
|
template <> struct are_settings<> : std::true_type {};
|
||
|
|
||
|
template <typename Setting, typename Tuple> struct is_setting_from_tuple;
|
||
|
|
||
|
template <typename Setting> struct is_setting_from_tuple<Setting, std::tuple<>> : std::true_type {};
|
||
|
|
||
|
template <typename Setting, typename... TupleTypes>
|
||
|
struct is_setting_from_tuple<Setting, std::tuple<TupleTypes...>>
|
||
|
: if_else<disjunction<std::is_same<Setting, TupleTypes>...>::value>::type {};
|
||
|
|
||
|
template <typename Tuple, typename... Settings>
|
||
|
struct are_settings_from_tuple
|
||
|
: if_else<conjuction<is_setting_from_tuple<Settings, Tuple>...>::value>::type {};
|
||
|
|
||
|
template <ProgressBarOption Id> struct always_true { static constexpr auto value = true; };
|
||
|
|
||
|
template <ProgressBarOption Id, typename Default> Default &&get_impl(Default &&def) {
|
||
|
return std::forward<Default>(def);
|
||
|
}
|
||
|
|
||
|
template <ProgressBarOption Id, typename Default, typename T, typename... Args>
|
||
|
auto get_impl(Default && /*def*/, T &&first, Args &&... /*tail*/) ->
|
||
|
typename std::enable_if<(std::decay<T>::type::id == Id),
|
||
|
decltype(std::forward<T>(first))>::type {
|
||
|
return std::forward<T>(first);
|
||
|
}
|
||
|
|
||
|
template <ProgressBarOption Id, typename Default, typename T, typename... Args>
|
||
|
auto get_impl(Default &&def, T && /*first*/, Args &&... tail) ->
|
||
|
typename std::enable_if<(std::decay<T>::type::id != Id),
|
||
|
decltype(get_impl<Id>(std::forward<Default>(def),
|
||
|
std::forward<Args>(tail)...))>::type {
|
||
|
return get_impl<Id>(std::forward<Default>(def), std::forward<Args>(tail)...);
|
||
|
}
|
||
|
|
||
|
template <ProgressBarOption Id, typename Default, typename... Args,
|
||
|
typename = typename std::enable_if<are_settings<Args...>::value, void>::type>
|
||
|
auto get(Default &&def, Args &&... args)
|
||
|
-> decltype(details::get_impl<Id>(std::forward<Default>(def), std::forward<Args>(args)...)) {
|
||
|
return details::get_impl<Id>(std::forward<Default>(def), std::forward<Args>(args)...);
|
||
|
}
|
||
|
|
||
|
template <ProgressBarOption Id> using StringSetting = Setting<std::string, Id>;
|
||
|
|
||
|
template <ProgressBarOption Id> using IntegerSetting = Setting<std::size_t, Id>;
|
||
|
|
||
|
template <ProgressBarOption Id> using BooleanSetting = Setting<bool, Id>;
|
||
|
|
||
|
template <ProgressBarOption Id, typename Tuple, std::size_t counter = 0> struct option_idx;
|
||
|
|
||
|
template <ProgressBarOption Id, typename T, typename... Settings, std::size_t counter>
|
||
|
struct option_idx<Id, std::tuple<T, Settings...>, counter>
|
||
|
: if_else_type<(Id == T::id), std::integral_constant<std::size_t, counter>,
|
||
|
option_idx<Id, std::tuple<Settings...>, counter + 1>>::type {};
|
||
|
|
||
|
template <ProgressBarOption Id, std::size_t counter> struct option_idx<Id, std::tuple<>, counter> {
|
||
|
static_assert(always_true<(ProgressBarOption)Id>::value, "No such option was found");
|
||
|
};
|
||
|
|
||
|
template <ProgressBarOption Id, typename Settings>
|
||
|
auto get_value(Settings &&settings)
|
||
|
-> decltype((std::get<option_idx<Id, typename std::decay<Settings>::type>::value>(
|
||
|
std::declval<Settings &&>()))) {
|
||
|
return std::get<option_idx<Id, typename std::decay<Settings>::type>::value>(
|
||
|
std::forward<Settings>(settings));
|
||
|
}
|
||
|
|
||
|
} // namespace details
|
||
|
|
||
|
namespace option {
|
||
|
using BarWidth = details::IntegerSetting<details::ProgressBarOption::bar_width>;
|
||
|
using PrefixText = details::StringSetting<details::ProgressBarOption::prefix_text>;
|
||
|
using PostfixText = details::StringSetting<details::ProgressBarOption::postfix_text>;
|
||
|
using Start = details::StringSetting<details::ProgressBarOption::start>;
|
||
|
using End = details::StringSetting<details::ProgressBarOption::end>;
|
||
|
using Fill = details::StringSetting<details::ProgressBarOption::fill>;
|
||
|
using Lead = details::StringSetting<details::ProgressBarOption::lead>;
|
||
|
using Remainder = details::StringSetting<details::ProgressBarOption::remainder>;
|
||
|
using MaxPostfixTextLen = details::IntegerSetting<details::ProgressBarOption::max_postfix_text_len>;
|
||
|
using Completed = details::BooleanSetting<details::ProgressBarOption::completed>;
|
||
|
using ShowPercentage = details::BooleanSetting<details::ProgressBarOption::show_percentage>;
|
||
|
using ShowElapsedTime = details::BooleanSetting<details::ProgressBarOption::show_elapsed_time>;
|
||
|
using ShowRemainingTime = details::BooleanSetting<details::ProgressBarOption::show_remaining_time>;
|
||
|
using SavedStartTime = details::BooleanSetting<details::ProgressBarOption::saved_start_time>;
|
||
|
using ForegroundColor = details::Setting<Color, details::ProgressBarOption::foreground_color>;
|
||
|
using ShowSpinner = details::BooleanSetting<details::ProgressBarOption::spinner_show>;
|
||
|
using SpinnerStates =
|
||
|
details::Setting<std::vector<std::string>, details::ProgressBarOption::spinner_states>;
|
||
|
using HideBarWhenComplete =
|
||
|
details::BooleanSetting<details::ProgressBarOption::hide_bar_when_complete>;
|
||
|
using FontStyles =
|
||
|
details::Setting<std::vector<FontStyle>, details::ProgressBarOption::font_styles>;
|
||
|
using MinProgress = details::IntegerSetting<details::ProgressBarOption::min_progress>;
|
||
|
using MaxProgress = details::IntegerSetting<details::ProgressBarOption::max_progress>;
|
||
|
using ProgressType = details::Setting<ProgressType, details::ProgressBarOption::progress_type>;
|
||
|
using Stream = details::Setting<std::ostream &, details::ProgressBarOption::stream>;
|
||
|
} // namespace option
|
||
|
} // namespace indicators
|
||
|
|
||
|
#endif
|
||
|
|
||
|
|
||
|
#ifndef INDICATORS_CURSOR_CONTROL
|
||
|
#define INDICATORS_CURSOR_CONTROL
|
||
|
|
||
|
#if defined(_MSC_VER)
|
||
|
#if !defined(NOMINMAX)
|
||
|
#define NOMINMAX
|
||
|
#endif
|
||
|
#include <io.h>
|
||
|
#include <windows.h>
|
||
|
#else
|
||
|
#include <cstdio>
|
||
|
#endif
|
||
|
|
||
|
namespace indicators {
|
||
|
|
||
|
#if defined(_MSC_VER)
|
||
|
|
||
|
static inline void show_console_cursor(bool const show) {
|
||
|
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||
|
|
||
|
CONSOLE_CURSOR_INFO cursorInfo;
|
||
|
|
||
|
GetConsoleCursorInfo(out, &cursorInfo);
|
||
|
cursorInfo.bVisible = show; // set the cursor visibility
|
||
|
SetConsoleCursorInfo(out, &cursorInfo);
|
||
|
}
|
||
|
|
||
|
static inline void erase_line() {
|
||
|
auto hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
||
|
if (!hStdout)
|
||
|
return;
|
||
|
|
||
|
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
||
|
GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
|
||
|
|
||
|
COORD cursor;
|
||
|
|
||
|
cursor.X = 0;
|
||
|
cursor.Y = csbiInfo.dwCursorPosition.Y;
|
||
|
|
||
|
DWORD count = 0;
|
||
|
|
||
|
FillConsoleOutputCharacterA(hStdout, ' ', csbiInfo.dwSize.X, cursor, &count);
|
||
|
|
||
|
FillConsoleOutputAttribute(hStdout, csbiInfo.wAttributes, csbiInfo.dwSize.X,
|
||
|
cursor, &count);
|
||
|
|
||
|
SetConsoleCursorPosition(hStdout, cursor);
|
||
|
}
|
||
|
|
||
|
#else
|
||
|
|
||
|
static inline void show_console_cursor(bool const show) {
|
||
|
std::fputs(show ? "\033[?25h" : "\033[?25l", stdout);
|
||
|
}
|
||
|
|
||
|
static inline void erase_line() {
|
||
|
std::fputs("\r\033[K", stdout);
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|
||
|
} // namespace indicators
|
||
|
|
||
|
#endif
|
||
|
|
||
|
|
||
|
#ifndef INDICATORS_CURSOR_MOVEMENT
|
||
|
#define INDICATORS_CURSOR_MOVEMENT
|
||
|
|
||
|
#if defined(_MSC_VER)
|
||
|
#if !defined(NOMINMAX)
|
||
|
#define NOMINMAX
|
||
|
#endif
|
||
|
#include <io.h>
|
||
|
#include <windows.h>
|
||
|
#else
|
||
|
#include <iostream>
|
||
|
#endif
|
||
|
|
||
|
namespace indicators {
|
||
|
|
||
|
#ifdef _MSC_VER
|
||
|
|
||
|
static inline void move(int x, int y) {
|
||
|
auto hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
||
|
if (!hStdout)
|
||
|
return;
|
||
|
|
||
|
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
|
||
|
GetConsoleScreenBufferInfo(hStdout, &csbiInfo);
|
||
|
|
||
|
COORD cursor;
|
||
|
|
||
|
cursor.X = csbiInfo.dwCursorPosition.X + x;
|
||
|
cursor.Y = csbiInfo.dwCursorPosition.Y + y;
|
||
|
SetConsoleCursorPosition(hStdout, cursor);
|
||
|
}
|
||
|
|
||
|
static inline void move_up(int lines) { move(0, -lines); }
|
||
|
static inline void move_down(int lines) { move(0, -lines); }
|
||
|
static inline void move_right(int cols) { move(cols, 0); }
|
||
|
static inline void move_left(int cols) { move(-cols, 0); }
|
||
|
|
||
|
#else
|
||
|
|
||
|
static inline void move_up(int lines) { std::cout << "\033[" << lines << "A"; }
|
||
|
static inline void move_down(int lines) { std::cout << "\033[" << lines << "B"; }
|
||
|
static inline void move_right(int cols) { std::cout << "\033[" << cols << "C"; }
|
||
|
static inline void move_left(int cols) { std::cout << "\033[" << cols << "D"; }
|
||
|
|
||
|
#endif
|
||
|
|
||
|
} // namespace indicators
|
||
|
|
||
|
#endif
|
||
|
|
||
|
|
||
|
#ifndef INDICATORS_STREAM_HELPER
|
||
|
#define INDICATORS_STREAM_HELPER
|
||
|
|
||
|
// #include <indicators/display_width.hpp>
|
||
|
#ifndef INDICATORS_DISPLAY_WIDTH
|
||
|
#define INDICATORS_DISPLAY_WIDTH
|
||
|
|
||
|
#include <clocale>
|
||
|
#include <cstdlib>
|
||
|
#include <locale>
|
||
|
#include <string>
|
||
|
#include <wchar.h>
|
||
|
|
||
|
namespace unicode {
|
||
|
|
||
|
namespace details {
|
||
|
|
||
|
/*
|
||
|
* This is an implementation of wcwidth() and wcswidth() (defined in
|
||
|
* IEEE Std 1002.1-2001) for Unicode.
|
||
|
*
|
||
|
* http://www.opengroup.org/onlinepubs/007904975/functions/wcwidth.html
|
||
|
* http://www.opengroup.org/onlinepubs/007904975/functions/wcswidth.html
|
||
|
*
|
||
|
* In fixed-width output devices, Latin characters all occupy a single
|
||
|
* "cell" position of equal width, whereas ideographic CJK characters
|
||
|
* occupy two such cells. Interoperability between terminal-line
|
||
|
* applications and (teletype-style) character terminals using the
|
||
|
* UTF-8 encoding requires agreement on which character should advance
|
||
|
* the cursor by how many cell positions. No established formal
|
||
|
* standards exist at present on which Unicode character shall occupy
|
||
|
* how many cell positions on character terminals. These routines are
|
||
|
* a first attempt of defining such behavior based on simple rules
|
||
|
* applied to data provided by the Unicode Consortium.
|
||
|
*
|
||
|
* For some graphical characters, the Unicode standard explicitly
|
||
|
* defines a character-cell width via the definition of the East Asian
|
||
|
* FullWidth (F), Wide (W), Half-width (H), and Narrow (Na) classes.
|
||
|
* In all these cases, there is no ambiguity about which width a
|
||
|
* terminal shall use. For characters in the East Asian Ambiguous (A)
|
||
|
* class, the width choice depends purely on a preference of backward
|
||
|
* compatibility with either historic CJK or Western practice.
|
||
|
* Choosing single-width for these characters is easy to justify as
|
||
|
* the appropriate long-term solution, as the CJK practice of
|
||
|
* displaying these characters as double-width comes from historic
|
||
|
* implementation simplicity (8-bit encoded characters were displayed
|
||
|
* single-width and 16-bit ones double-width, even for Greek,
|
||
|
* Cyrillic, etc.) and not any typographic considerations.
|
||
|
*
|
||
|
* Much less clear is the choice of width for the Not East Asian
|
||
|
* (Neutral) class. Existing practice does not dictate a width for any
|
||
|
* of these characters. It would nevertheless make sense
|
||
|
* typographically to allocate two character cells to characters such
|
||
|
* as for instance EM SPACE or VOLUME INTEGRAL, which cannot be
|
||
|
* represented adequately with a single-width glyph. The following
|
||
|
* routines at present merely assign a single-cell width to all
|
||
|
* neutral characters, in the interest of simplicity. This is not
|
||
|
* entirely satisfactory and should be reconsidered before
|
||
|
* establishing a formal standard in this area. At the moment, the
|
||
|
* decision which Not East Asian (Neutral) characters should be
|
||
|
* represented by double-width glyphs cannot yet be answered by
|
||
|
* applying a simple rule from the Unicode database content. Setting
|
||
|
* up a proper standard for the behavior of UTF-8 character terminals
|
||
|
* will require a careful analysis not only of each Unicode character,
|
||
|
* but also of each presentation form, something the author of these
|
||
|
* routines has avoided to do so far.
|
||
|
*
|
||
|
* http://www.unicode.org/unicode/reports/tr11/
|
||
|
*
|
||
|
* Markus Kuhn -- 2007-05-26 (Unicode 5.0)
|
||
|
*
|
||
|
* Permission to use, copy, modify, and distribute this software
|
||
|
* for any purpose and without fee is hereby granted. The author
|
||
|
* disclaims all warranties with regard to this software.
|
||
|
*
|
||
|
* Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
|
||
|
*/
|
||
|
|
||
|
struct interval {
|
||
|
int first;
|
||
|
int last;
|
||
|
};
|
||
|
|
||
|
/* auxiliary function for binary search in interval table */
|
||
|
static inline int bisearch(wchar_t ucs, const struct interval *table, int max) {
|
||
|
int min = 0;
|
||
|
int mid;
|
||
|
|
||
|
if (ucs < table[0].first || ucs > table[max].last)
|
||
|
return 0;
|
||
|
while (max >= min) {
|
||
|
mid = (min + max) / 2;
|
||
|
if (ucs > table[mid].last)
|
||
|
min = mid + 1;
|
||
|
else if (ucs < table[mid].first)
|
||
|
max = mid - 1;
|
||
|
else
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
/* The following two functions define the column width of an ISO 10646
|
||
|
* character as follows:
|
||
|
*
|
||
|
* - The null character (U+0000) has a column width of 0.
|
||
|
*
|
||
|
* - Other C0/C1 control characters and DEL will lead to a return
|
||
|
* value of -1.
|
||
|
*
|
||
|
* - Non-spacing and enclosing combining characters (general
|
||
|
* category code Mn or Me in the Unicode database) have a
|
||
|
* column width of 0.
|
||
|
*
|
||
|
* - SOFT HYPHEN (U+00AD) has a column width of 1.
|
||
|
*
|
||
|
* - Other format characters (general category code Cf in the Unicode
|
||
|
* database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
|
||
|
*
|
||
|
* - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
|
||
|
* have a column width of 0.
|
||
|
*
|
||
|
* - Spacing characters in the East Asian Wide (W) or East Asian
|
||
|
* Full-width (F) category as defined in Unicode Technical
|
||
|
* Report #11 have a column width of 2.
|
||
|
*
|
||
|
* - All remaining characters (including all printable
|
||
|
* ISO 8859-1 and WGL4 characters, Unicode control characters,
|
||
|
* etc.) have a column width of 1.
|
||
|
*
|
||
|
* This implementation assumes that wchar_t characters are encoded
|
||
|
* in ISO 10646.
|
||
|
*/
|
||
|
|
||
|
static inline int mk_wcwidth(wchar_t ucs) {
|
||
|
/* sorted list of non-overlapping intervals of non-spacing characters */
|
||
|
/* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */
|
||
|
static const struct interval combining[] = {
|
||
|
{0x0300, 0x036F}, {0x0483, 0x0486}, {0x0488, 0x0489},
|
||
|
{0x0591, 0x05BD}, {0x05BF, 0x05BF}, {0x05C1, 0x05C2},
|
||
|
{0x05C4, 0x05C5}, {0x05C7, 0x05C7}, {0x0600, 0x0603},
|
||
|
{0x0610, 0x0615}, {0x064B, 0x065E}, {0x0670, 0x0670},
|
||
|
{0x06D6, 0x06E4}, {0x06E7, 0x06E8}, {0x06EA, 0x06ED},
|
||
|
{0x070F, 0x070F}, {0x0711, 0x0711}, {0x0730, 0x074A},
|
||
|
{0x07A6, 0x07B0}, {0x07EB, 0x07F3}, {0x0901, 0x0902},
|
||
|
{0x093C, 0x093C}, {0x0941, 0x0948}, {0x094D, 0x094D},
|
||
|
{0x0951, 0x0954}, {0x0962, 0x0963}, {0x0981, 0x0981},
|
||
|
{0x09BC, 0x09BC}, {0x09C1, 0x09C4}, {0x09CD, 0x09CD},
|
||
|
{0x09E2, 0x09E3}, {0x0A01, 0x0A02}, {0x0A3C, 0x0A3C},
|
||
|
{0x0A41, 0x0A42}, {0x0A47, 0x0A48}, {0x0A4B, 0x0A4D},
|
||
|
{0x0A70, 0x0A71}, {0x0A81, 0x0A82}, {0x0ABC, 0x0ABC},
|
||
Add progress bar; version: 0.2.0.