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
//! Command-line parsing and conversion into application startup values.
use std::path::{Path, PathBuf};
use clap::{Parser, ValueEnum};
use crate::app::AppView;
/// A read-only terminal Git history, diff, and source-code explorer.
#[derive(Debug, Parser)]
#[command(name = "chronogit", version, about)]
pub struct Cli {
/// Repository root or any directory below it.
#[arg(value_name = "PATH", default_value = ".")]
path: PathBuf,
/// View to open first.
#[arg(long, value_enum, default_value_t = InitialView::Changes)]
view: InitialView,
/// Keymap file. Defaults to $XDG_CONFIG_HOME/chronogit/keymap.conf when present.
#[arg(long, value_name = "PATH")]
keymap: Option<PathBuf>,
/// Enable a trusted external language-server profile (repeatable).
#[arg(long = "lsp", value_name = "PROFILE")]
lsp: Vec<String>,
/// Trusted user-level LSP profile file. Defaults to XDG config when present.
#[arg(long, value_name = "PATH")]
lsp_config: Option<PathBuf>,
}
impl Cli {
/// Returns the repository root candidate supplied by the user.
///
/// The path may name the worktree root or any directory below it; repository
/// discovery resolves the actual absolute root later.
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
/// Converts the CLI view choice into the corresponding application view.
#[must_use]
pub fn initial_view(&self) -> AppView {
self.view.into()
}
/// Returns the explicitly requested keymap path, when supplied.
#[must_use]
pub fn keymap(&self) -> Option<&Path> {
self.keymap.as_deref()
}
/// Returns the explicitly enabled language-server profile identifiers.
#[must_use]
pub fn lsp_profiles(&self) -> &[String] {
&self.lsp
}
/// Returns the explicitly selected trusted LSP configuration path.
#[must_use]
pub fn lsp_config(&self) -> Option<&Path> {
self.lsp_config.as_deref()
}
}
/// Main view selected by the `--view` command-line option.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, ValueEnum)]
pub enum InitialView {
/// Show unstaged worktree changes.
#[default]
Changes,
/// Show paged commit history and diffs.
History,
/// Show commit parent lanes.
Graph,
/// Browse the working-tree file tree and source code.
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_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"]);
}
}