Commit ce500635 authored by Remotion's avatar Remotion Committed by Victor Zverovich

Renamed enum color to colors.

Added enum colors conversion to rgb struct.
Added colors_test.cpp.

Removed print_colored.
Renamed enum colors back to color.

Removed unnecessary inline keyword.
Removed print_rgb.
parent 0508bbc7
This diff is collapsed.
This diff is collapsed.
...@@ -499,6 +499,7 @@ FMT_FUNC void vprint(wstring_view format_str, wformat_args args) { ...@@ -499,6 +499,7 @@ FMT_FUNC void vprint(wstring_view format_str, wformat_args args) {
vprint(stdout, format_str, args); vprint(stdout, format_str, args);
} }
#ifndef FMT_EXTENDED_COLORS
FMT_FUNC void vprint_colored(color c, string_view format, format_args args) { FMT_FUNC void vprint_colored(color c, string_view format, format_args args) {
char escape[] = "\x1b[30m"; char escape[] = "\x1b[30m";
escape[3] = static_cast<char>('0' + c); escape[3] = static_cast<char>('0' + c);
...@@ -514,6 +515,45 @@ FMT_FUNC void vprint_colored(color c, wstring_view format, wformat_args args) { ...@@ -514,6 +515,45 @@ FMT_FUNC void vprint_colored(color c, wstring_view format, wformat_args args) {
vprint(format, args); vprint(format, args);
std::fputws(WRESET_COLOR, stdout); std::fputws(WRESET_COLOR, stdout);
} }
#else
namespace internal {
FMT_CONSTEXPR void to_esc(uint8_t c, char out[], int offset) {
out[offset + 0] = static_cast<char>('0' + c / 100);
out[offset + 1] = static_cast<char>('0' + c / 10 % 10);
out[offset + 2] = static_cast<char>('0' + c % 10);
}
} // namespace internal
FMT_FUNC void vprint_rgb(rgb fd, string_view format, format_args args) {
char escape_fd[] = "\x1b[38;2;000;000;000m";
static FMT_CONSTEXPR_DECL const char RESET_COLOR[] = "\x1b[0m";
internal::to_esc(fd.r, escape_fd, 7);
internal::to_esc(fd.g, escape_fd, 11);
internal::to_esc(fd.b, escape_fd, 15);
std::fputs(escape_fd, stdout);
vprint(format, args);
std::fputs(RESET_COLOR, stdout);
}
FMT_FUNC void vprint_rgb(rgb fd, rgb bg, string_view format, format_args args) {
char escape_fd[] = "\x1b[38;2;000;000;000m"; // foreground color
char escape_bg[] = "\x1b[48;2;000;000;000m"; // background color
static FMT_CONSTEXPR_DECL const char RESET_COLOR[] = "\x1b[0m";
internal::to_esc(fd.r, escape_fd, 7);
internal::to_esc(fd.g, escape_fd, 11);
internal::to_esc(fd.b, escape_fd, 15);
internal::to_esc(bg.r, escape_bg, 7);
internal::to_esc(bg.g, escape_bg, 11);
internal::to_esc(bg.b, escape_bg, 15);
std::fputs(escape_fd, stdout);
std::fputs(escape_bg, stdout);
vprint(format, args);
std::fputs(RESET_COLOR, stdout);
}
#endif
FMT_FUNC locale locale_provider::locale() { return fmt::locale(); } FMT_FUNC locale locale_provider::locale() { return fmt::locale(); }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
// //
// For the license information refer to format.h. // For the license information refer to format.h.
#define FMT_EXTENDED_COLORS
#define FMT_NOEXCEPT #define FMT_NOEXCEPT
#undef FMT_SHARED #undef FMT_SHARED
#include "test-assert.h" #include "test-assert.h"
...@@ -116,3 +117,10 @@ TEST(FormatTest, FormatErrorCode) { ...@@ -116,3 +117,10 @@ TEST(FormatTest, FormatErrorCode) {
EXPECT_EQ(msg, to_string(buffer)); EXPECT_EQ(msg, to_string(buffer));
} }
} }
TEST(ColorsTest, Colors) {
EXPECT_WRITE(stdout, fmt::print(fmt::rgb(255,20,30), "rgb(255,20,30)"),
"\x1b[38;2;255;020;030mrgb(255,20,30)\x1b[0m");
EXPECT_WRITE(stdout, fmt::print(fmt::color::blue,"blue"),
"\x1b[38;2;000;000;255mblue\x1b[0m");
}
...@@ -1266,13 +1266,6 @@ TEST(FormatTest, Print) { ...@@ -1266,13 +1266,6 @@ TEST(FormatTest, Print) {
#endif #endif
} }
#if FMT_USE_FILE_DESCRIPTORS
TEST(FormatTest, PrintColored) {
EXPECT_WRITE(stdout, fmt::print_colored(fmt::red, "Hello, {}!\n", "world"),
"\x1b[31mHello, world!\n\x1b[0m");
}
#endif
TEST(FormatTest, Variadic) { TEST(FormatTest, Variadic) {
EXPECT_EQ("abc1", format("{}c{}", "ab", 1)); EXPECT_EQ("abc1", format("{}c{}", "ab", 1));
EXPECT_EQ(L"abc1", format(L"{}c{}", L"ab", 1)); EXPECT_EQ(L"abc1", format(L"{}c{}", L"ab", 1));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment