Skip to content

Commit e926474

Browse files
author
flyingsl0ths
committed
Resolves #3
1 parent 9190069 commit e926474

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

example/forest-example.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,65 @@
11
#include <forest/forest.hpp>
2+
#include <cstdio>
3+
#include <functional>
24
#include <iostream>
5+
#include <iterator>
36

47
constexpr auto foo = forest::literal<64>("<rgb=500><b><i>hello</b></i></rgb> world");
58

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+
623
int main() {
724
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+
836
auto str = std::string("<invert>forest</invert> <dim>v");
937
str += forest::version_v;
1038
str += "</dim>\n";
1139
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");
1265
}

0 commit comments

Comments
 (0)