Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
5a9dc8f9
Commit
5a9dc8f9
authored
Feb 08, 2015
by
Ryuuke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
windows cli colors revision and test case
parent
bce73e2b
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
47 additions
and
29 deletions
+47
-29
format.cc
format.cc
+38
-4
format.h
format.h
+1
-24
test/format-test.cc
test/format-test.cc
+8
-1
No files found.
format.cc
View file @
5a9dc8f9
...
@@ -35,6 +35,15 @@
...
@@ -35,6 +35,15 @@
#include <cmath>
#include <cmath>
#include <cstdarg>
#include <cstdarg>
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# ifdef __MINGW32__
# include <cstring>
# endif
# include <windows.h>
# undef ERROR
#endif
using
fmt
::
internal
::
Arg
;
using
fmt
::
internal
::
Arg
;
// Check if exceptions are disabled.
// Check if exceptions are disabled.
...
@@ -113,7 +122,19 @@ struct IntChecker<true> {
...
@@ -113,7 +122,19 @@ struct IntChecker<true> {
};
};
#ifdef _WIN32
#ifdef _WIN32
const
char
RESET_COLOR
=
FOREGROUND_GREEN
|
FOREGROUND_RED
|
FOREGROUND_BLUE
;
uint8_t
win32_colors
[]
=
{
0
,
FOREGROUND_RED
|
FOREGROUND_INTENSITY
,
FOREGROUND_GREEN
|
FOREGROUND_INTENSITY
,
FOREGROUND_GREEN
|
FOREGROUND_RED
|
FOREGROUND_INTENSITY
,
FOREGROUND_BLUE
|
FOREGROUND_INTENSITY
,
FOREGROUND_BLUE
|
FOREGROUND_RED
|
FOREGROUND_INTENSITY
,
FOREGROUND_INTENSITY
|
FOREGROUND_GREEN
|
FOREGROUND_BLUE
,
FOREGROUND_GREEN
|
FOREGROUND_RED
|
FOREGROUND_BLUE
|
FOREGROUND_INTENSITY
};
WORD
RESET_COLOR
=
0
;
bool
reset_color_flag
=
false
;
#else
#else
const
char
RESET_COLOR
[]
=
"
\x1b
[0m"
;
const
char
RESET_COLOR
[]
=
"
\x1b
[0m"
;
#endif
#endif
...
@@ -1096,7 +1117,19 @@ FMT_FUNC void fmt::print(std::ostream &os, StringRef format_str, ArgList args) {
...
@@ -1096,7 +1117,19 @@ FMT_FUNC void fmt::print(std::ostream &os, StringRef format_str, ArgList args) {
FMT_FUNC
void
fmt
::
print_colored
(
Color
c
,
StringRef
format
,
ArgList
args
)
{
FMT_FUNC
void
fmt
::
print_colored
(
Color
c
,
StringRef
format
,
ArgList
args
)
{
#ifdef _WIN32
#ifdef _WIN32
SetConsoleTextAttribute
(
GetStdHandle
(
STD_OUTPUT_HANDLE
),
c
);
HANDLE
handle
=
GetStdHandle
(
STD_OUTPUT_HANDLE
);
if
(
handle
==
INVALID_HANDLE_VALUE
)
FMT_THROW
(
GetLastError
(),
"cannot get output handle"
);
if
(
!
reset_color_flag
)
{
CONSOLE_SCREEN_BUFFER_INFO
infoCon
;
if
(
!
GetConsoleScreenBufferInfo
(
handle
,
&
infoCon
))
FMT_THROW
(
GetLastError
(),
"cannot get console informations"
);
RESET_COLOR
=
infoCon
.
wAttributes
;
reset_color_flag
=
true
;
}
WORD
color
=
static_cast
<
int
>
(
c
)
>=
ARRAYSIZE
(
win32_colors
)
?
RESET_COLOR
:
win32_colors
[
c
];
if
(
!
SetConsoleTextAttribute
(
handle
,
color
))
FMT_THROW
(
GetLastError
(),
"cannot set console color"
);
#else
#else
char
escape
[]
=
"
\x1b
[30m"
;
char
escape
[]
=
"
\x1b
[30m"
;
escape
[
3
]
=
'0'
+
static_cast
<
char
>
(
c
);
escape
[
3
]
=
'0'
+
static_cast
<
char
>
(
c
);
...
@@ -1104,7 +1137,8 @@ FMT_FUNC void fmt::print_colored(Color c, StringRef format, ArgList args) {
...
@@ -1104,7 +1137,8 @@ FMT_FUNC void fmt::print_colored(Color c, StringRef format, ArgList args) {
#endif
#endif
print
(
format
,
args
);
print
(
format
,
args
);
#ifdef _WIN32
#ifdef _WIN32
SetConsoleTextAttribute
(
GetStdHandle
(
STD_OUTPUT_HANDLE
),
RESET_COLOR
);
if
(
!
SetConsoleTextAttribute
(
handle
,
RESET_COLOR
))
FMT_THROW
(
GetLastError
(),
"cannot set console color"
);
#else
#else
std
::
fputs
(
RESET_COLOR
,
stdout
);
std
::
fputs
(
RESET_COLOR
,
stdout
);
#endif
#endif
...
...
format.h
View file @
5a9dc8f9
...
@@ -40,15 +40,6 @@
...
@@ -40,15 +40,6 @@
#include <string>
#include <string>
#include <sstream>
#include <sstream>
#ifdef _WIN32
# define WIN32_LEAN_AND_MEAN
# ifdef __MINGW32__
# include <cstring>
# endif
# include <windows.h>
# undef ERROR
#endif
#if _SECURE_SCL
#if _SECURE_SCL
# include <iterator>
# include <iterator>
#endif
#endif
...
@@ -2163,21 +2154,7 @@ void report_windows_error(int error_code, StringRef message) FMT_NOEXCEPT(true);
...
@@ -2163,21 +2154,7 @@ void report_windows_error(int error_code, StringRef message) FMT_NOEXCEPT(true);
#endif
#endif
#ifdef _WIN32
enum
Color
{
BLACK
,
RED
,
GREEN
,
YELLOW
,
BLUE
,
MAGENTA
,
CYAN
,
WHITE
};
enum
Color
{
BLACK
=
0
,
RED
=
FOREGROUND_RED
|
FOREGROUND_INTENSITY
,
GREEN
=
FOREGROUND_GREEN
|
FOREGROUND_INTENSITY
,
YELLOW
=
FOREGROUND_GREEN
|
FOREGROUND_RED
|
FOREGROUND_INTENSITY
,
BLUE
=
FOREGROUND_BLUE
|
FOREGROUND_INTENSITY
,
MAGENTA
=
FOREGROUND_BLUE
|
FOREGROUND_RED
|
FOREGROUND_INTENSITY
,
CYAN
=
FOREGROUND_INTENSITY
|
FOREGROUND_GREEN
|
FOREGROUND_BLUE
,
WHITE
=
FOREGROUND_GREEN
|
FOREGROUND_RED
|
FOREGROUND_BLUE
|
FOREGROUND_INTENSITY
};
#else
enum
Color
{
BLACK
,
RED
,
GREEN
,
YELLOW
,
BLUE
,
MAGENTA
,
CYAN
,
WHITE
};
#endif
/**
/**
Formats a string and prints it to stdout using ANSI escape sequences
Formats a string and prints it to stdout using ANSI escape sequences
...
...
test/format-test.cc
View file @
5a9dc8f9
...
@@ -1371,8 +1371,15 @@ TEST(FormatTest, Print) {
...
@@ -1371,8 +1371,15 @@ TEST(FormatTest, Print) {
#if FMT_USE_FILE_DESCRIPTORS
#if FMT_USE_FILE_DESCRIPTORS
TEST
(
FormatTest
,
PrintColored
)
{
TEST
(
FormatTest
,
PrintColored
)
{
#if _WIN32
EXPECT_WRITE
(
stdout
,
fmt
::
print_colored
(
fmt
::
RED
,
"Hello, {}!
\n
"
,
"world"
),
"Hello, world!
\n
"
);
EXPECT_WRITE
(
stdout
,
fmt
::
print_colored
(
static_cast
<
fmt
::
Color
>
(
29673
),
"Hello, {}!
\n
"
,
"world"
),
"Hello, world!
\n
"
);
#else
EXPECT_WRITE
(
stdout
,
fmt
::
print_colored
(
fmt
::
RED
,
"Hello, {}!
\n
"
,
"world"
),
EXPECT_WRITE
(
stdout
,
fmt
::
print_colored
(
fmt
::
RED
,
"Hello, {}!
\n
"
,
"world"
),
"
\x1b
[31mHello, world!
\n\x1b
[0m"
);
"
\x1b
[31mHello, world!
\n\x1b
[0m"
);
#endif
}
}
#endif
#endif
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment