Skip to content

Commit 5d33dba

Browse files
add a CLI flag for specifying config file location (#2666)
1 parent 219d2c2 commit 5d33dba

File tree

8 files changed

+48
-16
lines changed

8 files changed

+48
-16
lines changed

book/src/configuration.md

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ select = "underline"
2525
hidden = 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
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ complete -c hx -s v -o vv -o vvv -d "Increases logging verbosity"
1111
complete -c hx -s V -l version -d "Prints version information"
1212
complete -c hx -l vsplit -d "Splits all given files vertically into different windows"
1313
complete -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
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,28 @@ pub mod config;
22
pub mod grammar;
33

44
use 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,15 +48,15 @@ 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();
3855
path.push("helix");
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,27 +65,30 @@ 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();
5572
path.push("helix");
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
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -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
}),

0 commit comments

Comments
 (0)