Examples of fmt-display-cpp Usage
Explore practical examples demonstrating various features of fmt-display-cpp.
Simple Output Example
This example demonstrates basic output, styled output, and container output:
#include "fmt/display.h"
#include "fmt/vector.h"
#include "fmt/map.h"
#include <vector>
#include <map>
int main() {
// Basic output
fmt::println("Hello, World!");
// Styled output
fmt::println(ansi::green, "Success:", ansi::reset, " Operation completed.");
// Container output
std::vector<int> numbers = {1, 2, 3, 4, 5};
fmt::println("Numbers: ", numbers);
// Complex data structure
std::map<std::string, std::vector<int>> data = {
{"Alice", {90, 85, 92}},
{"Bob", {78, 88, 76}},
{"Charlie", {95, 91, 89}}
};
fmt::println("Student Scores: ", data);
return 0;
}
This example shows how to use fmt::println
for various types of output, including styled text and complex data structures.
Custom Type Example
Learn how to create and use custom type specializations:
#include "fmt/display.h"
#include "fmt/vector.h"
#include <vector>
struct Point {
int x, y;
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 p1(10, 20);
Point p2(-5, 15);
fmt::println("Point 1: ", p1);
fmt::println("Point 2: ", p2);
std::vector<Point> points = {p1, p2, Point(0, 0)};
fmt::println("All points: ", points);
return 0;
}
This example demonstrates how to specialize fmt::Display
for a custom type and use it with both individual instances and containers.
Styled Output Example
Explore the ANSI styling capabilities of fmt-display-cpp:
#include "fmt/display.h"
int main() {
fmt::println(ansi::bold, "Bold Text");
fmt::println(ansi::red, "Red Text");
fmt::println(ansi::green, "Green Text");
fmt::println(ansi::blue, "Blue Text");
fmt::fmtout out;
out << "This is " << ansi::bold << "bold" << ansi::reset
<< " and this is " << ansi::red << "red" << ansi::reset
<< " and this is " << ansi::blue << ansi::italic << "blue italic" << ansi::reset;
fmt::println(out);
return 0;
}
This example shows how to use ANSI styling constants and the fmt::fmtout
class to create rich, colorful console output.
Error Handling Example
Implement custom error handling and formatting:
#include "fmt/display.h"
#include <stdexcept>
class CustomError : public std::runtime_error {
public:
CustomError(const std::string& message) : std::runtime_error(message) {}
};
template<>
struct fmt::Display<CustomError> {
static std::string print(const CustomError& e) {
return fmt::print.sprint(ansi::red, "CustomError: ", e.what(), ansi::reset);
}
};
void may_throw() {
throw CustomError("Something went wrong");
}
int main() {
try {
may_throw();
} catch (const CustomError& e) {
fmt::println(ansi::green, "Error caught: ", ansi::reset, e);
}
return 0;
}
This example demonstrates how to create a custom error type, specialize fmt::Display
for it, and use it in a try-catch block with styled error output.
Further Examples
For more advanced usage and examples, check out these topics: