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
74927607
Commit
74927607
authored
Dec 09, 2018
by
Nicolas
Committed by
Victor Zverovich
Dec 09, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add file stream support for stylized text printing. (#967)
parent
f54f3d0f
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
14 deletions
+42
-14
include/fmt/color.h
include/fmt/color.h
+38
-14
test/format-impl-test.cc
test/format-impl-test.cc
+4
-0
No files found.
include/fmt/color.h
View file @
74927607
...
...
@@ -423,43 +423,67 @@ template <>
inline
void
reset_color
<
wchar_t
>
(
FILE
*
stream
)
FMT_NOEXCEPT
{
fputs
(
internal
::
data
::
WRESET_COLOR
,
stream
);
}
// The following specialiazation disables using std::FILE as a character type,
// which is needed because or else
// fmt::print(stderr, fmt::emphasis::bold, "");
// would take stderr (a std::FILE *) as the format string.
template
<
>
struct
is_string
<
std
::
FILE
*>
:
std
::
false_type
{};
template
<
>
struct
is_string
<
const
std
::
FILE
*>
:
std
::
false_type
{};
}
// namespace internal
template
<
typename
S
,
typename
Char
=
typename
internal
::
char_t
<
S
>
::
type
>
void
vprint
(
const
text_style
&
tf
,
const
S
&
format
,
void
vprint
(
std
::
FILE
*
f
,
const
text_style
&
ts
,
const
S
&
format
,
basic_format_args
<
typename
buffer_context
<
Char
>::
type
>
args
)
{
if
(
t
f
.
has_emphasis
())
{
if
(
t
s
.
has_emphasis
())
{
internal
::
fputs
<
Char
>
(
internal
::
make_emphasis
<
Char
>
(
t
f
.
get_emphasis
()),
stdout
);
internal
::
make_emphasis
<
Char
>
(
t
s
.
get_emphasis
()),
f
);
}
if
(
t
f
.
has_foreground
())
{
if
(
t
s
.
has_foreground
())
{
internal
::
fputs
<
Char
>
(
internal
::
make_foreground_color
<
Char
>
(
t
f
.
get_foreground
()),
stdout
);
internal
::
make_foreground_color
<
Char
>
(
t
s
.
get_foreground
()),
f
);
}
if
(
t
f
.
has_background
())
{
if
(
t
s
.
has_background
())
{
internal
::
fputs
<
Char
>
(
internal
::
make_background_color
<
Char
>
(
t
f
.
get_background
()),
stdout
);
internal
::
make_background_color
<
Char
>
(
t
s
.
get_background
()),
f
);
}
vprint
(
format
,
args
);
internal
::
reset_color
<
Char
>
(
stdout
);
vprint
(
f
,
f
ormat
,
args
);
internal
::
reset_color
<
Char
>
(
f
);
}
/**
Formats a string and prints it to
stdout using ANSI escape sequences to
specify text formatting.
Formats a string and prints it to
the specified file stream using ANSI
escape sequences to
specify text formatting.
Example:
fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
"Elapsed time: {0:.2f} seconds", 1.23);
*/
template
<
typename
String
,
typename
...
Args
>
typename
std
::
enable_if
<
internal
::
is_string
<
String
>::
value
>::
type
print
(
const
text_style
&
tf
,
const
String
&
format_str
,
const
Args
&
...
args
)
{
typename
std
::
enable_if
<
internal
::
is_string
<
String
>::
value
>::
type
print
(
std
::
FILE
*
f
,
const
text_style
&
ts
,
const
String
&
format_str
,
const
Args
&
...
args
)
{
internal
::
check_format_string
<
Args
...
>
(
format_str
);
typedef
typename
internal
::
char_t
<
String
>::
type
char_t
;
typedef
typename
buffer_context
<
char_t
>::
type
context_t
;
format_arg_store
<
context_t
,
Args
...
>
as
{
args
...};
vprint
(
tf
,
format_str
,
basic_format_args
<
context_t
>
(
as
));
vprint
(
f
,
ts
,
format_str
,
basic_format_args
<
context_t
>
(
as
));
}
/**
Formats a string and prints it to stdout using ANSI escape sequences to
specify text formatting.
Example:
fmt::print(fmt::emphasis::bold | fg(fmt::color::red),
"Elapsed time: {0:.2f} seconds", 1.23);
*/
template
<
typename
String
,
typename
...
Args
>
typename
std
::
enable_if
<
internal
::
is_string
<
String
>::
value
>::
type
print
(
const
text_style
&
ts
,
const
String
&
format_str
,
const
Args
&
...
args
)
{
return
print
(
stdout
,
ts
,
format_str
,
args
...);
}
#endif
...
...
test/format-impl-test.cc
View file @
74927607
...
...
@@ -228,4 +228,8 @@ TEST(ColorsTest, Colors) {
stdout
,
fmt
::
print
(
fg
(
fmt
::
color
::
blue
)
|
fmt
::
emphasis
::
bold
,
"blue/bold"
),
"
\x1b
[1m
\x1b
[38;2;000;000;255mblue/bold
\x1b
[0m"
);
EXPECT_WRITE
(
stderr
,
fmt
::
print
(
stderr
,
fmt
::
emphasis
::
bold
,
"bold error"
),
"
\x1b
[1mbold error
\x1b
[0m"
);
EXPECT_WRITE
(
stderr
,
fmt
::
print
(
stderr
,
fg
(
fmt
::
color
::
blue
),
"blue log"
),
"
\x1b
[38;2;000;000;255mblue log
\x1b
[0m"
);
}
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