-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
106 lines (88 loc) · 2.89 KB
/
Copy pathmain.rs
File metadata and controls
106 lines (88 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::io::Write;
use clap::Parser;
use crossterm::{
QueueableCommand, cursor,
style::{Stylize, style},
terminal,
};
mod config;
mod events;
mod helpers;
mod matrix;
mod symbols;
// ====
// MAIN
// ====
/// The main entrypoint of the application
fn main() {
// Parse command-line arguments as the configuration
let config = config::Config::parse();
// Run the main logic with the given command-line arguments
match run(&config) {
Err(e) => {
eprintln!("{}", style(format!("Error: {e}")).red());
std::process::exit(1)
}
Ok(_) => std::process::exit(0),
}
}
/// Run the main logic of the application
fn run(config: &config::Config) -> std::io::Result<()> {
// Get a reference to stdout
let mut stdout = std::io::stdout();
// Get Terminal Window Size to determine the number of rows and columns
let (columns, rows) = terminal::size()?;
// Instantiate the matrix streams
let mut matrix = matrix::Matrix::new(rows, columns, config);
// Setup the terminal before running the application
setup(&mut stdout)?;
// Setup the Matrix renderer
matrix.setup(config, &mut stdout)?;
// Render the matrixfall on screen
loop {
// Render each stream
matrix.render(config, &mut stdout)?;
// Handle events
if crossterm::event::poll(std::time::Duration::from_millis(1000 / config.fps as u64))?
&& let events::Action::Exit = events::handle_events()?
{
break;
}
}
// Cleanup the terminal after the application stops
cleanup(&mut stdout)?;
Ok(())
}
// HELPER FUNCTIONS
// ----------------
/// Prepares the terminal by switching to the alternate screen and clearing it.
/// Also moves the cursor to the top before hiding it from view.
/// Registers a panic-hook to automatically call the `cleanup` function
fn setup(stdout: &mut std::io::Stdout) -> std::io::Result<()> {
terminal::enable_raw_mode()?;
stdout
.queue(terminal::EnterAlternateScreen)?
.queue(terminal::Clear(terminal::ClearType::All))?
.queue(cursor::MoveTo(0, 0))?
.queue(cursor::Hide)?
.flush()?;
// Create a custom hook to handle graceful cleanup of the terminal when panicking
let original_panic = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
let mut stdout = std::io::stdout();
// Intentionally ignore errors here since we're already in a panic!
let _ = cleanup(&mut stdout);
original_panic(info);
}));
Ok(())
}
/// Restores terminal to its original state by leaving alternate screen,
/// showing the cursor, and disabling raw mode.
fn cleanup(stdout: &mut std::io::Stdout) -> std::io::Result<()> {
stdout
.queue(terminal::LeaveAlternateScreen)?
.queue(cursor::Show)?
.flush()?;
terminal::disable_raw_mode()?;
Ok(())
}