-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expand examples #8
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,73 @@ | ||
#include <forest/forest.hpp> | ||
#include <cstdio> | ||
#include <iostream> | ||
#include <iterator> | ||
#include <memory> | ||
|
||
constexpr auto foo = forest::literal<64>("<rgb=500><b><i>hello</b></i></rgb> world"); | ||
|
||
template <typename F> | ||
void with_temp_file(F const fn) { | ||
constexpr auto tf_name = "temp.txt"; | ||
|
||
auto const file_deleter = [=](std::FILE* const handle) -> void { | ||
std::fclose(handle); | ||
std::remove(tf_name); | ||
}; | ||
|
||
auto temp_file = std::unique_ptr<std::FILE, decltype(file_deleter)>(std::fopen(tf_name, "w+"), file_deleter); | ||
|
||
if (temp_file == nullptr) { return; } | ||
|
||
fn(temp_file.get()); | ||
} | ||
|
||
int main() { | ||
std::cout << foo << '\n'; | ||
|
||
constexpr std::string_view strike_and_underline_styles = "I <strike>copy</strike> <b>study</b> code from <u>stackoverflow.com</u>"; | ||
std::cout << forest::literal<strike_and_underline_styles.size()>(strike_and_underline_styles) << '\n'; | ||
|
||
// TERMINAL SUPPORT MAY VARY | ||
constexpr std::string_view blink_style = "<blink>Hey listen!</blink>"; | ||
std::cout << forest::literal<blink_style.size()>(blink_style) << '\n'; | ||
|
||
constexpr std::string_view dim_style = "<dim>Loading...</dim>"; | ||
std::cout << forest::literal<dim_style.size()>(dim_style) << '\n'; | ||
// | ||
|
||
constexpr std::string_view reset_and_invert_styles = "<rgb=150>G<reset>O</reset></rgb> <rgb=150><invert>Team!</invert></rgb>"; | ||
std::cout << forest::literal<reset_and_invert_styles.size()>(reset_and_invert_styles) << '\n'; | ||
|
||
std::cout << forest::format("<i><u>Formatted</u></i>") << '\n'; | ||
|
||
auto str = std::string("<invert>forest</invert> <dim>v"); | ||
str += forest::version_v; | ||
str += "</dim>\n"; | ||
forest::print(str); | ||
|
||
// Uncomment to clear everything written so far | ||
// std::cout << forest::literal<24>("<clear>Goodbye..</clear>") << '\n'; | ||
|
||
std::string ft{}; | ||
forest::format_to(std::back_inserter(ft), "<b><rgb=155>Hello from the string!</rgb></b>"); | ||
std::cout << ft << '\n'; | ||
|
||
with_temp_file([](std::FILE* const tf) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice pattern! Inspired from Python? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haskell 😎 |
||
forest::print_to(tf, "<rgb=505><b>Hello from the file!</b></rgb>"); | ||
|
||
// Required when printing to FILE* that is not stdout/stderr | ||
// As forest::print_to leaves the file pointer at the end of the file | ||
// which is the end of the written text | ||
// Can also be a call to std::fseek(tf, 0, SEEK_SET) | ||
std::rewind(tf); | ||
|
||
constexpr int BUFFER_SIZE{256}; | ||
char buffer[BUFFER_SIZE]{}; | ||
while (std::fgets(buffer, BUFFER_SIZE, tf) != nullptr) { std::cout << buffer << '\n'; } | ||
}); | ||
|
||
forest::print_to(stdout, "<rgb=050><b>Hello from stdout</b></rgb>\n"); | ||
|
||
forest::print_to(stderr, "<rgb=500><b>Hello from stderr!</b></rgb>\n"); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: make this
static
to auto capture it.