Write default config when not found

This commit is contained in:
quininer kel 2017-01-06 12:56:05 +08:00 committed by Joe Wilm
parent 7d07b5a165
commit 2befe3681e
2 changed files with 15 additions and 2 deletions

View File

@ -6,11 +6,12 @@
use std::env;
use std::fmt;
use std::fs;
use std::io::{self, Read};
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::mpsc;
use std::ops::{Index, IndexMut};
use std::fs::File;
use ::Rgb;
use font::Size;
@ -821,6 +822,14 @@ impl Config {
Config::load_from(path)
}
pub fn write_defaults() -> io::Result<PathBuf> {
let path = ::xdg::BaseDirectories::new()
.map_err(|err| io::Error::new(io::ErrorKind::NotFound, ::std::error::Error::description(&err)))
.and_then(|p| p.place_config_file("alacritty.yml"))?;
File::create(&path)?.write_all(DEFAULT_ALACRITTY_CONFIG.as_bytes())?;
Ok(path)
}
/// Get list of colors
///
/// The ordering returned here is expected by the terminal. Colors are simply indexed in this

View File

@ -38,7 +38,11 @@ fn main() {
match err {
// Use default config when not found
config::Error::NotFound => {
err_println!("Config file not found; using defaults");
match Config::write_defaults() {
Ok(path) => err_println!("Config file not found; write defaults config to {:?}", path),
Err(err) => err_println!("Write defaults config failure: {}", err)
}
Config::default()
},