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
//! the core app
use {
super::{handlers::Handlers, interrupt::InterruptHandler, logging},
crate::{
app::cli::Cli,
client::E6Client,
config::instance::reload_config,
data::{pools::PoolDb, tags::TagDb},
error::Result,
getopt, opt_and,
ui::E6Ui,
},
color_eyre::eyre::Context,
std::sync::Arc,
tracing::info,
};
/// the e62rs app
pub struct E6App {
/// the logic handlers
handlers: Handlers,
}
impl E6App {
/// initialize e62rs
///
/// - 1. installs the miette error handler hook
/// - 2. validates the base api url
/// - 3. handles any cli arguments if any
/// - 4. clears the screen
/// - 5. sets up logging
/// - 6. sets up the custom interruption handler if enabled
/// - 7. sets up the ui
/// - 8. loads the config file
///
/// # Errors
///
/// returns an error if color_eyre fails to install [`color_eyre::install`]
/// returns an error if the cli fails to run
/// returns an error if it fails to setup logging
/// returns an error if it fails to setup the interrupt handler
/// returns an error if it fails to setup the UI
/// returns an error if it fails to load the configuration file
pub async fn init() -> Result<Self> {
miette::set_hook(Box::new(|_| {
Box::new(
miette::MietteHandlerOpts::new()
.terminal_links(true)
.unicode(true)
.context_lines(3)
.tab_width(4)
.build(),
)
}))?;
if crate::utils::ꟿ(crate::getopt!(http.api)) {
std::process::exit(1);
}
Cli::run().await?;
Self::clear_screen();
if getopt!(logging.enable) {
logging::setup()?;
}
owo_colors::set_override(getopt!(ui.colored_output));
let interrupt = Self::setup_interrupt_handler()?;
let ui = Self::setup_ui().await?;
reload_config()?;
Ok(Self {
handlers: Handlers::new(ui, interrupt),
})
}
/// run the main loop
///
/// # Errors
///
/// returns an error if the main loop fails
pub async fn run(&self) -> Result<()> {
self.handlers.run_main_loop().await
}
/// clear the screen
fn clear_screen() {
print!("\x1B[2J\x1B[3J\x1B[H");
std::io::Write::flush(&mut std::io::stdout()).unwrap();
}
/// setup the interruption handler
fn setup_interrupt_handler() -> Result<InterruptHandler> {
let handler = InterruptHandler::new();
let handler_clone = handler.clone();
if getopt!(ui.ctrlc_handler) {
ctrlc::set_handler(move || {
handler_clone.trigger();
})
.context("failed to set Ctrl+C handler")?;
}
Ok(handler)
}
/// setup the UI
async fn setup_ui() -> Result<E6Ui> {
let client = Arc::new(E6Client::default());
opt_and!(autoupdate.tags, client.update_tags().await?);
opt_and!(autoupdate.pools, client.update_pools().await?);
let tag_db = Arc::new(TagDb::load().context("failed to load tag db")?);
let pool_db = Arc::new(PoolDb::load().context("failed to load pool db")?);
info!(
"Starting {} v{} using {}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION"),
getopt!(http.api)
);
Ok(E6Ui::new(client, tag_db, pool_db))
}
}