Repository navigation
Expand file tree
/
Copy pathmain.rs
More file actions
148 lines (126 loc) · 4.33 KB
/
Copy pathmain.rs
File metadata and controls
148 lines (126 loc) · 4.33 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#![cfg_attr(not(debug_assertions), windows_subsystem = "console")]
#![allow(clippy::collapsible_if)]
#![allow(clippy::collapsible_else_if)]
// TEMPORARILY
#![allow(clippy::uninlined_format_args)]
#![allow(clippy::assigning_clones)]
mod about;
mod commands;
mod date_ago;
mod ui;
mod unsaved_settings;
use gossip_lib::{Error, RunState, GLOBALS};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::{env, thread};
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
use tracing_subscriber::prelude::*;
pub const AVATAR_SIZE: u32 = 48; // points, not pixels
pub const AVATAR_SIZE_F32: f32 = 48.0; // points, not pixels
pub const AVATAR_SIZE_REPOST: u32 = 27; // points, not pixels
pub const AVATAR_SIZE_REPOST_F32: f32 = 27.0; // points, not pixels
fn main() -> Result<(), Error> {
// Read command line parameters
let mut rapid: bool = false;
let mut debug_async: bool = false;
{
let mut args = env::args();
let _ = args.next(); // program name
for cmd in args {
if &*cmd == "--rapid" {
rapid = true;
}
if &*cmd == "--debug-async" {
debug_async = true;
}
}
}
// Setup logging
if env::var("RUST_LOG").is_err() {
env::set_var("RUST_LOG", "info");
}
let env_filter = EnvFilter::from_default_env();
let max_level = match env_filter.max_level_hint() {
Some(l) => l,
None => LevelFilter::ERROR,
};
let show_debug = cfg!(debug_assertions) || max_level <= LevelFilter::DEBUG;
if debug_async {
let console_layer = console_subscriber::spawn();
let main_layer = tracing_subscriber::fmt::layer()
.with_writer(std::io::stderr)
.with_target(false)
.with_file(show_debug)
.with_line_number(show_debug)
.with_filter(env_filter);
tracing_subscriber::registry()
.with(console_layer)
.with(main_layer)
.init();
} else {
tracing_subscriber::fmt::fmt()
.with_writer(std::io::stderr)
.with_target(false)
.with_file(show_debug)
.with_line_number(show_debug)
.with_env_filter(env_filter)
.init();
}
let about = about::About::new();
tracing::info!("Gossip {}", about.version);
// restart args
let mut args = env::args();
let _ = args.next(); // program name
if rapid {
let _ = args.next(); // --rapid param
}
if debug_async {
let _ = args.next(); // --debug-async param
}
// Setup async, and allow non-async code the context to spawn tasks
let _main_rt = GLOBALS.runtime.enter(); // <-- this allows it.
// Initialize the lib
GLOBALS
.runtime
.block_on(async { gossip_lib::init(rapid, args.len() > 0).await })?;
// If we were handed a command, execute the command and (usually) return
let exit: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
let exit2 = exit.clone();
if args.len() > 0 {
GLOBALS.runtime.block_on(async {
let exit = match commands::handle_command(args).await {
Err(e) => {
println!("{}", e);
true
}
Ok(exit) => exit,
};
if exit {
exit2.store(true, Ordering::Relaxed);
}
});
if exit.load(Ordering::Relaxed) {
return Ok(());
}
}
// We run our main async code on a separate thread, not just a
// separate task. This leave the main thread for UI work only.
// egui is most portable when it is on the main thread.
let async_thread = thread::spawn(move || {
GLOBALS.runtime.block_on(Box::pin(gossip_lib::run()));
});
// Run the UI
if let Err(e) = ui::run() {
tracing::error!("{}", e);
}
// Move to the ShuttingDown runstate
let _ = GLOBALS.write_runstate.send(RunState::ShuttingDown);
// Make sure the overlord isn't stuck on waiting for login
GLOBALS.wait_for_login.store(false, Ordering::Relaxed);
GLOBALS.wait_for_login_notify.notify_one();
tracing::info!("UI thread complete, waiting on lib...");
// Wait for the async thread to complete
async_thread.join().unwrap();
tracing::info!("Gossip end.");
Ok(())
}