chaos_engine/lib.rs
1//! # chaos-engine
2//!
3//! The ChaosEngine is a useful library that helps CLI-based projects that actively take input from
4//! the user to perform actions, which is suitable for text-based games in terminals.
5//!
6//! The way it works is you create a few [`Page`] instances, which would contain the text to be
7//! displayed by the program, then at the bottom the user would have to give some text input. All
8//! inputs made by the user must be handled by you, where you could exit the program/display a new
9//! page (like progressing in a story), start a mini-game on a different page, etc.
10//!
11//! ## Features
12//!
13//! ChaosEngine includes a few useful features, including:
14//!
15//! - Automatic word wrapping, with respect to paddings.
16//! - Automatic re-adjustments to word wrapping when the terminal gets resized.
17//! - Ability to create different pages to easily print whichever one you want depending on your
18//! logic.
19//!
20//! ## Examples
21//!
22//! Basic usage of ChaosEngine to display a hello world page, taking user input and creating a very
23//! simple exit command.
24//!
25//! ```no_run
26//! use chaos_engine::{Chaos, ChaosOptions, Page, types::Vector2};
27//!
28//! fn main() {
29//! let stdout = std::io::stdout();
30//!
31//! // These are needed options to customize how your program will look.
32//! let options = ChaosOptions {
33//! // The input label, what text the user will see at the bottom right before the input.
34//! input_label: "Input:",
35//! // The X and Y paddings for the main text output section (buffer).
36//! // X gets split into left and right (4 each), and Y is how many spaces from the top the text
37//! // will have.
38//! buffer_padding: Vector2::new(8, 2),
39//! // ChaosOptions provide default values, and this would use the default X and Y paddings for
40//! // the input line.
41//! // X would be how many spaces to the left of the input label.
42//! ..Default::default()
43//! };
44//!
45//! // Instantiate chaos, passing the needed stdout and options.
46//! // This is mostly what you will work with, whether it be printing pages, taking input, etc.
47//! // Alternate screens are isolated terminal buffers, they are useful to restore the terminal
48//! // state back to how it was, so activating them is very useful.
49//! let mut chaos = Chaos::new(stdout, options);
50//! chaos.alternate_screen(true);
51//!
52//! // Instantiate a page where it will contain some text to display.
53//! // A page by default is empty, therefore you need to push text to it so it gets properly
54//! // displayed.
55//! let mut page = Page::new();
56//! page.push("hello, world!");
57//!
58//! // This loop will contain the logic of the program, as ChaosEngine works by repeatedly
59//! // taking input from the user.
60//! // You can check the user input for specific words or patterns yourself, then execute some
61//! // code.
62//! loop {
63//! // Clearing the terminal is necessary for each loop, to prevent printing over existing
64//! // text.
65//! chaos.clear_terminal();
66//!
67//! // Get text input from the user.
68//! let input = chaos.get_input(&mut page).unwrap();
69//!
70//! // Test for what the input could possibly be, if it's "exit", then quit the program.
71//! match input.as_str() {
72//! "exit" => {
73//! // Breaking the loop is how you quit the program.
74//! // Exiting the alternate screen is necessary before exiting to restore back the
75//! // text that was on the terminal before starting the program.
76//! chaos.alternate_screen(false);
77//! break;
78//! },
79//! _ => (),
80//! }
81//! }
82//! }
83//! ```
84//!
85//! ## Features
86//!
87//! There is only one feature named `test`, and it's only there to help with writing integrated
88//! tests for the crate.
89
90pub mod chaos;
91pub mod text;
92pub mod types;
93
94pub use chaos::*;
95pub use text::*;