diff options
author | quininer kel <quininer@live.com> | 2017-01-06 12:56:05 +0800 |
---|---|---|
committer | Joe Wilm <joe@jwilm.com> | 2017-01-06 11:17:33 -0800 |
commit | 2befe3681e7e813e704fbbf75a520e04bf049d36 (patch) | |
tree | b9e0d11ffeafb3a0c8607206acdee54d07cf27f1 /src | |
parent | 7d07b5a1655dc9d1cda62f1dd3005b966c0fa76a (diff) |
Write default config when not found
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 11 | ||||
-rw-r--r-- | src/main.rs | 6 |
2 files changed, 15 insertions, 2 deletions
diff --git a/src/config.rs b/src/config.rs index 5daa4455..bd073371 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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 diff --git a/src/main.rs b/src/main.rs index 3328ab59..ce323ac1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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() }, |