Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ dir: ~/.chira/md # source: dir で開くディレクトリ / :md :sync

- `:db <path>` — SQLite に切替(chira-ura のメモ。`notes` テーブルを読み書き)
- `:dir <path>` — md/txt ディレクトリに切替
- `:md [dir]` / `:sync [dir]` — 外部 `chira md` / `chira md import`
- `:md [dir]` / `:sync [dir]` — 外部 `chira md` / `chira md import`(外部 CLI `chira` が必要です: `cargo install chira-ura`)

## 操作早見表

Expand Down Expand Up @@ -96,8 +96,8 @@ dir: ~/.chira/md # source: dir で開くディレクトリ / :md :sync
- `:new`: 新規メモ作成
- `:next`: 次のファイルを開く
- `:prev`: 前のファイルを開く
- `:md [dir]`: 外部コマンド `chira md [dir]` を実行
- `:sync [dir]`: 外部コマンド `chira md import [dir]` を実行
- `:md [dir]`: 外部コマンド `chira md [dir]` を実行(要 `cargo install chira-ura`)
- `:sync [dir]`: 外部コマンド `chira md import [dir]` を実行(要 `cargo install chira-ura`)

未保存の変更がある間は、`:db` / `:dir` / `:new` / `:next` / `:prev` / `:e` はバッファを差し替えず、その理由をメッセージに表示します。`:db` / `:dir` の読み込みに失敗した場合は読み込み元を切り替えません。

Expand Down
97 changes: 97 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@ use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command as ProcessCommand;

pub const CHIRA_NOT_FOUND_MSG: &str =
"chira CLI is not installed or not in PATH. Install with: cargo install chira-ura";

/// Checks if the `chira` binary is available in PATH.
pub fn is_chira_available() -> bool {
let path_var = match std::env::var_os("PATH") {
Some(val) => val,
None => return false,
};

#[cfg(windows)]
let exe_names = ["chira.exe", "chira.cmd", "chira.bat", "chira"];
#[cfg(not(windows))]
let exe_names = ["chira"];

for dir in std::env::split_paths(&path_var) {
for name in &exe_names {
let candidate = dir.join(name);
if candidate.is_file() {
return true;
}
}
}

false
}

/// 読み込み元。db = chira-ura の SQLite、dir = md/txt ディレクトリ。
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Source {
Expand Down Expand Up @@ -1113,6 +1140,10 @@ impl App {

/// `:md [dir]` / `:sync [dir]` — 外部 chira の実行結果を報告する。
fn run_chira(&mut self, arg: Option<&str>, import: bool) -> CommandOutcome {
// PR #9: chira が PATH になければ実行前に案内して止まる。
if !is_chira_available() {
return CommandOutcome::Notice(CHIRA_NOT_FOUND_MSG.to_string());
}
let target_dir = match arg {
Some(p) => p.to_string(),
None => match self.root_dir.to_str() {
Expand Down Expand Up @@ -1144,6 +1175,10 @@ impl App {

let out = match output {
Ok(out) => out,
// PR #9: 実行時にも chira が見つからない場合は案内メッセージにする。
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
return CommandOutcome::Notice(CHIRA_NOT_FOUND_MSG.to_string());
}
Err(e) => return CommandOutcome::Failure(format!("failed to run chira: {e}")),
};
if !import {
Expand Down Expand Up @@ -1784,4 +1819,66 @@ mod tests {

let _ = fs::remove_dir_all(&dir);
}

#[test]
fn test_is_chira_available_returns_false_when_not_in_path() {
let original_path = std::env::var_os("PATH");
let temp_empty_dir = std::env::temp_dir().join("rum_empty_path_test");
let _ = fs::create_dir_all(&temp_empty_dir);

std::env::set_var("PATH", &temp_empty_dir);
let available = is_chira_available();
if let Some(orig) = original_path {
std::env::set_var("PATH", orig);
} else {
std::env::remove_var("PATH");
}
let _ = fs::remove_dir(&temp_empty_dir);

assert!(!available);
}

#[test]
fn test_execute_command_md_missing_chira() {
let original_path = std::env::var_os("PATH");
let temp_empty_dir = std::env::temp_dir().join("rum_empty_path_md_test");
let _ = fs::create_dir_all(&temp_empty_dir);

std::env::set_var("PATH", &temp_empty_dir);

let mut app = App::new(Source::Dir(temp_empty_dir.clone()), temp_empty_dir.clone())
.expect("app init");
app.execute_command("md");

if let Some(orig) = original_path {
std::env::set_var("PATH", orig);
} else {
std::env::remove_var("PATH");
}
let _ = fs::remove_dir_all(&temp_empty_dir);

assert_eq!(app.status_message, Some(CHIRA_NOT_FOUND_MSG.to_string()));
}

#[test]
fn test_execute_command_sync_missing_chira() {
let original_path = std::env::var_os("PATH");
let temp_empty_dir = std::env::temp_dir().join("rum_empty_path_sync_test");
let _ = fs::create_dir_all(&temp_empty_dir);

std::env::set_var("PATH", &temp_empty_dir);

let mut app = App::new(Source::Dir(temp_empty_dir.clone()), temp_empty_dir.clone())
.expect("app init");
app.execute_command("sync");

if let Some(orig) = original_path {
std::env::set_var("PATH", orig);
} else {
std::env::remove_var("PATH");
}
let _ = fs::remove_dir_all(&temp_empty_dir);

assert_eq!(app.status_message, Some(CHIRA_NOT_FOUND_MSG.to_string()));
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ rum — vim 風 TUI リーダ/エディタ
/ pattern, n N 検索
:w :q :wq :e :new :next :prev コマンド
:db <path> / :dir <path> 読み込み元を切替
:md [dir] / :sync [dir] chira md / chira md import
:md [dir] / :sync [dir] chira md / chira md import (要 chira CLI: cargo install chira-ura)
Ctrl-s 保存 / Ctrl-p Ctrl-n 前後のノート
";

Expand Down
Loading