Krolyxon
Commit 61cb83e

rmptuisrcmain.rs

59 lines · 1.7 KB · Rust

1use std::{io, path::PathBuf, process::ExitCode};
2
3use clap::Parser;
4use crossterm::{
5 event::{DisableMouseCapture, EnableMouseCapture},
6 execute,
7};
8use rmptui::{app::App, config::Config, logging};
9
10/// A fast, keyboard-driven MPD client for the terminal.
11#[derive(Debug, Parser)]
12#[command(version, about)]
13struct Args {
14 /// Use this configuration file instead of the default location.
15 #[arg(short, long, value_name = "FILE")]
16 config: Option<PathBuf>,
17 /// Print the default configuration and exit.
18 #[arg(long)]
19 print_default_config: bool,
20}
21
22#[tokio::main]
23async fn main() -> ExitCode {
24 let args = Args::parse();
25 if args.print_default_config {
26 print!("{}", rmptui::config::DEFAULT_CONFIG);
27 return ExitCode::SUCCESS;
28 }
29 if let Err(error) = logging::init(logging::LOG_PATH.as_ref()) {
30 eprintln!("rmptui: cannot log to {}: {error}", logging::LOG_PATH);
31 }
32 match run(args).await {
33 Ok(()) => ExitCode::SUCCESS,
34 Err(error) => {
35 tracing::error!("exiting: {error:#}");
36 eprintln!("rmptui: {error:#}");
37 ExitCode::FAILURE
38 }
39 }
40}
41
42async fn run(args: Args) -> anyhow::Result<()> {
43 let (config, path) = Config::load(args.config.as_deref())?;
44 tracing::info!("using configuration {}", path.display());
45 let mouse = config.ui.mouse;
46 let app = App::new(config)?;
47
48 // `init` installs a panic hook that restores the terminal.
49 let mut terminal = ratatui::init();
50 if mouse {
51 execute!(io::stdout(), EnableMouseCapture)?;
52 }
53 let result = app.run(&mut terminal).await;
54 if mouse {
55 let _ = execute!(io::stdout(), DisableMouseCapture);
56 }
57 ratatui::restore();
58 result
59}