Move config loading logic to config.rs
Config loading was complexity that shouldn't be in main.
diff --git a/src/config.rs b/src/config.rs
index 7734e7a..957557f 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1123,7 +1123,43 @@
self.hide_cursor_when_typing
}
- pub fn load_from<P: Into<PathBuf>>(path: P) -> Result<Config> {
+ /// Load configuration
+ ///
+ /// If a configuration file is given as a command line argument we don't
+ /// generate a default file. If an empty configuration file is given, i.e.
+ /// /dev/null, we load the compiled-in defaults.
+ pub fn load(options: &::cli::Options) -> Config {
+ let config_path = options.config_path()
+ .or_else(|| Config::installed_config())
+ .unwrap_or_else(|| {
+ Config::write_defaults()
+ .unwrap_or_else(|err| die!("Write defaults config failure: {}", err))
+ });
+
+ Config::load_from(&*config_path)
+ .map(|config| {
+ if let Some(path) = config.path().as_ref() {
+ info!("Config loaded from {}", path.display());
+ }
+
+ config
+ })
+ .unwrap_or_else(|err| {
+ use self::Error::*;
+ match err {
+ NotFound => {
+ die!("Config file not found at: {}", config_path.display());
+ },
+ Empty => {
+ err_println!("Empty config; Loading defaults");
+ Config::default()
+ },
+ _ => die!("{}", err),
+ }
+ })
+ }
+
+ fn load_from<P: Into<PathBuf>>(path: P) -> Result<Config> {
let path = path.into();
let raw = Config::read_file(path.as_path())?;
let mut config: Config = serde_yaml::from_str(&raw)?;
diff --git a/src/main.rs b/src/main.rs
index f2a5109..fcff923 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -37,55 +37,29 @@
use alacritty::util::fmt::Red;
fn main() {
- // Load command line options and config
- let options = cli::Options::load();
- let config = load_config(&options);
-
// Run alacritty
- if let Err(err) = run(config, options) {
- die!("Alacritty encountered an unrecoverable error:\n\n\t{}\n", Red(err));
+ if let Err(err) = run() {
+ die!("Encountered an unrecoverable error:\n\n\t{}\n", Red(err));
}
info!("Goodbye.");
}
-/// Load configuration
-///
-/// If a configuration file is given as a command line argument we don't
-/// generate a default file. If an empty configuration file is given, i.e.
-/// /dev/null, we load the compiled-in defaults.
-fn load_config(options: &cli::Options) -> Config {
- let config_path = options.config_path()
- .or_else(|| Config::installed_config())
- .unwrap_or_else(|| {
- Config::write_defaults()
- .unwrap_or_else(|err| die!("Write defaults config failure: {}", err))
- });
-
- Config::load_from(&*config_path).unwrap_or_else(|err| {
- match err {
- config::Error::NotFound => {
- die!("Config file not found at: {}", config_path.display());
- },
- config::Error::Empty => {
- err_println!("Empty config; Loading defaults");
- Config::default()
- },
- _ => die!("{}", err),
- }
- })
-}
-
/// Run Alacritty
///
/// Creates a window, the terminal state, pty, I/O event loop, input processor,
/// config change monitor, and runs the main display loop.
-fn run(mut config: Config, options: cli::Options) -> Result<(), Box<Error>> {
- // Initialize the logger first as to capture output from other subsystems
- logging::initialize(&options)?;
+fn run() -> Result<(), Box<Error>> {
+ // Load command line options
+ let options = cli::Options::load();
+ // Initialize the logger ASAP
+ logging::initialize(&options)?;
info!("Welcome to Alacritty.");
+ // Load config
+ let mut config = Config::load(&options);
+
// Create a display.
//
// The display manages a window and can draw the terminal