use std::path::{Path, PathBuf};
use clap::{Parser, ValueEnum};
use crate::app::AppView;
#[derive(Debug, Parser)]
#[command(name = "chronogit", version, about)]
pub struct Cli {
#[arg(value_name = "PATH", default_value = ".")]
path: PathBuf,
#[arg(long, value_enum, default_value_t = InitialView::Changes)]
view: InitialView,
#[arg(long, value_name = "PATH")]
keymap: Option<PathBuf>,
#[arg(long, value_name = "PATH")]
config: Option<PathBuf>,
#[arg(long, value_name = "LINES")]
scrolloff: Option<usize>,
#[arg(long = "lsp", value_name = "PROFILE")]
lsp: Vec<String>,
#[arg(long, value_name = "PATH")]
lsp_config: Option<PathBuf>,
}
impl Cli {
#[must_use]
pub fn config(&self) -> Option<&Path> {
self.config.as_deref()
}
#[must_use]
pub fn scrolloff(&self) -> Option<usize> {
self.scrolloff
}
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
#[must_use]
pub fn initial_view(&self) -> AppView {
self.view.into()
}
#[must_use]
pub fn keymap(&self) -> Option<&Path> {
self.keymap.as_deref()
}
#[must_use]
pub fn lsp_profiles(&self) -> &[String] {
&self.lsp
}
#[must_use]
pub fn lsp_config(&self) -> Option<&Path> {
self.lsp_config.as_deref()
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, ValueEnum)]
pub enum InitialView {
#[default]
Changes,
History,
Graph,
Code,
}
impl From<InitialView> for AppView {
fn from(value: InitialView) -> Self {
match value {
InitialView::Changes => Self::Changes,
InitialView::History => Self::History,
InitialView::Graph => Self::Graph,
InitialView::Code => Self::Code,
}
}
}
#[cfg(test)]
mod tests {
use clap::Parser;
use super::Cli;
use crate::app::AppView;
#[test]
fn parses_display_settings_and_rejects_invalid_margins() {
let default = Cli::try_parse_from(["chronogit"]).unwrap_or_else(|error| panic!("{error}"));
assert_eq!(default.scrolloff(), None);
for value in ["0", "2", "3"] {
let cli =
Cli::try_parse_from(["chronogit", "--config", "custom.toml", "--scrolloff", value])
.unwrap_or_else(|error| panic!("{error}"));
assert_eq!(
cli.scrolloff().map(|value| value.to_string()).as_deref(),
Some(value)
);
assert_eq!(cli.config(), Some(std::path::Path::new("custom.toml")));
}
for value in ["-1", "2.5", "invalid"] {
assert!(Cli::try_parse_from(["chronogit", "--scrolloff", value]).is_err());
}
}
#[test]
fn parses_code_as_an_initial_view() {
let cli = Cli::try_parse_from(["chronogit", "--view", "code"])
.unwrap_or_else(|error| panic!("could not parse code view: {error}"));
assert_eq!(cli.initial_view(), AppView::Code);
}
#[test]
fn accepts_multiple_explicit_lsp_profiles() {
let cli = Cli::try_parse_from([
"chronogit",
"--lsp",
"rust-analyzer",
"--lsp",
"jdtls",
"--lsp",
"pyright",
])
.unwrap_or_else(|error| panic!("could not parse LSP profiles: {error}"));
assert_eq!(cli.lsp_profiles(), &["rust-analyzer", "jdtls", "pyright"]);
}
}