Core Features of fmt-display-cpp
Explore the powerful features that make fmt-display-cpp a versatile and user-friendly formatting library.
ANSI Styling
fmt-display-cpp provides built-in support for ANSI styling, allowing you to add colors and text formatting to your console output easily.
#include "fmt/display.h"
int main() {
fmt::println(ansi::red, "Error: ", ansi::reset, "Something went wrong.");
fmt::println(ansi::green, "Success: ", ansi::bold, "Operation completed.", ansi::reset);
fmt::println(ansi::blue, "Info: ", ansi::italic, "This is some information.", ansi::reset);
return 0;
}
Available ANSI styles include colors (e.g., ansi::red
, ansi::green
), text styles (e.g., ansi::bold
, ansi::italic
), and ansi::reset
to return to default styling.
Custom Display Trait
Customize how your types are displayed by specializing the fmt::Display
trait. This allows for seamless integration of custom types with fmt-display-cpp's formatting system.
#include "fmt/display.h"
#include "fmt/vector.h"
struct Point {
int x, y;
// Constructor for convenience
Point(int x, int y) : x(x), y(y) {}
};
template<>
struct fmt::Display<Point> {
static std::string print(const Point& p) {
return fmt::format_string("(%d, %d)", p.x, p.y);
}
};
int main() {
Point p{10, 20};
fmt::println("Point: ", p); // Output: Point: (10, 20)
std::vector<Point> points = {{1, 2}, {3, 4}, {5, 6}};
fmt::println("Points: ", points); // Output: Points: [(1, 2), (3, 4), (5, 6)]
return 0;
}
By specializing fmt::Display
for your custom types, you can control how they are formatted in all fmt-display-cpp operations, including when they're part of containers.
Safe String Formatting
fmt-display-cpp provides type-safe string formatting with fmt::format_string
, helping to catch formatting errors at compile-time.
#include "fmt/display.h"
int main() {
std::string name = "Alice";
int age = 30;
float height = 1.75f;
// Type-safe formatting
std::string message = fmt::format_string("%s is %d years old and %.2f meters tall.",
name.c_str(), age, height);
fmt::println(message);
// TODO: This would cause a compile-time error:
// fmt::format_string("%d", "not a number");
return 0;
}
The fmt::format_string
function ensures that the format string matches the types of the provided arguments, preventing runtime formatting errors.
fmtout Class
The fmt::fmtout
class allows for complex formatting operations with a stream-like interface, making it easy to build formatted strings.
#include "fmt/display.h"
int main() {
fmt::fmtout out;
out << "The answer is " << ansi::bold << 42 << ansi::reset << "!";
fmt::println(out); // Output: The answer is 42! (with 42 in bold)
out.clear();
out << ansi::red << "Error: " << ansi::reset << "File not found.";
fmt::println(out); // Output: Error: File not found. (with "Error:" in red)
return 0;
}
fmt::fmtout
is particularly useful when you need to build complex formatted strings with multiple components or styling changes.
PrintFormatter Utility
The fmt::PrintFormatter
utility allows for customized printing with user-defined separators and end-of-line characters.
#include "fmt/display.h"
int main() {
fmt::PrintFormatter custom_println(", ", "\n");
custom_println(1, 2, 3); // Output: 1, 2, 3\n
fmt::PrintFormatter custom_print(":", ";");
custom_print("name", "Alice", "age", 30); // Output: name:Alice:age:30;
return 0;
}
PrintFormatter
is highly customizable, allowing you to create specialized formatting for various output needs, such as CSV generation or simple serialization.
Global Print Instances
fmt-display-cpp provides global print instances for convenience:
fmt::print
: Prints without a newlinefmt::println
: Prints with a newline
These global instances make it easy to quickly output formatted text without needing to create custom PrintFormatter
instances for common use cases.