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//! ```
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::new(
33//! // The input label, what text the user will see at the bottom right before the input.
34//! String::from("Input: "),
35//! // The X and Y paddings for the input line.
36//! // X would be how many spaces to the left of the input label.
37//! Vector2::new(1, 1),
38//! // The X and Y paddings for the main text output section (buffer).
39//! // X gets split into left and right (4 each), and Y is how many spaces from the top the text
40//! // will have.
41//! Vector2::new(8, 2)
42//! );
43//!
44//! // Instantiate chaos, passing the needed stdout and options.
45//! // This is mostly what you will work with, whether it be printing pages, taking input, etc.
46//! // Alternate screens are isolated terminal buffers, they are useful to restore the terminal
47//! // state back to how it was, so activating them is very useful.
48//! let mut chaos = Chaos::new(stdout, options);
49//! chaos.alternate_screen(true);
50//!
51//! // Instantiate a page where it will contain some text to display.
52//! // A page by default is empty, therefore you need to push text to it so it gets properly
53//! // displayed.
54//! let mut page = Page::new();
55//! page.push("hello, world!");
56//!
57//! // This loop will contain the logic of the program, as ChaosEngine works by repeatedly
58//! // taking input from the user.
59//! // You can check the user input for specific words or patterns yourself, then execute some
60//! // code.
61//! loop {
62//! // Clearing the terminal is necessary for each loop, to prevent printing over existing
63//! // text.
64//! chaos.clear_terminal();
65//!
66//! // Get text input from the user.
67//! let input = chaos.get_input(&mut page).unwrap();
68//!
69//! // Test for what the input could possibly be, if it's "exit", then quit the program.
70//! match input.as_str() {
71//! "exit" => {
72//! // Breaking the loop is how you quit the program.
73//! // Exiting the alternate screen is necessary before exiting to restore back the
74//! // text that was on the terminal before starting the program.
75//! chaos.alternate_screen(false);
76//! break;
77//! },
78//! _ => (),
79//! }
80//! }
81//! }
82//! ```
83
84pub mod chaos;
85pub mod text;
86pub mod types;
87
88pub use chaos::*;
89pub use text::*;