aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2017-07-07 10:35:33 -0700
committerJoe Wilm <joe@jwilm.com>2017-07-07 10:35:33 -0700
commitc67f0ddf34b630c538caf6b6d4f6e801a6c0bcb6 (patch)
treeaffe4f14e8e67ce79b917e46523671ae01fc4aad
parent150d5bf98f140d305853528a04fe9ef5a17d8474 (diff)
downloadalacritty-c67f0ddf34b630c538caf6b6d4f6e801a6c0bcb6.tar.gz
alacritty-c67f0ddf34b630c538caf6b6d4f6e801a6c0bcb6.zip
Move config loading logic to config.rs
Config loading was complexity that shouldn't be in main.
-rw-r--r--src/config.rs38
-rw-r--r--src/main.rs46
2 files changed, 47 insertions, 37 deletions
diff --git a/src/config.rs b/src/config.rs
index 7734e7af..957557f9 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1123,7 +1123,43 @@ impl Config {
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 f2a5109b..fcff9239 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -37,55 +37,29 @@ use alacritty::tty::{self, process_should_exit};
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