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