1
0
Fork 0
mirror of https://github.com/alacritty/alacritty.git synced 2024-11-11 13:51:01 -05:00

Move config path into Config type

This cleans up the Config::load API significantly. Several miscellaneous
comments were also added.
This commit is contained in:
Joe Wilm 2016-12-05 11:18:02 -08:00
parent 01bb10885b
commit 093ac43f80
2 changed files with 38 additions and 12 deletions

View file

@ -194,6 +194,9 @@ pub struct Config {
/// Bindings for the mouse
#[serde(default)]
mouse_bindings: Vec<MouseBinding>,
/// Path where config was loaded from
config_path: Option<PathBuf>,
}
impl Default for Config {
@ -206,6 +209,7 @@ impl Default for Config {
colors: Default::default(),
key_bindings: Vec::new(),
mouse_bindings: Vec::new(),
config_path: None,
}
}
}
@ -785,7 +789,7 @@ impl Config {
///
/// 1. `$HOME/.config/alacritty.yml`
/// 2. `$HOME/.alacritty.yml`
pub fn load() -> Result<(Config, PathBuf)> {
pub fn load() -> Result<Config> {
let home = env::var("HOME")?;
// First path
@ -849,10 +853,19 @@ impl Config {
self.render_timer
}
fn load_from<P: Into<PathBuf>>(path: P) -> Result<(Config, PathBuf)> {
pub fn path(&self) -> Option<&Path> {
self.config_path
.as_ref()
.map(|p| p.as_path())
}
fn load_from<P: Into<PathBuf>>(path: P) -> Result<Config> {
let path = path.into();
let raw = Config::read_file(path.as_path())?;
Ok((serde_yaml::from_str(&raw[..])?, path))
let mut config: Config = serde_yaml::from_str(&raw)?;
config.config_path = Some(path);
Ok(config)
}
fn read_file<P: AsRef<Path>>(path: P) -> Result<String> {
@ -1068,7 +1081,12 @@ pub trait OnConfigReload {
}
impl Watcher {
pub fn new<H: OnConfigReload + Send + 'static>(path: PathBuf, mut handler: H) -> Watcher {
pub fn new<H, P>(path: P, mut handler: H) -> Watcher
where H: OnConfigReload + Send + 'static,
P: Into<PathBuf>
{
let path = path.into();
Watcher(::util::thread::spawn_named("config watcher", move || {
let (tx, rx) = mpsc::channel();
let mut watcher = FileWatcher::new(tx).unwrap();
@ -1099,7 +1117,7 @@ impl Watcher {
path.map(|path| {
if path == config_path {
match Config::load() {
Ok((config, _)) => handler.on_config_reload(config),
Ok(config) => handler.on_config_reload(config),
Err(err) => err_println!("Ignoring invalid config: {}", err),
}
}

View file

@ -74,13 +74,13 @@ mod cli;
fn main() {
// Load configuration
let (config, config_path) = match Config::load() {
let config = match Config::load() {
// Error loading config
Err(err) => match err {
// Use default config when not found
config::Error::NotFound => {
err_println!("Config file not found; using defaults");
(Config::default(), None)
Config::default()
},
// If there's a problem with the config file, print an error
@ -89,23 +89,29 @@ fn main() {
},
// Successfully loaded config from file
Ok((config, path)) => (config, Some(path)),
Ok(config) => config
};
// Load command line options
let options = cli::Options::load();
// Extract some properties from config
let font = config.font();
let dpi = config.dpi();
let render_timer = config.render_timer();
// Create glutin window
let mut window = glutin::WindowBuilder::new()
.with_vsync()
.with_title("Alacritty")
.build().unwrap();
// Set the glutin window resize callback for this one window.
window.set_window_resize_callback(Some(window_resize_handler as fn(u32, u32)));
// load gl symbols
gl::load_with(|symbol| window.get_proc_address(symbol) as *const _);
// get window properties for initializing the other subsytems
let (width, height) = window.get_inner_size_pixels().unwrap();
let dpr = window.hidpi_factor();
@ -140,11 +146,14 @@ fn main() {
cache
};
// Need font metrics to resize the window properly. This suggests to me the
// font metrics should be computed before creating the window in the first
// place so that a resize is not needed.
let metrics = glyph_cache.font_metrics();
let cell_width = (metrics.average_advance + font.offset().x() as f64) as u32;
let cell_height = (metrics.line_height + font.offset().y() as f64) as u32;
// Resize window to be 80 col x 24 lines
// Resize window to specified dimensions
let width = cell_width * options.columns_u32() + 4;
let height = cell_height * options.lines_u32() + 4;
println!("set_inner_size: {} x {}", width, height);
@ -225,8 +234,8 @@ fn main() {
let (config_tx, config_rx) = mpsc::channel();
// create a config watcher when config is loaded from disk
let _config_reloader = config_path.map(|config_path| {
config::Watcher::new(config_path, ConfigHandler {
let _config_reloader = config.path().map(|path| {
config::Watcher::new(path, ConfigHandler {
tx: config_tx,
window: window.create_window_proxy(),
})
@ -273,7 +282,6 @@ struct ConfigHandler {
window: ::glutin::WindowProxy,
}
// TODO FIXME
impl config::OnConfigReload for ConfigHandler {
fn on_config_reload(&mut self, config: Config) {
if let Err(..) = self.tx.send(config) {