Skip to content

Commit 1d0ca0a

Browse files
committed
add a CLI flag for specifying config file location
1 parent dfd0754 commit 1d0ca0a

File tree

8 files changed

+48
-20
lines changed

8 files changed

+48
-20
lines changed

book/src/configuration.md

Lines changed: 3 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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" -- $2))
19+
COMPREPLY=($(compgen -fd -W "-h --help --tutor -V --version -v -vv -vvv --health -g --grammar -c --config" -- $2))
2020
;;
2121
esac
2222
} && complete -F _hx hx

contrib/completion/hx.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ complete -c hx -l health -x -a "$langs" -d "Checks for errors in editor setup"
99
complete -c hx -s g -l grammar -x -a "fetch build" -d "Fetches or builds tree-sitter grammars"
1010
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"
12+
complete -c hx -s c -l config -d "Specifies a file to use for completion"
1213

contrib/completion/hx.zsh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ _hx() {
1414
"--health[Checks for errors in editor setup]:language:->health" \
1515
"-g[Fetches or builds tree-sitter grammars]:action:->grammar" \
1616
"--grammar[Fetches or builds tree-sitter grammars]:action:->grammar" \
17+
"-c[Specifies a file to use for configuration]" \
18+
"--config[Specifies a file to use for configuration]" \
1719
"*:file:_files"
1820

1921
case "$state" in

helix-loader/src/lib.rs

Lines changed: 32 additions & 12 deletions
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
}
@@ -19,7 +36,7 @@ pub fn runtime_dir() -> std::path::PathBuf {
1936

2037
if let Ok(dir) = std::env::var("CARGO_MANIFEST_DIR") {
2138
// this is the directory of the crate being run by cargo, we need the workspace path so we take the parent
22-
return std::path::PathBuf::from(dir).parent().unwrap().join(RT_DIR);
39+
return PathBuf::from(dir).parent().unwrap().join(RT_DIR);
2340
}
2441

2542
// fallback to location of the executable being run
@@ -29,15 +46,15 @@ pub fn runtime_dir() -> std::path::PathBuf {
2946
.unwrap()
3047
}
3148

32-
pub fn config_dir() -> std::path::PathBuf {
49+
pub fn config_dir() -> PathBuf {
3350
// TODO: allow env var override
3451
let strategy = choose_base_strategy().expect("Unable to find the config directory!");
3552
let mut path = strategy.config_dir();
3653
path.push("helix");
3754
path
3855
}
3956

40-
pub fn local_config_dirs() -> Vec<std::path::PathBuf> {
57+
pub fn local_config_dirs() -> Vec<PathBuf> {
4158
let directories = find_root_impl(None, &[".helix".to_string()])
4259
.into_iter()
4360
.map(|path| path.join(".helix"))
@@ -46,27 +63,30 @@ pub fn local_config_dirs() -> Vec<std::path::PathBuf> {
4663
directories
4764
}
4865

49-
pub fn cache_dir() -> std::path::PathBuf {
66+
pub fn cache_dir() -> PathBuf {
5067
// TODO: allow env var override
5168
let strategy = choose_base_strategy().expect("Unable to find the config directory!");
5269
let mut path = strategy.cache_dir();
5370
path.push("helix");
5471
path
5572
}
5673

57-
pub fn config_file() -> std::path::PathBuf {
58-
config_dir().join("config.toml")
74+
pub fn config_file() -> PathBuf {
75+
CONFIG_FILE
76+
.get()
77+
.map(|path| path.to_path_buf())
78+
.unwrap_or_else(|| config_dir().join("config.toml"))
5979
}
6080

61-
pub fn lang_config_file() -> std::path::PathBuf {
81+
pub fn lang_config_file() -> PathBuf {
6282
config_dir().join("languages.toml")
6383
}
6484

65-
pub fn log_file() -> std::path::PathBuf {
85+
pub fn log_file() -> PathBuf {
6686
cache_dir().join("helix.log")
6787
}
6888

69-
pub fn find_root_impl(root: Option<&str>, root_markers: &[String]) -> Vec<std::path::PathBuf> {
89+
pub fn find_root_impl(root: Option<&str>, root_markers: &[String]) -> Vec<PathBuf> {
7090
let current_dir = std::env::current_dir().expect("unable to determine current directory");
7191
let mut directories = Vec::new();
7292

helix-term/src/application.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,15 @@ impl Application {
5959
pub fn new(args: Args) -> Result<Self, Error> {
6060
use helix_view::editor::Action;
6161

62-
let config_dir = helix_loader::config_dir();
63-
if !config_dir.exists() {
64-
std::fs::create_dir_all(&config_dir).ok();
65-
}
62+
helix_loader::initialize_config_file(args.config_file);
6663

67-
let config = match std::fs::read_to_string(config_dir.join("config.toml")) {
64+
let config = match std::fs::read_to_string(helix_loader::config_file()) {
6865
Ok(config) => toml::from_str(&config)
6966
.map(crate::keymap::merge_keys)
7067
.unwrap_or_else(|err| {
7168
eprintln!("Bad config: {}", err);
7269
eprintln!("Press <ENTER> to continue with default config");
7370
use std::io::Read;
74-
// This waits for an enter press.
7571
let _ = std::io::stdin().read(&mut []);
7672
Config::default()
7773
}),
@@ -80,7 +76,7 @@ impl Application {
8076
};
8177

8278
let theme_loader = std::sync::Arc::new(theme::Loader::new(
83-
&config_dir,
79+
&helix_loader::config_dir(),
8480
&helix_loader::runtime_dir(),
8581
));
8682

helix-term/src/args.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct Args {
1212
pub fetch_grammars: bool,
1313
pub build_grammars: bool,
1414
pub verbosity: u64,
15+
pub config_file: Option<PathBuf>,
1516
pub files: Vec<(PathBuf, Position)>,
1617
}
1718

@@ -39,6 +40,10 @@ impl Args {
3940
anyhow::bail!("--grammar must be followed by either 'fetch' or 'build'")
4041
}
4142
},
43+
"-c" | "--config" => match argv.next().as_deref() {
44+
Some(path) => args.config_file = Some(path.into()),
45+
None => anyhow::bail!("--config must specify a path to read"),
46+
},
4247
arg if arg.starts_with("--") => {
4348
anyhow::bail!("unexpected double dash argument: {}", arg)
4449
}

helix-term/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ FLAGS:
6262
--health [LANG] Checks for potential errors in editor setup
6363
If given, checks for config errors in language LANG
6464
-g, --grammar {{fetch|build}} Fetches or builds tree-sitter grammars listed in languages.toml
65+
-c, --config <file> Specifies a file to use for configuration
6566
-v Increases logging verbosity each use for up to 3 times
6667
(default file: {})
6768
-V, --version Prints version information

0 commit comments

Comments
 (0)