diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..cb212d6 --- /dev/null +++ b/src/config.rs @@ -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, + 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::>(); + 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>(&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")); + } + } +} diff --git a/src/main.rs b/src/main.rs index 7212e59..132e24d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,12 +9,15 @@ use std::process::{exit, Command}; use std::thread::sleep; use std::time::Duration; use std::fs::{self, File}; -use std::io::{Write, BufReader, BufRead, Read, self}; +use std::io::{Write, Read, self}; use std::net::TcpStream; use simple_input::input; use tempfile::NamedTempFile; +mod config; +use config::{Config}; + /* structs */ #[derive(Clone, Debug)] pub struct Link { @@ -25,18 +28,6 @@ pub struct Link { pub port: u16, pub selector: String, } -#[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 verbose: bool, -} - #[derive(Clone, Debug)] pub struct BrowserState { pub tmpfilename: String, @@ -67,58 +58,7 @@ impl BrowserState { parsed_port: 0, parsed_selector: String::new(), bookmarks: Vec::new(), - config: Config { - 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"), - verbose: false, - }, - } - } - - fn parse_config_line(&mut self, line: &str) { - let trimmed_line = line.trim_start(); - - let words = trimmed_line.split_whitespace().take(2).collect::>(); - match words[0] { - "start_uri" => self.config.start_uri = words[1].to_string(), - "cmd_text" => self.config.cmd_text = words[1].to_string(), - "cmd_browser" => self.config.cmd_browser = words[1].to_string(), - "cmd_image" => self.config.cmd_image = words[1].to_string(), - "cmd_player" => self.config.cmd_player = words[1].to_string(), - "color_prompt" => self.config.color_prompt = words[1].to_string(), - "color_selector" => self.config.color_selector = words[1].to_string(), - "verbose" => self.config.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_config>(&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_config_line(&line)) - } - - pub fn init_config(&mut self) { - /* read configs */ - self.load_config("/etc/cgorc"); /* ignore incomplete selectors */ - if let Some(dir) = dirs::home_dir() { - self.load_config(dir.join(".cgorc")); + config: Config::new(), } } @@ -170,7 +110,7 @@ impl BrowserState { fs::remove_file(&self.tmpfilename).expect("failed to delete temp file"); return false; } - tmpfile.keep(); + let _ = tmpfile.keep(); return true; } @@ -616,7 +556,9 @@ impl BrowserState { pub fn init(&mut self, argc: usize, argv: Vec) -> i32 { let mut i: usize = 1; /* copy defaults */ - self.init_config(); + self.config.init(); + self.bookmarks = self.config.bookmarks.clone(); + let mut uri: String = self.config.start_uri.clone(); /* parse command line */ while i < argc {