separated bookmarks from config, clean up in main pending
This commit is contained in:
@@ -0,0 +1,121 @@
|
|||||||
|
//! handle the config file and bookmarks stored
|
||||||
|
//! in said config file
|
||||||
|
|
||||||
|
use std::collections::HashMap;
|
||||||
|
use std::env::var;
|
||||||
|
use std::fs;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{Error, Result};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use crate::{RESET_SEQ, STYLES};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Bookmarks {
|
||||||
|
conf_dir: PathBuf,
|
||||||
|
bookmarks: HashMap<String, PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
const BOOKMARK_FILE_NAME: &str = "bookmarks.conf";
|
||||||
|
|
||||||
|
/// generates and populates a new instance of Config
|
||||||
|
pub fn new() -> Result<Self> {
|
||||||
|
let mut bookmarks = Config {
|
||||||
|
conf_dir: PathBuf::new(),
|
||||||
|
bookmarks: HashMap::<String, PathBuf>::new(),
|
||||||
|
};
|
||||||
|
// get home directory path
|
||||||
|
let home_dir = match var("HOME") {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
|
};
|
||||||
|
// create PathBuf object from home dir path
|
||||||
|
bookmarks.conf_dir = match PathBuf::from_str(&home_dir) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
|
};
|
||||||
|
// expand home directory path to get configuration directory path
|
||||||
|
bookmarks.conf_dir.push(".config/navigate/");
|
||||||
|
bookmarks.build_bookmarks()?;
|
||||||
|
|
||||||
|
Ok(bookmarks)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// reads and parses the bookmarks file
|
||||||
|
fn build_bookmarks(&mut self) -> Result<()> {
|
||||||
|
let mut bookmark_file = self.conf_dir.clone();
|
||||||
|
bookmark_file.push(Self::BOOKMARK_FILE_NAME);
|
||||||
|
|
||||||
|
if !bookmark_file.is_file() {
|
||||||
|
_ = File::create(bookmark_file.clone())?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let bookmarks = fs::read_to_string(bookmark_file)?;
|
||||||
|
let bookmarks = bookmarks.split("\n");
|
||||||
|
for entry in bookmarks {
|
||||||
|
let tokens: Vec<&str> = entry.split("=").collect();
|
||||||
|
if tokens.len() != 2 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let key: String = String::from(tokens[0]);
|
||||||
|
let path = match PathBuf::from_str(tokens[1]) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(err) => return Err(Error::other(err.to_string())),
|
||||||
|
};
|
||||||
|
if !path.is_dir() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
self.bookmarks.insert(key, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// writes the bookmarks file
|
||||||
|
fn write_bookmark_file(&self) -> Result<()> {
|
||||||
|
let mut file_content = String::new();
|
||||||
|
for (mark, path) in self.bookmarks.iter() {
|
||||||
|
file_content.push_str(&format!("{}={}\n", mark, path.to_str().unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut path = self.conf_dir.clone();
|
||||||
|
path.push(Self::BOOKMARK_FILE_NAME);
|
||||||
|
|
||||||
|
fs::write(path, file_content)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// returns a mutable reference to self.bookmarks
|
||||||
|
pub fn get_bookmarks(&mut self) -> &mut HashMap<String, PathBuf> {
|
||||||
|
&mut self.bookmarks
|
||||||
|
}
|
||||||
|
|
||||||
|
/// adds a key/value pair to bookmarks and writes the bookmarks file
|
||||||
|
pub fn add_bookmark(&mut self, name: &String, path: &PathBuf) -> Result<()> {
|
||||||
|
if !path.is_dir() {
|
||||||
|
return Err(Error::other(
|
||||||
|
"-- provided path argument does not point to a valid directory",
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
self.bookmarks.insert(name.to_string(), path.to_path_buf());
|
||||||
|
self.write_bookmark_file()?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// removes a the entry with key=name if it exists, then writes the bookmarks file
|
||||||
|
pub fn remove_bookmark(&mut self, name: &String) -> Result<()> {
|
||||||
|
if self.bookmarks.contains_key(name) {
|
||||||
|
_ = self.bookmarks.remove(name);
|
||||||
|
self.write_bookmark_file()?;
|
||||||
|
} else {
|
||||||
|
return Err(Error::other(
|
||||||
|
"-- bookmark requested to delete does not exist",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
-83
@@ -1,7 +1,6 @@
|
|||||||
//! handle the config file and bookmarks stored
|
//! handle the config file and bookmarks stored
|
||||||
//! in said config file
|
//! in said config file
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use std::env::var;
|
use std::env::var;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
@@ -15,7 +14,6 @@ use crate::{RESET_SEQ, STYLES};
|
|||||||
pub struct Config {
|
pub struct Config {
|
||||||
conf_dir: PathBuf,
|
conf_dir: PathBuf,
|
||||||
pub settings: Settings,
|
pub settings: Settings,
|
||||||
bookmarks: HashMap<String, PathBuf>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@@ -50,11 +48,10 @@ pub struct StyleSettings {
|
|||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
const CONFIG_FILE_NAME: &str = "navigate.conf";
|
const CONFIG_FILE_NAME: &str = "navigate.conf";
|
||||||
const BOOKMARK_FILE_NAME: &str = "bookmarks.conf";
|
|
||||||
|
|
||||||
/// generates and populates a new instance of Config
|
/// generates and populates a new instance of Config
|
||||||
pub fn new() -> Result<Self> {
|
pub fn new() -> Result<Self> {
|
||||||
let mut bookmarks = Config {
|
let mut config = Config {
|
||||||
conf_dir: PathBuf::new(),
|
conf_dir: PathBuf::new(),
|
||||||
settings: Settings {
|
settings: Settings {
|
||||||
general: GeneralSettings {
|
general: GeneralSettings {
|
||||||
@@ -75,7 +72,6 @@ impl Config {
|
|||||||
reset: RESET_SEQ.to_owned(),
|
reset: RESET_SEQ.to_owned(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
bookmarks: HashMap::<String, PathBuf>::new(),
|
|
||||||
};
|
};
|
||||||
// get home directory path
|
// get home directory path
|
||||||
let home_dir = match var("HOME") {
|
let home_dir = match var("HOME") {
|
||||||
@@ -83,15 +79,14 @@ impl Config {
|
|||||||
Err(error) => return Err(Error::other(error.to_string())),
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
};
|
};
|
||||||
// create PathBuf object from home dir path
|
// create PathBuf object from home dir path
|
||||||
bookmarks.conf_dir = match PathBuf::from_str(&home_dir) {
|
config.conf_dir = match PathBuf::from_str(&home_dir) {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => return Err(Error::other(error.to_string())),
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
};
|
};
|
||||||
// expand home directory path to get configuration directory path
|
// expand home directory path to get configuration directory path
|
||||||
bookmarks.conf_dir.push(".config/navigate/");
|
config.build_config()?;
|
||||||
bookmarks.build_bookmarks()?;
|
|
||||||
|
|
||||||
Ok(bookmarks)
|
Ok(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// reads and parses the configuration file
|
/// reads and parses the configuration file
|
||||||
@@ -99,78 +94,4 @@ impl Config {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// reads and parses the bookmarks file
|
|
||||||
fn build_bookmarks(&mut self) -> Result<()> {
|
|
||||||
let mut bookmark_file = self.conf_dir.clone();
|
|
||||||
bookmark_file.push(Self::BOOKMARK_FILE_NAME);
|
|
||||||
|
|
||||||
if !bookmark_file.is_file() {
|
|
||||||
_ = File::create(bookmark_file.clone())?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let bookmarks = fs::read_to_string(bookmark_file)?;
|
|
||||||
let bookmarks = bookmarks.split("\n");
|
|
||||||
for entry in bookmarks {
|
|
||||||
let tokens: Vec<&str> = entry.split("=").collect();
|
|
||||||
if tokens.len() != 2 {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
let key: String = String::from(tokens[0]);
|
|
||||||
let path = match PathBuf::from_str(tokens[1]) {
|
|
||||||
Ok(value) => value,
|
|
||||||
Err(err) => return Err(Error::other(err.to_string())),
|
|
||||||
};
|
|
||||||
if !path.is_dir() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
self.bookmarks.insert(key, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// writes the bookmarks file
|
|
||||||
fn write_bookmark_file(&self) -> Result<()> {
|
|
||||||
let mut file_content = String::new();
|
|
||||||
for (mark, path) in self.bookmarks.iter() {
|
|
||||||
file_content.push_str(&format!("{}={}\n", mark, path.to_str().unwrap()));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut path = self.conf_dir.clone();
|
|
||||||
path.push(Self::BOOKMARK_FILE_NAME);
|
|
||||||
|
|
||||||
fs::write(path, file_content)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// returns a mutable reference to self.bookmarks
|
|
||||||
pub fn get_bookmarks(&mut self) -> &mut HashMap<String, PathBuf> {
|
|
||||||
&mut self.bookmarks
|
|
||||||
}
|
|
||||||
|
|
||||||
/// adds a key/value pair to bookmarks and writes the bookmarks file
|
|
||||||
pub fn add_bookmark(&mut self, name: &String, path: &PathBuf) -> Result<()> {
|
|
||||||
if !path.is_dir() {
|
|
||||||
return Err(Error::other(
|
|
||||||
"-- provided path argument does not point to a valid directory",
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
self.bookmarks.insert(name.to_string(), path.to_path_buf());
|
|
||||||
self.write_bookmark_file()?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// removes a the entry with key=name if it exists, then writes the bookmarks file
|
|
||||||
pub fn remove_bookmark(&mut self, name: &String) -> Result<()> {
|
|
||||||
if self.bookmarks.contains_key(name) {
|
|
||||||
_ = self.bookmarks.remove(name);
|
|
||||||
self.write_bookmark_file()?;
|
|
||||||
} else {
|
|
||||||
return Err(Error::other(
|
|
||||||
"-- bookmark requested to delete does not exist",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-7
@@ -1,10 +1,8 @@
|
|||||||
use std::io::{Error, Result};
|
pub const ESC: &str = "\x1B";
|
||||||
|
pub const PREFIX: &str = "\x1B[";
|
||||||
pub const ESC: &'static str = "\x1B";
|
pub const RESET_ARG: &str = "0";
|
||||||
pub const PREFIX: &'static str = "\x1B[";
|
pub const TERMINATION: &str = "m";
|
||||||
pub const RESET_ARG: &'static str = "0";
|
pub const RESET_SEQ: &str = "\x1B[0m";
|
||||||
pub const TERMINATION: &'static str = "m";
|
|
||||||
pub const RESET_SEQ: &'static str = "\x1B[0m";
|
|
||||||
pub const FG: ColorContext = ColorContext::Foreground;
|
pub const FG: ColorContext = ColorContext::Foreground;
|
||||||
pub const BG: ColorContext = ColorContext::Background;
|
pub const BG: ColorContext = ColorContext::Background;
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
mod arguments;
|
mod arguments;
|
||||||
mod config;
|
mod config;
|
||||||
|
mod bookmarks;
|
||||||
mod format;
|
mod format;
|
||||||
mod stack;
|
mod stack;
|
||||||
|
|
||||||
use arguments::*;
|
use arguments::*;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use config::*;
|
use config::*;
|
||||||
|
use bookmarks::*;
|
||||||
use format::*;
|
use format::*;
|
||||||
use stack::Stack;
|
use stack::Stack;
|
||||||
use std::env::{current_dir, var};
|
use std::env::{current_dir, var};
|
||||||
|
|||||||
Reference in New Issue
Block a user