pub mod html_view;
pub mod latex_view;
pub mod markdown_view;
pub mod mermaid_view;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
#[cfg(test)]
mod tests;
#[derive(Debug, PartialEq, Clone)]
pub enum StartupMode {
OpenFile(PathBuf),
OpenDir(PathBuf),
Empty,
MarkdownPreview { file: PathBuf, no_color: bool },
HtmlPreview { file: PathBuf, no_color: bool },
MermaidPreview { file: PathBuf, no_color: bool },
LatexPreview { file: PathBuf, no_color: bool },
ViewFile { file: PathBuf, line_numbers: bool },
CliCommandExecuted,
}
#[derive(Parser, Debug)]
#[command(name = "lala")]
#[command(author = "lala team")]
#[command(version = "0.1.0")]
#[command(about = "軽量で高速なテキストエディタ", long_about = None)]
#[command(disable_version_flag = true)]
struct Args {
#[arg(value_name = "PATH")]
path: Option<PathBuf>,
#[arg(short = 'v', long = "version", action = clap::ArgAction::Version)]
_version: Option<bool>,
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand, Debug)]
enum Commands {
#[command(about = "Preview Markdown file in terminal")]
Markdown {
#[arg(value_name = "FILE")]
file: PathBuf,
#[arg(long)]
no_color: bool,
},
#[command(about = "Preview HTML file in terminal")]
Html {
#[arg(value_name = "FILE")]
file: PathBuf,
#[arg(long)]
no_color: bool,
},
#[command(about = "Preview Mermaid diagram in terminal")]
Mermaid {
#[arg(value_name = "FILE")]
file: PathBuf,
#[arg(long)]
no_color: bool,
},
#[command(about = "Preview LaTeX document in terminal")]
Latex {
#[arg(value_name = "FILE")]
file: PathBuf,
#[arg(long)]
no_color: bool,
},
#[command(about = "View file content")]
View {
#[arg(value_name = "FILE")]
file: PathBuf,
#[arg(short = 'n', long)]
line_numbers: bool,
},
}
pub fn parse_args<I, T>(args: I) -> StartupMode
where
I: IntoIterator<Item = T>,
T: Into<std::ffi::OsString> + Clone,
{
let args = Args::parse_from(args);
if let Some(command) = args.command {
return match command {
Commands::Markdown { file, no_color } => {
StartupMode::MarkdownPreview { file, no_color }
}
Commands::Html { file, no_color } => StartupMode::HtmlPreview { file, no_color },
Commands::Mermaid { file, no_color } => StartupMode::MermaidPreview { file, no_color },
Commands::Latex { file, no_color } => StartupMode::LatexPreview { file, no_color },
Commands::View { file, line_numbers } => StartupMode::ViewFile { file, line_numbers },
};
}
match args.path {
Some(path) => {
if path.extension().is_some() {
StartupMode::OpenFile(path)
} else if path.is_dir() {
StartupMode::OpenDir(path)
} else if path.is_file() {
StartupMode::OpenFile(path)
} else {
if path.extension().is_some() {
StartupMode::OpenFile(path)
} else {
StartupMode::OpenDir(path)
}
}
}
None => StartupMode::Empty,
}
}
pub fn parse_args_default() -> StartupMode {
parse_args(std::env::args())
}