Skip to content

Commit

Permalink
feat: Add config file support (#1119)
Browse files Browse the repository at this point in the history
Refs  #1472
Co-authored-by: Serhii Tereshchenko <[email protected]>
  • Loading branch information
LoipesMas authored Jun 11, 2023
1 parent 13f6f8f commit ed58c49
Show file tree
Hide file tree
Showing 9 changed files with 416 additions and 259 deletions.
530 changes: 274 additions & 256 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ clap = { version = "4.0.8", features = ["cargo", "derive", "env"] }
copypasta = "0.8.1"
csscolorparser = "0.6.2"
derive-new = "0.5.9"
dirs = "4.0.0"
dirs = "5.0.0"
euclid = "0.22.7"
flexi_logger = { version = "0.22.3", default-features = false }
futures = "0.3.21"
Expand All @@ -47,6 +47,7 @@ swash = "0.1.8"
time = "0.3.9"
tokio = { version = "1.25.0", features = ["full"] }
tokio-util = { version = "0.7.4", features = ["compat"] }
toml = "0.7.3"
tracy-client-sys = { version = "0.19.0", optional = true }
unicode-segmentation = "1.9.0"
which = "4.2.5"
Expand Down
4 changes: 3 additions & 1 deletion src/frame.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use core::fmt;

use serde::Deserialize;

use clap::{builder::PossibleValue, ValueEnum};

// Options for the frame decorations
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Default, Deserialize)]
pub enum Frame {
#[default]
Full,
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use std::env::{self, args};

#[cfg(not(test))]
use flexi_logger::{Cleanup, Criterion, Duplicate, FileSpec, Logger, Naming};

use log::trace;

use backtrace::Backtrace;
Expand All @@ -58,6 +59,8 @@ pub use running_tracker::*;
#[cfg(target_os = "windows")]
pub use windows_utils::*;

use crate::settings::Config;

pub use profiling::startup_profiler;

const BACKTRACES_FILE: &str = "neovide_backtraces.log";
Expand Down Expand Up @@ -147,6 +150,8 @@ fn protected_main() {
#[cfg(target_os = "windows")]
windows_attach_to_console();

Config::init();

//Will exit if -h or -v
if let Err(err) = cmd_line::handle_command_line_arguments(args().collect()) {
eprintln!("{err}");
Expand Down
95 changes: 95 additions & 0 deletions src/settings/config.rs
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)
}
}
3 changes: 3 additions & 0 deletions src/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub use window_size::{
load_last_window_settings, save_window_size, PersistentWindowSettings, DEFAULT_WINDOW_GEOMETRY,
};

mod config;
pub use config::{config_path, Config};

lazy_static! {
pub static ref SETTINGS: Settings = Settings::new();
}
Expand Down
1 change: 1 addition & 0 deletions website/docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ______________________________________________________________________
- [Installation](installation.md)
- [Configuration](configuration.md)
- [Command Line Reference](command-line-reference.md)
- [Config File](config-file.md)
- [Editing w/ External Tools](editing-with-external-tools.md)
- [Troubleshooting](troubleshooting.md)
- [FAQ](faq.md)
Expand Down
2 changes: 1 addition & 1 deletion website/docs/command-line-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ buffers.
Note: Even if files are opened in tabs, they're buffers anyways. It's just about them being visible
or not.

## No VSync
### No VSync

```sh
--novsync, --vsync or $NEOVIDE_VSYNC=0|1
Expand Down
32 changes: 32 additions & 0 deletions website/docs/config-file.md
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.

0 comments on commit ed58c49

Please sign in to comment.