-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.rs
66 lines (56 loc) · 2.1 KB
/
main.rs
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
/// Fakeminimap2 is an example of how to use the minimap2 crate with multithreading, preferring crossbeam's channels.
/// Although mpsc is also available in the standard library.
///
/// For logging, pass in RUST_LOG=debug or RUST_LOG=trace to see more information. RUST_LOG=info is also supported.
// CLI interface
mod cli;
// Multithreading methods
mod channels; // I prefer using channels over rayon, but rayon is simpler to use
mod rayon;
use flexi_logger::{FileSpec, Logger, WriteMode};
// Ignore the tokio stuff, it's just for visualization and interaction!
#[tokio::main]
async fn main() {
flexi_logger::Logger::try_with_env_or_str("info")
.unwrap()
.log_to_file(FileSpec::default())
.write_mode(WriteMode::BufferAndFlush)
.start()
.expect("Unable to start logger");
// Parse command line arguments
let args = cli::parse_args();
// UI Stuff
let (dispatcher_tx, dispatcher_rx) = mpsc::unbounded_channel::<state::Action>();
let (ui_tx, ui_rx) = tokio::sync::watch::channel(None);
{
let dispatcher_tx = dispatcher_tx.clone();
let handle = std::thread::spawn(move || match args.method.unwrap_or_default() {
cli::Method::Channels => {
channels::map_with_channels(
args.target,
args.query,
args.threads,
dispatcher_tx.clone(),
)
.expect("Error mapping with channels");
}
cli::Method::Rayon => {
rayon::map(args.target, args.query, args.threads, dispatcher_tx.clone())
.expect("Error mapping with rayon");
}
});
}
// Runs the UI Loop
tokio::join!(
state::start_dispatcher(dispatcher_tx.clone(), dispatcher_rx, ui_tx),
ui::main_loop(dispatcher_tx.clone(), ui_rx),
);
}
// Trying to keep UI separated from mapping for easier code understanding
// UI Stuff
// UI Inspo: https://github.com/Yengas/rust-chat-server/tree/main/tui
mod datatypes;
mod state;
mod ui;
pub use datatypes::*;
use tokio::sync::mpsc;