diff options
author | Kirill Chibisov <wchibisovkirill@gmail.com> | 2019-10-06 13:47:20 +0300 |
---|---|---|
committer | Christian Duerr <contact@christianduerr.com> | 2019-10-06 12:47:20 +0200 |
commit | 24651a6144e5071f0a72d991734a9b380255156e (patch) | |
tree | 92135f2f51abb709229dd94c29e0331cec01bf42 /alacritty/src | |
parent | 729eef0c933831bccfeac6a355bdb410787fbe5f (diff) | |
download | r-alacritty-24651a6144e5071f0a72d991734a9b380255156e.tar.gz r-alacritty-24651a6144e5071f0a72d991734a9b380255156e.tar.bz2 r-alacritty-24651a6144e5071f0a72d991734a9b380255156e.zip |
Remove automatic config generation
Fixes #2818.
Diffstat (limited to 'alacritty/src')
-rw-r--r-- | alacritty/src/cli.rs | 18 | ||||
-rw-r--r-- | alacritty/src/config/mod.rs | 94 | ||||
-rw-r--r-- | alacritty/src/config/test.rs | 24 | ||||
-rw-r--r-- | alacritty/src/config/ui_config.rs | 10 | ||||
-rw-r--r-- | alacritty/src/main.rs | 16 |
5 files changed, 58 insertions, 104 deletions
diff --git a/alacritty/src/cli.rs b/alacritty/src/cli.rs index d5eb12d7..aabcdee3 100644 --- a/alacritty/src/cli.rs +++ b/alacritty/src/cli.rs @@ -12,9 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::borrow::Cow; use std::cmp::max; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg}; use log::{self, LevelFilter}; @@ -242,8 +241,8 @@ impl Options { options } - pub fn config_path(&self) -> Option<Cow<'_, Path>> { - self.config.as_ref().map(|p| Cow::Borrowed(p.as_path())) + pub fn config_path(&self) -> Option<PathBuf> { + self.config.clone() } pub fn into_config(self, mut config: Config) -> Config { @@ -284,15 +283,12 @@ impl Options { #[cfg(test)] mod test { - use alacritty_terminal::config::DEFAULT_ALACRITTY_CONFIG; - use crate::cli::Options; use crate::config::Config; #[test] fn dynamic_title_ignoring_options_by_default() { - let config: Config = - ::serde_yaml::from_str(DEFAULT_ALACRITTY_CONFIG).expect("deserialize config"); + let config = Config::default(); let old_dynamic_title = config.dynamic_title(); let config = Options::default().into_config(config); @@ -302,8 +298,7 @@ mod test { #[test] fn dynamic_title_overridden_by_options() { - let config: Config = - ::serde_yaml::from_str(DEFAULT_ALACRITTY_CONFIG).expect("deserialize config"); + let config = Config::default(); let mut options = Options::default(); options.title = Some("foo".to_owned()); @@ -314,8 +309,7 @@ mod test { #[test] fn dynamic_title_overridden_by_config() { - let mut config: Config = - ::serde_yaml::from_str(DEFAULT_ALACRITTY_CONFIG).expect("deserialize config"); + let mut config = Config::default(); config.window.title = Some("foo".to_owned()); let config = Options::default().into_config(config); diff --git a/alacritty/src/config/mod.rs b/alacritty/src/config/mod.rs index fe0ee7af..8b881fca 100644 --- a/alacritty/src/config/mod.rs +++ b/alacritty/src/config/mod.rs @@ -1,8 +1,6 @@ -use std::borrow::Cow; use std::env; -use std::fs::File; -use std::io::{self, Read, Write}; -use std::path::{Path, PathBuf}; +use std::io; +use std::path::PathBuf; #[cfg(windows)] use dirs; @@ -11,15 +9,11 @@ use serde_yaml; #[cfg(not(windows))] use xdg; -use alacritty_terminal::config::{ - Config as TermConfig, DEFAULT_ALACRITTY_CONFIG, LOG_TARGET_CONFIG, -}; +use alacritty_terminal::config::{Config as TermConfig, LOG_TARGET_CONFIG}; mod bindings; pub mod monitor; mod mouse; -#[cfg(test)] -mod test; mod ui_config; pub use crate::config::bindings::{Action, Binding, Key, RelaxedEq}; @@ -48,7 +42,7 @@ pub enum Error { Yaml(serde_yaml::Error), } -impl ::std::error::Error for Error { +impl std::error::Error for Error { fn cause(&self) -> Option<&dyn (::std::error::Error)> { match *self { Error::NotFound => None, @@ -68,7 +62,7 @@ impl ::std::error::Error for Error { } } -impl ::std::fmt::Display for Error { +impl std::fmt::Display for Error { fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { match *self { Error::NotFound => write!(f, "{}", ::std::error::Error::description(self)), @@ -111,7 +105,7 @@ impl From<serde_yaml::Error> for Error { /// 3. $HOME/.config/alacritty/alacritty.yml /// 4. $HOME/.alacritty.yml #[cfg(not(windows))] -pub fn installed_config<'a>() -> Option<Cow<'a, Path>> { +pub fn installed_config() -> Option<PathBuf> { // Try using XDG location by default xdg::BaseDirectories::with_prefix("alacritty") .ok() @@ -136,41 +130,11 @@ pub fn installed_config<'a>() -> Option<Cow<'a, Path>> { } None }) - .map(Into::into) -} - -#[cfg(windows)] -pub fn installed_config<'a>() -> Option<Cow<'a, Path>> { - dirs::config_dir() - .map(|path| path.join("alacritty\\alacritty.yml")) - .filter(|new| new.exists()) - .map(Cow::from) -} - -#[cfg(not(windows))] -pub fn write_defaults() -> io::Result<Cow<'static, Path>> { - let path = xdg::BaseDirectories::with_prefix("alacritty") - .map_err(|err| io::Error::new(io::ErrorKind::NotFound, err.to_string().as_str())) - .and_then(|p| p.place_config_file("alacritty.yml"))?; - - File::create(&path)?.write_all(DEFAULT_ALACRITTY_CONFIG.as_bytes())?; - - Ok(path.into()) } #[cfg(windows)] -pub fn write_defaults() -> io::Result<Cow<'static, Path>> { - let mut path = dirs::config_dir().ok_or_else(|| { - io::Error::new(io::ErrorKind::NotFound, "Couldn't find profile directory") - })?; - - path = path.join("alacritty/alacritty.yml"); - - std::fs::create_dir_all(path.parent().unwrap())?; - - File::create(&path)?.write_all(DEFAULT_ALACRITTY_CONFIG.as_bytes())?; - - Ok(path.into()) +pub fn installed_config() -> Option<PathBuf> { + dirs::config_dir().map(|path| path.join("alacritty\\alacritty.yml")).filter(|new| new.exists()) } pub fn load_from(path: PathBuf) -> Config { @@ -190,24 +154,31 @@ pub fn reload_from(path: &PathBuf) -> Result<Config> { } fn read_config(path: &PathBuf) -> Result<Config> { - let mut contents = String::new(); - File::open(path)?.read_to_string(&mut contents)?; + let mut contents = std::fs::read_to_string(path)?; // Remove UTF-8 BOM if contents.chars().nth(0) == Some('\u{FEFF}') { contents = contents.split_off(3); } - // Prevent parsing error with empty string - if contents.is_empty() { - return Ok(Config::default()); - } - - let config = serde_yaml::from_str(&contents)?; - - print_deprecation_warnings(&config); + parse_config(&contents) +} - Ok(config) +fn parse_config(contents: &str) -> Result<Config> { + match serde_yaml::from_str(&contents) { + Err(error) => { + // Prevent parsing error with an empty string and commented out file. + if std::error::Error::description(&error) == "EOF while parsing a value" { + Ok(Config::default()) + } else { + Err(Error::Yaml(error)) + } + }, + Ok(config) => { + print_deprecation_warnings(&config); + Ok(config) + }, + } } fn print_deprecation_warnings(config: &Config) { @@ -232,3 +203,16 @@ fn print_deprecation_warnings(config: &Config) { ); } } + +#[cfg(test)] +mod test { + static DEFAULT_ALACRITTY_CONFIG: &str = + include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../alacritty.yml")); + + use super::Config; + + #[test] + fn config_read_eof() { + assert_eq!(super::parse_config(DEFAULT_ALACRITTY_CONFIG).unwrap(), Config::default()); + } +} diff --git a/alacritty/src/config/test.rs b/alacritty/src/config/test.rs deleted file mode 100644 index 8da6cef5..00000000 --- a/alacritty/src/config/test.rs +++ /dev/null @@ -1,24 +0,0 @@ -use alacritty_terminal::config::DEFAULT_ALACRITTY_CONFIG; - -use crate::config::Config; - -#[test] -fn parse_config() { - let config: Config = - ::serde_yaml::from_str(DEFAULT_ALACRITTY_CONFIG).expect("deserialize config"); - - // Sanity check that mouse bindings are being parsed - assert!(!config.ui_config.mouse_bindings.is_empty()); - - // Sanity check that key bindings are being parsed - assert!(!config.ui_config.key_bindings.is_empty()); -} - -#[test] -fn default_match_empty() { - let default = Config::default(); - - let empty = serde_yaml::from_str("key: val\n").unwrap(); - - assert_eq!(default, empty); -} diff --git a/alacritty/src/config/ui_config.rs b/alacritty/src/config/ui_config.rs index 6230c5bb..d7a477a0 100644 --- a/alacritty/src/config/ui_config.rs +++ b/alacritty/src/config/ui_config.rs @@ -19,6 +19,16 @@ pub struct UIConfig { pub mouse_bindings: Vec<MouseBinding>, } +impl Default for UIConfig { + fn default() -> Self { + UIConfig { + mouse: Mouse::default(), + key_bindings: default_key_bindings(), + mouse_bindings: default_mouse_bindings(), + } + } +} + fn default_key_bindings() -> Vec<KeyBinding> { bindings::default_key_bindings() } diff --git a/alacritty/src/main.rs b/alacritty/src/main.rs index 146709fd..cbd00443 100644 --- a/alacritty/src/main.rs +++ b/alacritty/src/main.rs @@ -34,7 +34,7 @@ use std::sync::Arc; #[cfg(target_os = "macos")] use dirs; use glutin::event_loop::EventLoop as GlutinEventLoop; -use log::{error, info}; +use log::info; #[cfg(windows)] use winapi::um::wincon::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS}; @@ -85,18 +85,8 @@ fn main() { .expect("Unable to initialize logger"); // Load configuration file - // If the file is a command line argument, we won't write a generated default file - let config_path = options - .config_path() - .or_else(config::installed_config) - .or_else(|| config::write_defaults().ok()) - .map(|path| path.to_path_buf()); - let config = if let Some(path) = config_path { - config::load_from(path) - } else { - error!("Unable to write the default config"); - Config::default() - }; + let config_path = options.config_path().or_else(config::installed_config); + let config = config_path.map(config::load_from).unwrap_or_else(Config::default); let config = options.into_config(config); // Update the log level from config |