|
1 | 1 | #include <forest/forest.hpp>
|
| 2 | +#include <cstdio> |
2 | 3 | #include <iostream>
|
| 4 | +#include <iterator> |
| 5 | +#include <memory> |
3 | 6 |
|
4 | 7 | constexpr auto foo = forest::literal<64>("<rgb=500><b><i>hello</b></i></rgb> world");
|
5 | 8 |
|
| 9 | +template <typename F> |
| 10 | +void with_temp_file(F const fn) { |
| 11 | + constexpr auto tf_name = "temp.txt"; |
| 12 | + |
| 13 | + auto const file_deleter = [=](std::FILE* const handle) -> void { |
| 14 | + std::fclose(handle); |
| 15 | + std::remove(tf_name); |
| 16 | + }; |
| 17 | + |
| 18 | + auto temp_file = std::unique_ptr<std::FILE, decltype(file_deleter)>(std::fopen(tf_name, "w+"), file_deleter); |
| 19 | + |
| 20 | + if (temp_file == nullptr) { return; } |
| 21 | + |
| 22 | + fn(temp_file.get()); |
| 23 | +} |
| 24 | + |
6 | 25 | int main() {
|
7 | 26 | std::cout << foo << '\n';
|
| 27 | + |
| 28 | + constexpr std::string_view strike_and_underline_styles = "I <strike>copy</strike> <b>study</b> code from <u>stackoverflow.com</u>"; |
| 29 | + std::cout << forest::literal<strike_and_underline_styles.size()>(strike_and_underline_styles) << '\n'; |
| 30 | + |
| 31 | + // TERMINAL SUPPORT MAY VARY |
| 32 | + constexpr std::string_view blink_style = "<blink>Hey listen!</blink>"; |
| 33 | + std::cout << forest::literal<blink_style.size()>(blink_style) << '\n'; |
| 34 | + |
| 35 | + constexpr std::string_view dim_style = "<dim>Loading...</dim>"; |
| 36 | + std::cout << forest::literal<dim_style.size()>(dim_style) << '\n'; |
| 37 | + // |
| 38 | + |
| 39 | + constexpr std::string_view reset_and_invert_styles = "<rgb=150>G<reset>O</reset></rgb> <rgb=150><invert>Team!</invert></rgb>"; |
| 40 | + std::cout << forest::literal<reset_and_invert_styles.size()>(reset_and_invert_styles) << '\n'; |
| 41 | + |
| 42 | + std::cout << forest::format("<i><u>Formatted</u></i>") << '\n'; |
| 43 | + |
8 | 44 | auto str = std::string("<invert>forest</invert> <dim>v");
|
9 | 45 | str += forest::version_v;
|
10 | 46 | str += "</dim>\n";
|
11 | 47 | forest::print(str);
|
| 48 | + |
| 49 | + // Uncomment to clear everything written so far |
| 50 | + // std::cout << forest::literal<24>("<clear>Goodbye..</clear>") << '\n'; |
| 51 | + |
| 52 | + std::string ft{}; |
| 53 | + forest::format_to(std::back_inserter(ft), "<b><rgb=155>Hello from the string!</rgb></b>"); |
| 54 | + std::cout << ft << '\n'; |
| 55 | + |
| 56 | + with_temp_file([](std::FILE* const tf) { |
| 57 | + forest::print_to(tf, "<rgb=505><b>Hello from the file!</b></rgb>"); |
| 58 | + |
| 59 | + // Required when printing to FILE* that is not stdout/stderr |
| 60 | + // As forest::print_to leaves the file pointer at the end of the file |
| 61 | + // which is the end of the written text |
| 62 | + // Can also be a call to std::fseek(tf, 0, SEEK_SET) |
| 63 | + std::rewind(tf); |
| 64 | + |
| 65 | + constexpr int BUFFER_SIZE{256}; |
| 66 | + char buffer[BUFFER_SIZE]{}; |
| 67 | + while (std::fgets(buffer, BUFFER_SIZE, tf) != nullptr) { std::cout << buffer << '\n'; } |
| 68 | + }); |
| 69 | + |
| 70 | + forest::print_to(stdout, "<rgb=050><b>Hello from stdout</b></rgb>\n"); |
| 71 | + |
| 72 | + forest::print_to(stderr, "<rgb=500><b>Hello from stderr!</b></rgb>\n"); |
12 | 73 | }
|
0 commit comments