extract config stuff
parent
59fc5a09e5
commit
d085009a70
@ -0,0 +1,75 @@
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::process::exit;
|
||||
use std::path::Path;
|
||||
use std::fs::File;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Config {
|
||||
pub start_uri: String,
|
||||
pub cmd_text: String,
|
||||
pub cmd_image: String,
|
||||
pub cmd_browser: String,
|
||||
pub cmd_player: String,
|
||||
pub color_prompt: String,
|
||||
pub color_selector: String,
|
||||
pub bookmarks: Vec<String>,
|
||||
pub verbose: bool,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
start_uri: String::from("gopher://gopher.floodgap.com:70"),
|
||||
cmd_text: String::from("less"),
|
||||
cmd_image: String::from("display"),
|
||||
cmd_browser: String::from("firefox"),
|
||||
cmd_player: String::from("mplayer"),
|
||||
color_prompt: String::from("1;34"),
|
||||
color_selector: String::from("1;32"),
|
||||
bookmarks: Vec::new(),
|
||||
verbose: false,
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_line(&mut self, line: &str) {
|
||||
let trimmed_line = line.trim_start();
|
||||
|
||||
let words = trimmed_line.split_whitespace().take(2).collect::<Vec<_>>();
|
||||
match words[0] {
|
||||
"start_uri" => self.start_uri = words[1].to_string(),
|
||||
"cmd_text" => self.cmd_text = words[1].to_string(),
|
||||
"cmd_browser" => self.cmd_browser = words[1].to_string(),
|
||||
"cmd_image" => self.cmd_image = words[1].to_string(),
|
||||
"cmd_player" => self.cmd_player = words[1].to_string(),
|
||||
"color_prompt" => self.color_prompt = words[1].to_string(),
|
||||
"color_selector" => self.color_selector = words[1].to_string(),
|
||||
"verbose" => self.verbose = words[1].parse().unwrap_or_default(),
|
||||
x if x.starts_with("bookmark") => self.bookmarks.push(words[1].to_string()),
|
||||
x => {
|
||||
eprintln!("invalid key in config: {}", x);
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn load<P: AsRef<Path>>(&mut self, filename: P) {
|
||||
let file = match File::open(filename) {
|
||||
Ok(f) => BufReader::new(f),
|
||||
Err(_) => {
|
||||
return;
|
||||
}
|
||||
};
|
||||
file.lines()
|
||||
.filter_map(|x| x.ok())
|
||||
.filter(|x| !x.starts_with('#'))
|
||||
.for_each(|line| self.parse_line(&line))
|
||||
}
|
||||
|
||||
pub fn init(&mut self) {
|
||||
/* read configs */
|
||||
self.load("/etc/cgorc"); /* ignore incomplete selectors */
|
||||
if let Some(dir) = dirs::home_dir() {
|
||||
self.load(dir.join(".cgorc"));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue