1
0
Fork 0
mirror of https://github.com/alacritty/alacritty.git synced 2024-11-25 14:05:41 -05:00

Move config loading logic to config.rs

Config loading was complexity that shouldn't be in main.
This commit is contained in:
Joe Wilm 2017-07-07 10:35:33 -07:00
parent 150d5bf98f
commit c67f0ddf34
2 changed files with 47 additions and 37 deletions

View file

@ -1123,7 +1123,43 @@ impl Config {
self.hide_cursor_when_typing 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 path = path.into();
let raw = Config::read_file(path.as_path())?; let raw = Config::read_file(path.as_path())?;
let mut config: Config = serde_yaml::from_str(&raw)?; let mut config: Config = serde_yaml::from_str(&raw)?;

View file

@ -37,55 +37,29 @@ use alacritty::tty::{self, process_should_exit};
use alacritty::util::fmt::Red; use alacritty::util::fmt::Red;
fn main() { fn main() {
// Load command line options and config
let options = cli::Options::load();
let config = load_config(&options);
// Run alacritty // Run alacritty
if let Err(err) = run(config, options) { if let Err(err) = run() {
die!("Alacritty encountered an unrecoverable error:\n\n\t{}\n", Red(err)); die!("Encountered an unrecoverable error:\n\n\t{}\n", Red(err));
} }
info!("Goodbye."); 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 /// Run Alacritty
/// ///
/// Creates a window, the terminal state, pty, I/O event loop, input processor, /// Creates a window, the terminal state, pty, I/O event loop, input processor,
/// config change monitor, and runs the main display loop. /// config change monitor, and runs the main display loop.
fn run(mut config: Config, options: cli::Options) -> Result<(), Box<Error>> { fn run() -> Result<(), Box<Error>> {
// Initialize the logger first as to capture output from other subsystems // Load command line options
logging::initialize(&options)?; let options = cli::Options::load();
// Initialize the logger ASAP
logging::initialize(&options)?;
info!("Welcome to Alacritty."); info!("Welcome to Alacritty.");
// Load config
let mut config = Config::load(&options);
// Create a display. // Create a display.
// //
// The display manages a window and can draw the terminal // The display manages a window and can draw the terminal