Skip to content

Commit 5329099

Browse files
authored
Add basalt CLI layer (#510)
2 parents 0d0704c + 96fd8e1 commit 5329099

10 files changed

Lines changed: 273 additions & 5 deletions

Cargo.lock

Lines changed: 111 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

basalt/CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## [0.12.5](https://github.com/erikjuhani/basalt/releases/tag/basalt/0.12.5) (Unreleased)
3+
## [0.12.5](https://github.com/erikjuhani/basalt/releases/tag/basalt/0.12.5) (May, 16 2026)
44

55
### Added
66

@@ -17,6 +17,13 @@
1717
> the scrollbar is always shown if we have more than 4 items regardless if
1818
> we have space to show all items and not needing a scrollbar.
1919
20+
- [8e248e7](https://github.com/erikjuhani/basalt/commit/8e248e736afd27a36ba941479f68c8fa858bda9f) Add basalt CLI layer
21+
22+
> Initial CLI layer with --version and --help flags. The version output is
23+
> in a "standard" format similar to cargo version output `0.12.5 (abc123def 2026-05-15)`.
24+
>
25+
> The values are populated by a build phase (compile time) using env vars.
26+
2027
### Changed
2128

2229
- [022d988](https://github.com/erikjuhani/basalt/commit/022d988f1709a10d4ef45449e4a138390b585339) Add indentation to floating rename input by @erikjuhani
@@ -131,7 +138,7 @@
131138
> case, which then effectively retains the scroll position of the
132139
> viewport.
133140
134-
- [79082f3](https://github.com/erikjuhani/basalt/commit/79082f317efc499febb6bc7c892cd7f6c84b2887) Use config symbols for status bar component badge
141+
- [79082f3](https://github.com/erikjuhani/basalt/commit/79082f317efc499febb6bc7c892cd7f6c84b2887) Use config symbols for status bar component badge by @erikjuhani
135142

136143
> Pass the configured symbol preset into StatusBar so the active component
137144
> badge falls back to ASCII-safe glyphs when the Ascii preset is selected.

basalt/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ default-run = "basalt"
1313

1414
[dependencies]
1515
basalt-core = { path = "../basalt-core", version = "0.9.0" }
16+
clap = { version = "4.6.1", features = ["derive", "string"] }
1617
ratatui = { version = "0.30.0" }
1718
crossterm = "0.29.0"
1819
pulldown-cmark = "0.13.0"

basalt/build.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use std::{env, path::Path, process::Command};
2+
3+
fn main() {
4+
let Some(commit_info) = commit_info() else {
5+
return;
6+
};
7+
8+
let version = env::var("CARGO_PKG_VERSION").unwrap();
9+
10+
println!(
11+
"cargo:rustc-env=BASALT_COMMIT_SHORT_HASH={}",
12+
commit_info.short_hash
13+
);
14+
println!("cargo:rustc-env=BASALT_COMMIT_HASH={}", commit_info.hash);
15+
println!("cargo:rustc-env=BASALT_COMMIT_DATE={}", commit_info.date);
16+
println!("cargo:rustc-env=BASALT_VERSION={version}");
17+
}
18+
19+
struct CommitInfo {
20+
hash: String,
21+
short_hash: String,
22+
date: String,
23+
}
24+
25+
fn commit_info() -> Option<CommitInfo> {
26+
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
27+
let workspace_root = Path::new(&manifest_dir).parent()?;
28+
29+
// Not a git directory
30+
if !workspace_root.join(".git").exists() {
31+
return None;
32+
}
33+
34+
// Reference for commit info output: https://github.com/rust-lang/cargo/blob/a967402/build.rs#L60
35+
let output = match Command::new("git")
36+
.arg("log")
37+
.arg("-1")
38+
.arg("--date=short")
39+
.arg("--format=%H %h %cd")
40+
.arg("--abbrev=9")
41+
.output()
42+
{
43+
Ok(output) if output.status.success() => Some(output),
44+
_ => None,
45+
}?;
46+
47+
let stdout = String::from_utf8(output.stdout).unwrap();
48+
let stdout = stdout.trim();
49+
let mut parts = stdout.split_whitespace().map(|s| s.to_string());
50+
51+
let hash = parts.next()?;
52+
let short_hash = parts.next()?;
53+
let date = parts.next()?;
54+
55+
Some(CommitInfo {
56+
hash,
57+
short_hash,
58+
date,
59+
})
60+
}

basalt/src/cli.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use clap::Parser;
2+
3+
use crate::version;
4+
5+
const VERSION_INFO: version::VersionInfo = version::VersionInfo::from_env();
6+
7+
#[derive(Parser)]
8+
#[command(name = "basalt", version = VERSION_INFO.to_string())]
9+
pub struct Cli {}
10+
11+
#[cfg(test)]
12+
mod tests {
13+
use clap::CommandFactory;
14+
15+
use crate::{cli::Cli, version};
16+
17+
#[test]
18+
fn version_output() {
19+
let help = Cli::command()
20+
.version(
21+
version::VersionInfo {
22+
version: "0.12.5",
23+
short_hash: "abc123def",
24+
date: "2026-05-15",
25+
}
26+
.to_string(),
27+
)
28+
.render_version()
29+
.to_string();
30+
insta::assert_snapshot!(help)
31+
}
32+
33+
#[test]
34+
fn help_output() {
35+
let help = Cli::command().render_help().to_string();
36+
insta::assert_snapshot!(help)
37+
}
38+
}

basalt/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod app;
2+
pub mod cli;
23
pub mod command;
34
pub mod config;
45
pub mod explorer;
@@ -13,3 +14,4 @@ pub mod text_counts;
1314
pub mod toast;
1415
pub mod vault_selector;
1516
pub mod vault_selector_modal;
17+
pub mod version;

basalt/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
use clap::Parser;
12
use std::path::PathBuf;
23

34
use basalt_core::obsidian::{self, Error, Vault};
4-
use basalt_tui::app::App;
5+
use basalt_tui::{app::App, cli::Cli};
56

67
fn main() -> Result<(), Error> {
8+
let _cli = Cli::parse();
9+
710
let obsidian_config = obsidian::config::load().unwrap();
811
let vaults = obsidian_config.vaults();
912

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: basalt/src/cli.rs
3+
expression: help
4+
---
5+
Usage: basalt
6+
7+
Options:
8+
-h, --help Print help
9+
-V, --version Print version
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
source: basalt/src/cli.rs
3+
expression: help
4+
---
5+
basalt 0.12.5 (abc123def 2026-05-15)

basalt/src/version.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::fmt;
2+
3+
#[derive(Debug, Clone, PartialEq, Default)]
4+
pub struct VersionInfo {
5+
pub version: &'static str,
6+
pub short_hash: &'static str,
7+
pub date: &'static str,
8+
}
9+
10+
impl VersionInfo {
11+
pub const fn from_env() -> Self {
12+
Self {
13+
version: env!("BASALT_VERSION"),
14+
short_hash: env!("BASALT_COMMIT_SHORT_HASH"),
15+
date: env!("BASALT_COMMIT_DATE"),
16+
}
17+
}
18+
}
19+
20+
impl fmt::Display for VersionInfo {
21+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22+
write!(f, "{} ({} {})", self.version, self.short_hash, self.date)
23+
}
24+
}
25+
26+
#[test]
27+
fn version_format() {
28+
let info = VersionInfo {
29+
version: "0.12.5",
30+
short_hash: "abc123def",
31+
date: "2026-05-15",
32+
};
33+
assert_eq!(info.to_string(), "0.12.5 (abc123def 2026-05-15)");
34+
}

0 commit comments

Comments
 (0)