From 3572174f306cf32101f463bc326a1062d527eaec Mon Sep 17 00:00:00 2001 From: leftibot Date: Sat, 11 Apr 2026 15:04:56 -0600 Subject: [PATCH] Address review: add IO redirection section to cheatsheet Documents set_print_handler() and set_println_handler() with usage examples for GUI embedding and output capture. Requested by @lefticus in PR #657 review. Co-Authored-By: Claude Opus 4.6 (1M context) --- cheatsheet.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/cheatsheet.md b/cheatsheet.md index 1819ddc0..6a28d5b4 100644 --- a/cheatsheet.md +++ b/cheatsheet.md @@ -815,5 +815,42 @@ m.is_type("MyClass") // true (checks the ChaiScript class name) * `from_json` converts a JSON string into its strongly typed (map, vector, int, double, string) representations * `to_json` converts a ChaiScript object (either a `Object` or one of map, vector, int, double, string) tree into its JSON string representation +## IO Redirection + +By default, ChaiScript's `print()` and `puts()` functions write to stdout. You can redirect +output on a per-instance basis using custom handlers. Each ChaiScript instance can have its +own independent handlers. + +```cpp +chaiscript::ChaiScript chai; + +// Redirect print() / println_string() output (includes newline) +chai.set_println_handler([](const std::string &s) { + my_log_window.append(s + "\n"); +}); + +// Redirect puts() / print_string() output (no newline) +chai.set_print_handler([](const std::string &s) { + my_log_window.append(s); +}); +``` + +This is useful for embedding ChaiScript in GUI applications, logging frameworks, or any +context where stdout is not the desired output destination. + +```cpp +// Example: capture all output to a string +std::string captured; +chai.set_println_handler([&captured](const std::string &s) { + captured += s + "\n"; +}); +chai.set_print_handler([&captured](const std::string &s) { + captured += s; +}); + +chai.eval("print(42)"); // captured == "42\n" +chai.eval("puts(\"hi\")"); // captured == "42\nhi" +``` + ## Extras ChaiScript itself does not provide a link to the math functions defined in ``. You can either add them yourself, or use the [ChaiScript_Extras](https://github.com/ChaiScript/ChaiScript_Extras) helper library. (Which also provides some additional string functions.)