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
6968ef3b
Commit
6968ef3b
authored
Feb 19, 2014
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement PrintColored.
Thanks
https://github.com/jose55
for the idea.
parent
9bb93992
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
0 deletions
+36
-0
format-test.cc
format-test.cc
+5
-0
format.cc
format.cc
+2
-0
format.h
format.h
+29
-0
No files found.
format-test.cc
View file @
6968ef3b
...
...
@@ -1462,6 +1462,11 @@ TEST(FormatIntTest, FormatDec) {
EXPECT_EQ
(
"42"
,
FormatDec
(
42ull
));
}
TEST
(
ColorTest
,
PrintColored
)
{
fmt
::
PrintColored
(
fmt
::
RED
,
"Hello, {}!
\n
"
)
<<
"world"
;
// TODO
}
template
<
typename
T
>
std
::
string
str
(
const
T
&
value
)
{
return
fmt
::
str
(
fmt
::
Format
(
"{0}"
)
<<
value
);
...
...
format.cc
View file @
6968ef3b
...
...
@@ -663,6 +663,8 @@ void fmt::BasicFormatter<Char>::DoFormat() {
writer
.
buffer_
.
append
(
start
,
s
);
}
const
char
fmt
::
ColorWriter
::
RESET
[]
=
"
\x1b
[0m"
;
// Explicit instantiations for char.
template
void
fmt
::
BasicWriter
<
char
>
::
FormatDouble
<
double
>
(
...
...
format.h
View file @
6968ef3b
...
...
@@ -1465,6 +1465,35 @@ inline Formatter<Write> Print(StringRef format) {
Formatter
<
Write
>
f
(
format
);
return
f
;
}
enum
Color
{
BLACK
,
RED
,
GREEN
,
YELLOW
,
BLUE
,
MAGENTA
,
CYAN
,
WHITE
};
/** A formatting action that writes colored output to stdout. */
class
ColorWriter
{
private:
Color
color_
;
static
const
char
RESET
[];
public:
explicit
ColorWriter
(
Color
c
)
:
color_
(
c
)
{}
/** Writes the colored output to stdout. */
void
operator
()(
const
BasicWriter
<
char
>
&
w
)
const
{
char
escape
[]
=
"
\x1b
[30m"
;
escape
[
3
]
=
'0'
+
color_
;
std
::
fputs
(
escape
,
stdout
);
std
::
fwrite
(
w
.
data
(),
1
,
w
.
size
(),
stdout
);
std
::
fputs
(
RESET
,
stdout
);
}
};
// Formats a string and prints it to stdout with the given color.
// Example:
// PrintColored(fmt::RED, "Elapsed time: {0:.2f} seconds") << 1.23;
inline
Formatter
<
ColorWriter
>
PrintColored
(
Color
c
,
StringRef
format
)
{
Formatter
<
ColorWriter
>
f
(
format
,
ColorWriter
(
c
));
return
f
;
}
}
#if _MSC_VER
...
...
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