Refactor structure
This commit is contained in:
63
ui/src/cli/mod.rs
Executable file
63
ui/src/cli/mod.rs
Executable file
@@ -0,0 +1,63 @@
|
||||
mod folder;
|
||||
mod sync;
|
||||
|
||||
use crate::{app, helpers};
|
||||
use clap::{command, Parser};
|
||||
use color_eyre::Result;
|
||||
use ratatui::crossterm;
|
||||
use crate::cli::folder::FolderCommand;
|
||||
use crate::cli::sync::DLSiteCommand;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
enum CliSubCommand {
|
||||
Folder(FolderCommand),
|
||||
#[command(name = "dlsite")]
|
||||
DLSite(DLSiteCommand),
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about)]
|
||||
pub struct Cli {
|
||||
#[command(subcommand)]
|
||||
subcommand: Option<CliSubCommand>,
|
||||
}
|
||||
|
||||
impl Cli {
|
||||
pub async fn run(&self) -> Result<()> {
|
||||
helpers::initialize_folders().await?;
|
||||
if self.subcommand.is_none() {
|
||||
return self.start_tui().await;
|
||||
}
|
||||
if let Some(sub_command) = &self.subcommand {
|
||||
return sub_command.handle().await;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn start_tui(&self) -> Result<()> {
|
||||
crossterm::terminal::enable_raw_mode()?;
|
||||
let mut terminal = ratatui::init();
|
||||
struct TuiCleanup;
|
||||
impl Drop for TuiCleanup {
|
||||
fn drop(&mut self) {
|
||||
ratatui::restore();
|
||||
let _ = crossterm::terminal::disable_raw_mode();
|
||||
}
|
||||
}
|
||||
let _cleanup = TuiCleanup;
|
||||
let app = app::App::create().await?;
|
||||
let result = app.run(&mut terminal).await;
|
||||
ratatui::restore();
|
||||
crossterm::terminal::disable_raw_mode()?;
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
impl CliSubCommand {
|
||||
pub async fn handle(&self) -> Result<()> {
|
||||
match self {
|
||||
CliSubCommand::Folder(cmd) => cmd.subcommand.handle().await,
|
||||
CliSubCommand::DLSite(cmd) => cmd.subcommand.handle().await,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user