-
-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add config file support (#1119)
Refs #1472 Co-authored-by: Serhii Tereshchenko <[email protected]>
- Loading branch information
Showing
9 changed files
with
416 additions
and
259 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Config file handling | ||
|
||
use std::env; | ||
|
||
use serde::Deserialize; | ||
|
||
use crate::frame::Frame; | ||
|
||
use std::path::{Path, PathBuf}; | ||
|
||
const CONFIG_FILE: &str = "config.toml"; | ||
|
||
#[cfg(unix)] | ||
fn neovide_config_dir() -> PathBuf { | ||
let xdg_dirs = xdg::BaseDirectories::with_prefix("neovide").unwrap(); | ||
xdg_dirs.get_config_home() | ||
} | ||
|
||
#[cfg(windows)] | ||
fn neovide_config_dir() -> PathBuf { | ||
let mut path = dirs::config_dir().unwrap(); | ||
path.push("neovide"); | ||
path | ||
} | ||
|
||
pub fn config_path() -> PathBuf { | ||
let mut config_path = neovide_config_dir(); | ||
config_path.push(CONFIG_FILE); | ||
config_path | ||
} | ||
|
||
#[derive(Debug, Deserialize, Default)] | ||
pub struct Config { | ||
pub multigrid: Option<bool>, | ||
pub maximized: Option<bool>, | ||
pub vsync: Option<bool>, | ||
pub srgb: Option<bool>, | ||
pub idle: Option<bool>, | ||
pub neovim_bin: Option<PathBuf>, | ||
pub frame: Option<Frame>, | ||
} | ||
|
||
impl Config { | ||
/// Loads config from `config_path()` and writes it to env variables. | ||
pub fn init() { | ||
match Config::load_from_path(&config_path()) { | ||
Ok(config) => config.write_to_env(), | ||
Err(err) => { | ||
eprintln!("{err}"); | ||
} | ||
}; | ||
} | ||
|
||
fn write_to_env(&self) { | ||
if let Some(multigrid) = self.multigrid { | ||
env::set_var("NEOVIDE_MULTIGRID", multigrid.to_string()); | ||
} | ||
if let Some(maximized) = self.maximized { | ||
env::set_var("NEOVIDE_MAXIMIZED", maximized.to_string()); | ||
} | ||
if let Some(vsync) = self.vsync { | ||
env::set_var("NEOVIDE_VSYNC", vsync.to_string()); | ||
} | ||
if let Some(srgb) = self.srgb { | ||
env::set_var("NEOVIDE_SRGB", srgb.to_string()); | ||
} | ||
if let Some(idle) = self.idle { | ||
env::set_var("NEOVIDE_IDLE", idle.to_string()); | ||
} | ||
if let Some(frame) = self.frame { | ||
env::set_var("NEOVIDE_FRAME", frame.to_string()); | ||
} | ||
if let Some(neovim_bin) = &self.neovim_bin { | ||
env::set_var("NEOVIM_BIN", neovim_bin.to_string_lossy().to_string()); | ||
} | ||
} | ||
|
||
fn load_from_path(path: &Path) -> Result<Self, String> { | ||
let toml = std::fs::read_to_string(path).map_err(|e| { | ||
format!( | ||
"Error while trying to open config file {}:\n{}\nContinuing with default config.", | ||
path.to_string_lossy(), | ||
e | ||
) | ||
})?; | ||
let config = toml::from_str(&toml).map_err(|e| { | ||
format!( | ||
"Error while parsing config file {}:\n{}\nContinuing with default config.", | ||
path.to_string_lossy(), | ||
e | ||
) | ||
})?; | ||
Ok(config) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Config File | ||
|
||
Neovide also support configuration through a config file in [the toml format](https://toml.io). | ||
|
||
## Settings priority | ||
|
||
Settings specified in the config file override settings from the environment variables, but are | ||
overridden by command line arguments. | ||
|
||
## Location | ||
|
||
|Platform|Location| | ||
|--------|-----| | ||
|Linux|`$XDG_CONFIG_HOME/neovide/config.toml` or `$HOME/.config/neovide/config.toml`| | ||
|macOS|`$XDG_CONFIG_HOME/neovide/config.toml` or `$HOME/.config/neovide/config.toml`| | ||
|Windows|`{FOLDERID_RoamingAppData}/neovide/config.toml`| | ||
|
||
## Available settings | ||
|
||
Settings currently available in the config file with default values: | ||
|
||
```toml | ||
multigrid = false | ||
vsync = true | ||
maximized = false | ||
srgb = false | ||
idle = true | ||
neovim_bin = "/usr/bin/nvim" | ||
frame = "Full" | ||
``` | ||
|
||
See [Command Line Reference](command-line-reference.md) for details on what those settings do. |