Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement styled arguments #2793

Merged
merged 11 commits into from
Mar 8, 2022
Merged

Implement styled arguments #2793

merged 11 commits into from
Mar 8, 2022

Conversation

rbrugo
Copy link
Contributor

@rbrugo rbrugo commented Mar 2, 2022

Hello,
this PR implements a way to apply different styles to different arguments in a single format (or print) call.
It is done by introducing a new function, fmt::styled, which takes an argument of arbitrary type and a color, an emphasis or a text_style, and creates a wrapper around the argument that will be formatted it in the requested style.

A simple example of usage:

void log(std::string_view caller, std::string_view message, bool error = false)
{
    fmt::print("[{}] {}: {}\n",
                fmt::styled(std::chrono::system_clock::now(), fmt::emphasis::bold),
                fmt::styled(caller, error ? fmt::color::red : fmt::color::green),
                message
    );
}

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Overall looks good but please rebase past the CI fix and address inline comments.

include/fmt/color.h Outdated Show resolved Hide resolved
include/fmt/color.h Outdated Show resolved Hide resolved
include/fmt/color.h Outdated Show resolved Hide resolved
@rbrugo
Copy link
Contributor Author

rbrugo commented Mar 7, 2022

Everything should be solved now!
Do you think something else needs to be changed?

include/fmt/color.h Outdated Show resolved Hide resolved
include/fmt/color.h Outdated Show resolved Hide resolved
include/fmt/color.h Outdated Show resolved Hide resolved
include/fmt/color.h Outdated Show resolved Hide resolved
include/fmt/color.h Outdated Show resolved Hide resolved
include/fmt/color.h Outdated Show resolved Hide resolved
include/fmt/color.h Outdated Show resolved Hide resolved
include/fmt/color.h Outdated Show resolved Hide resolved
@vitaut vitaut merged commit 8c9bc07 into fmtlib:master Mar 8, 2022
@vitaut
Copy link
Contributor

vitaut commented Mar 8, 2022

Thank you!

@vitaut
Copy link
Contributor

vitaut commented Mar 8, 2022

Could you also add it to the docs (https://github.com/fmtlib/fmt/blob/master/doc/api.rst)?

@rbrugo
Copy link
Contributor Author

rbrugo commented Mar 8, 2022

Sure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants