on master help sign in
add a CLI flag for specifying config file location (#2666)
commit5d33dbacac3564c50b9f3a74cfef6a956c35dd80 authorMichael Davis <mcarsondavis@gmail.com> dateWed 3 Aug 2022, 23:05 (4 years ago) parent219d2c2
8 files changed+48−16
book/src/configuration.md+3 contrib/completion/hx.bash+1−1 contrib/completion/hx.fish+1 contrib/completion/hx.zsh+2 helix-loader/src/lib.rs+31−11 helix-term/src/application.rs+1−2 helix-term/src/args.rs+5 helix-term/src/main.rs+4−2
book/src/configuration.md+3
@@ -25,6 +25,9 @@ select = "underline"
2525hidden = false
2626```
2727
28+You may also specify a file to use for configuration with the `-c` or
29+`--config` CLI argument: `hx -c path/to/custom-config.toml`.
30+
2831## Editor
2932
3033### `[editor]` Section
contrib/completion/hx.bash+1−1
@@ -16,7 +16,7 @@ _hx() {
1616 COMPREPLY=($(compgen -W "$languages" -- $2))
1717 ;;
1818 *)
19− COMPREPLY=($(compgen -fd -W "-h --help --tutor -V --version -v -vv -vvv --health -g --grammar --vsplit --hsplit" -- $2))
19+ COMPREPLY=($(compgen -fd -W "-h --help --tutor -V --version -v -vv -vvv --health -g --grammar --vsplit --hsplit -c --config" -- $2))
2020 ;;
2121 esac
2222} && complete -F _hx hx
contrib/completion/hx.fish+1
@@ -11,3 +11,4 @@ complete -c hx -s v -o vv -o vvv -d "Increases logging verbosity"
1111complete -c hx -s V -l version -d "Prints version information"
1212complete -c hx -l vsplit -d "Splits all given files vertically into different windows"
1313complete -c hx -l hsplit -d "Splits all given files horizontally into different windows"
14+complete -c hx -s c -l config -d "Specifies a file to use for completion"
contrib/completion/hx.zsh+2
@@ -16,6 +16,8 @@ _hx() {
1616 "--grammar[Fetches or builds tree-sitter grammars]:action:->grammar" \
1717 "--vsplit[Splits all given files vertically into different windows]" \
1818 "--hsplit[Splits all given files horizontally into different windows]" \
19+ "-c[Specifies a file to use for configuration]" \
20+ "--config[Specifies a file to use for configuration]" \
1921 "*:file:_files"
2022
2123 case "$state" in
helix-loader/src/lib.rs+31−11
@@ -2,11 +2,28 @@ pub mod config;
22pub mod grammar;
33
44use etcetera::base_strategy::{choose_base_strategy, BaseStrategy};
5+use std::path::PathBuf;
56
6−pub static RUNTIME_DIR: once_cell::sync::Lazy<std::path::PathBuf> =
7− once_cell::sync::Lazy::new(runtime_dir);
7+pub static RUNTIME_DIR: once_cell::sync::Lazy<PathBuf> = once_cell::sync::Lazy::new(runtime_dir);
88
9−pub fn runtime_dir() -> std::path::PathBuf {
9+static CONFIG_FILE: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
10+
11+pub fn initialize_config_file(specified_file: Option<PathBuf>) {
12+ let config_file = specified_file.unwrap_or_else(|| {
13+ let config_dir = config_dir();
14+
15+ if !config_dir.exists() {
16+ std::fs::create_dir_all(&config_dir).ok();
17+ }
18+
19+ config_dir.join("config.toml")
20+ });
21+
22+ // We should only initialize this value once.
23+ CONFIG_FILE.set(config_file).ok();
24+}
25+
26+pub fn runtime_dir() -> PathBuf {
1027 if let Ok(dir) = std::env::var("HELIX_RUNTIME") {
1128 return dir.into();
1229 }
@@ -31,7 +48,7 @@ pub fn runtime_dir() -> std::path::PathBuf {
3148 .unwrap()
3249}
3350
34−pub fn config_dir() -> std::path::PathBuf {
51+pub fn config_dir() -> PathBuf {
3552 // TODO: allow env var override
3653 let strategy = choose_base_strategy().expect("Unable to find the config directory!");
3754 let mut path = strategy.config_dir();
@@ -39,7 +56,7 @@ pub fn config_dir() -> std::path::PathBuf {
3956 path
4057}
4158
42−pub fn local_config_dirs() -> Vec<std::path::PathBuf> {
59+pub fn local_config_dirs() -> Vec<PathBuf> {
4360 let directories = find_root_impl(None, &[".helix".to_string()])
4461 .into_iter()
4562 .map(|path| path.join(".helix"))
@@ -48,7 +65,7 @@ pub fn local_config_dirs() -> Vec<std::path::PathBuf> {
4865 directories
4966}
5067
51−pub fn cache_dir() -> std::path::PathBuf {
68+pub fn cache_dir() -> PathBuf {
5269 // TODO: allow env var override
5370 let strategy = choose_base_strategy().expect("Unable to find the config directory!");
5471 let mut path = strategy.cache_dir();
@@ -56,19 +73,22 @@ pub fn cache_dir() -> std::path::PathBuf {
5673 path
5774}
5875
59−pub fn config_file() -> std::path::PathBuf {
60− config_dir().join("config.toml")
76+pub fn config_file() -> PathBuf {
77+ CONFIG_FILE
78+ .get()
79+ .map(|path| path.to_path_buf())
80+ .unwrap_or_else(|| config_dir().join("config.toml"))
6181}
6282
63−pub fn lang_config_file() -> std::path::PathBuf {
83+pub fn lang_config_file() -> PathBuf {
6484 config_dir().join("languages.toml")
6585}
6686
67−pub fn log_file() -> std::path::PathBuf {
87+pub fn log_file() -> PathBuf {
6888 cache_dir().join("helix.log")
6989}
7090
71−pub fn find_root_impl(root: Option<&str>, root_markers: &[String]) -> Vec<std::path::PathBuf> {
91+pub fn find_root_impl(root: Option<&str>, root_markers: &[String]) -> Vec<PathBuf> {
7292 let current_dir = std::env::current_dir().expect("unable to determine current directory");
7393 let mut directories = Vec::new();
7494
helix-term/src/application.rs+1−2
@@ -89,9 +89,8 @@ impl Application {
8989
9090 use helix_view::editor::Action;
9191
92− let config_dir = helix_loader::config_dir();
9392 let theme_loader = std::sync::Arc::new(theme::Loader::new(
94− &config_dir,
93+ &helix_loader::config_dir(),
9594 &helix_loader::runtime_dir(),
9695 ));
9796
helix-term/src/args.rs+5
@@ -14,6 +14,7 @@ pub struct Args {
1414 pub build_grammars: bool,
1515 pub split: Option<Layout>,
1616 pub verbosity: u64,
17+ pub config_file: Option<PathBuf>,
1718 pub files: Vec<(PathBuf, Position)>,
1819}
1920
@@ -43,6 +44,10 @@ impl Args {
4344 anyhow::bail!("--grammar must be followed by either 'fetch' or 'build'")
4445 }
4546 },
47+ "-c" | "--config" => match argv.next().as_deref() {
48+ Some(path) => args.config_file = Some(path.into()),
49+ None => anyhow::bail!("--config must specify a path to read"),
50+ },
4651 arg if arg.starts_with("--") => {
4752 anyhow::bail!("unexpected double dash argument: {}", arg)
4853 }
helix-term/src/main.rs+4−2
@@ -64,6 +64,7 @@ FLAGS:
6464 --health [LANG] Checks for potential errors in editor setup
6565 If given, checks for config errors in language LANG
6666 -g, --grammar {{fetch|build}} Fetches or builds tree-sitter grammars listed in languages.toml
67+ -c, --config <file> Specifies a file to use for configuration
6768 -v Increases logging verbosity each use for up to 3 times
6869 (default file: {})
6970 -V, --version Prints version information
@@ -119,14 +120,15 @@ FLAGS:
119120 std::fs::create_dir_all(&config_dir).ok();
120121 }
121122
122− let config = match std::fs::read_to_string(config_dir.join("config.toml")) {
123+ helix_loader::initialize_config_file(args.config_file.clone());
124+
125+ let config = match std::fs::read_to_string(helix_loader::config_file()) {
123126 Ok(config) => toml::from_str(&config)
124127 .map(helix_term::keymap::merge_keys)
125128 .unwrap_or_else(|err| {
126129 eprintln!("Bad config: {}", err);
127130 eprintln!("Press <ENTER> to continue with default config");
128131 use std::io::Read;
129− // This waits for an enter press.
130132 let _ = std::io::stdin().read(&mut []);
131133 Config::default()
132134 }),