fmt::display
is a header-only C++ library that provides a flexible and extensible way to format and display various data types. Additionally, the library now includes a simple testing framework (test::assert.h
) that allows for easy creation and execution of unit tests within your C++ project.
- Custom display implementations for user-defined types.
- ANSI color support for terminal output.
- Built-in support for standard containers like
vector
,map
, andset
. - Support for C++17 features like
std::optional
andstd::variant
. - Simple testing framework for creating and running unit tests.
- Compile-time type checking for printable types.
- String-building capabilities with the
fmtout
class. - Variadic template functions for flexible printing.
- C++11 compatible compiler.
- Header-only library (no compilation needed).
- Clone this repository or download the
fmt.display.h
andtest.assert.h
files. - Include the headers in your C++ project:
#include "fmt.display.h"
#include "test.assert.h"
#include "fmt.display.h"
int main() {
fmt::println("Hello, world!");
fmt::print("Value: ", 42, "\n");
return 0;
}
Output:
Hello, world!
Value: 42
Define a Display
specialization for your custom types:
struct Point {
int x, y;
};
template<>
struct fmt::Display<Point> {
static std::string print(const Point& p) {
return fmt::sprint("(", p.x, ", ", p.y, ")");
}
};
int main() {
Point p{10, 20};
fmt::println("Point: ", p);
return 0;
}
Output:
Point: (10, 20)
The library provides built-in support for standard containers:
#include <vector>
#include <map>
#include <set>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::map<std::string, int> map = {{"one", 1}, {"two", 2}};
std::set<char> set = {'a', 'b', 'c'};
fmt::println("Vector: ", vec);
fmt::println("Map: ", map);
fmt::println("Set: ", set);
return 0;
}
Output:
Vector: [1, 2, 3, 4, 5]
Map: {one: 1, two: 2}
Set: {a, b, c}
Use ANSI color codes for terminal output:
fmt::println(ansi::red, "Error: ", ansi::reset, "Something went wrong!");
Output (colors may not be visible here):
Error: Something went wrong!
(The word "Error:" would appear in red in a terminal that supports ANSI colors)
Use the fmtout
class for building complex strings:
fmt::fmtout out;
out << "Hello, " << ansi::blue << "world" << ansi::reset << "!";
fmt::println(out);
Output (colors may not be visible here):
Hello, world!
(The word "world" would appear in blue in a terminal that supports ANSI colors)
The library also supports C++17 features like std::optional
and std::variant
:
#include <optional>
#include <variant>
int main() {
std::optional<int> opt1 = 42;
std::optional<int> opt2;
fmt::println("Optional with value: ", opt1);
fmt::println("Optional without value: ", opt2);
std::variant<int, float, std::string> var1 = 10;
std::variant<int, float, std::string> var2 = 3.14f;
std::variant<int, float, std::string> var3 = "Hello";
fmt::println("Variant with int: ", var1);
fmt::println("Variant with float: ", var2);
fmt::println("Variant with string: ", var3);
return 0;
}
Output:
Optional with value: Some(42)
Optional without value: None
Variant with int: Variant(10)
Variant with float: Variant(3.14)
Variant with string: Variant(Hello)
The test::assert.h
file adds a simple and intuitive way to write unit tests for your C++ code. It includes assertion functions and a testing runner.
#include "test.assert.h"
int main() {
RUN_TESTS;
return 0;
}
MAKE_TESTS{
It(divide by zero){
asserteq(1 / 1, 1);
}
It(simple equality test){
asserteq("test", "test");
}
{
assert(42 > 0);
}
}
test 1 passed... [1 / 1:`1` == 1:`1`] It(divide by zero)
test 2 passed... ["test":`test` == "test":`test`] It(simple equality test)
test 3 passed... [42 > 0:`True` == True] It(test case 3)
Result :
3 tests passed
0 tests failed
This framework allows you to structure and run your tests efficiently with clear output indicating which tests passed or failed.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.
We're always looking to improve fmt::display
and the testing framework. Here are some areas we're considering for future development:
- Performance optimizations.
- Support for more complex formatting options (padding, alignment, etc.).
- Integration with existing logging libraries.
- More extensive testing suite.
- Additional specializations for other standard library types.
- Expanded functionality for the testing framework.
If you have any suggestions or would like to contribute to these efforts, please open an issue or submit a pull request!