..
This commit is contained in:
+12
-3
@@ -12,16 +12,21 @@ use std::str::FromStr;
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
conf_dir: PathBuf,
|
conf_dir: PathBuf,
|
||||||
|
settings: Settings,
|
||||||
bookmarks: HashMap<String, PathBuf>,
|
bookmarks: HashMap<String, PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Settings {}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
const BOOKMARK_FILE_NAME : &str = "bookmarks.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 bookmarks = Config {
|
||||||
conf_dir: PathBuf::new(),
|
conf_dir: PathBuf::new(),
|
||||||
|
settings: Settings{},
|
||||||
bookmarks: HashMap::<String, PathBuf>::new(),
|
bookmarks: HashMap::<String, PathBuf>::new(),
|
||||||
};
|
};
|
||||||
let home_dir = match var("HOME") {
|
let home_dir = match var("HOME") {
|
||||||
@@ -44,7 +49,9 @@ impl Config {
|
|||||||
|
|
||||||
pub fn add_bookmark(&mut self, name: &String, path: &PathBuf) -> Result<()> {
|
pub fn add_bookmark(&mut self, name: &String, path: &PathBuf) -> Result<()> {
|
||||||
if !path.is_dir() {
|
if !path.is_dir() {
|
||||||
return Err(Error::other("-- provided path argument does not point to a valid directory"))
|
return Err(Error::other(
|
||||||
|
"-- provided path argument does not point to a valid directory",
|
||||||
|
));
|
||||||
} else {
|
} else {
|
||||||
self.bookmarks.insert(name.to_string(), path.to_path_buf());
|
self.bookmarks.insert(name.to_string(), path.to_path_buf());
|
||||||
self.write_bookmark_file()?;
|
self.write_bookmark_file()?;
|
||||||
@@ -57,7 +64,9 @@ impl Config {
|
|||||||
_ = self.bookmarks.remove(name);
|
_ = self.bookmarks.remove(name);
|
||||||
self.write_bookmark_file()?;
|
self.write_bookmark_file()?;
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::other("-- bookmark requested to delete does not exist"));
|
return Err(Error::other(
|
||||||
|
"-- bookmark requested to delete does not exist",
|
||||||
|
));
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-11
@@ -57,9 +57,8 @@ fn handle_push(args: &PushArgs, stack: &mut Stack) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_pop(_args: &PopArgs, stack: &mut Stack) -> Result<()> {
|
fn handle_pop(args: &PopArgs, stack: &mut Stack) -> Result<()> {
|
||||||
// TODO: handle arguments
|
let path = stack.pop_entry(args.num_entries)?;
|
||||||
let path = stack.pop_entry()?;
|
|
||||||
println!(
|
println!(
|
||||||
"cd -- {}",
|
"cd -- {}",
|
||||||
match path.to_str() {
|
match path.to_str() {
|
||||||
@@ -77,14 +76,8 @@ fn handle_stack(args: &StackArgs, stack: &mut Stack) -> Result<()> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// retrieve stack
|
// retrieve stack
|
||||||
let output = stack.get_stack()?;
|
let output: String = stack.to_string(None)?;
|
||||||
if output.is_empty() {
|
print!("echo '{}'", output);
|
||||||
return Err(Error::other("-- the stack is empty"));
|
|
||||||
}
|
|
||||||
// print stack to standard output
|
|
||||||
for (n, item) in output.iter().rev().enumerate() {
|
|
||||||
println!("echo '{} - {}'", n, item.to_str().unwrap());
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+50
-35
@@ -4,6 +4,7 @@ use std::io::{Error, Result};
|
|||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use sysinfo::{Pid, System};
|
use sysinfo::{Pid, System};
|
||||||
|
use super::config::Settings;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Stack {
|
pub struct Stack {
|
||||||
@@ -19,21 +20,22 @@ impl Stack {
|
|||||||
path: PathBuf::new(),
|
path: PathBuf::new(),
|
||||||
stack: Vec::<PathBuf>::new(),
|
stack: Vec::<PathBuf>::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
// remove first entry if it is empty, because after
|
|
||||||
// creation of the stack there seems to be an empty
|
|
||||||
// cell in the vector
|
|
||||||
stack.build_stack()?;
|
stack.build_stack()?;
|
||||||
if !stack.stack[0].is_dir() {
|
|
||||||
stack.stack.remove(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(stack)
|
Ok(stack)
|
||||||
}
|
}
|
||||||
|
|
||||||
// return stack
|
// return stack
|
||||||
pub fn get_stack(&mut self) -> Result<&Vec<PathBuf>> {
|
pub fn to_string(&self, _settings: Option<Settings>) -> Result<String> {
|
||||||
Ok(&self.stack)
|
if self.stack.is_empty() {
|
||||||
|
return Err(Error::other("-- the stack is empty"));
|
||||||
|
}
|
||||||
|
// print stack to string
|
||||||
|
let mut output : String = "".to_string();
|
||||||
|
for (n, item) in self.stack.iter().rev().enumerate() {
|
||||||
|
output.push_str(&format!("'{} - {}'\n", n, item.to_str().unwrap()));
|
||||||
|
}
|
||||||
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// clear stack by deleting the associated stack file
|
/// clear stack by deleting the associated stack file
|
||||||
@@ -52,10 +54,9 @@ impl Stack {
|
|||||||
|
|
||||||
/// pop entry from stack
|
/// pop entry from stack
|
||||||
/// return popped entry
|
/// return popped entry
|
||||||
pub fn pop_entry(&mut self) -> Result<PathBuf> {
|
pub fn pop_entry(&mut self, _num_entries: Option<usize>) -> Result<PathBuf> {
|
||||||
let entry = self.stack.pop();
|
let entry = self.stack.pop();
|
||||||
self.write_stack_file()?;
|
self.write_stack_file()?;
|
||||||
|
|
||||||
match entry {
|
match entry {
|
||||||
Some(entry) => Ok(entry),
|
Some(entry) => Ok(entry),
|
||||||
None => Err(Error::other(
|
None => Err(Error::other(
|
||||||
@@ -68,23 +69,26 @@ impl Stack {
|
|||||||
/// return nth last entry
|
/// return nth last entry
|
||||||
pub fn get_entry_by_number(&mut self, entry_number: usize) -> Result<&PathBuf> {
|
pub fn get_entry_by_number(&mut self, entry_number: usize) -> Result<&PathBuf> {
|
||||||
// index from the end of the vector as new entries are appended at the end of the list
|
// index from the end of the vector as new entries are appended at the end of the list
|
||||||
match self.stack.get(
|
let index = match self.stack.len().checked_sub(entry_number) {
|
||||||
self.stack
|
Some(value) => value,
|
||||||
.len()
|
None => return Err(Error::other("-- no entry found at request index")),
|
||||||
.checked_sub(entry_number)
|
};
|
||||||
.expect("-- requested entry number is out of bounds"),
|
match self.stack.get(index) {
|
||||||
) {
|
|
||||||
Some(item) => Ok(item),
|
Some(item) => Ok(item),
|
||||||
None => Err(Error::other(
|
None => Err(Error::other("-- failed to retrieve stack entry by number")),
|
||||||
"-- failed to retrieve stack entry by number",
|
|
||||||
)),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// clean up dead stack files, parse and build stack
|
/// clean up dead stack files, parse and build stack
|
||||||
fn build_stack(&mut self) -> Result<()> {
|
fn build_stack(&mut self) -> Result<()> {
|
||||||
let stack_dir: PathBuf = PathBuf::from_str("/tmp/navigation/")
|
let stack_dir: PathBuf = match PathBuf::from_str("/tmp/navigation/") {
|
||||||
.expect("-- failed to create path object of '/tmp/navigation'");
|
Ok(value) => value,
|
||||||
|
Err(_) => {
|
||||||
|
return Err(Error::other(
|
||||||
|
"-- failed to create path object of the stack directory",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
};
|
||||||
let mut sys = System::new_all();
|
let mut sys = System::new_all();
|
||||||
sys.refresh_all();
|
sys.refresh_all();
|
||||||
let procs = sys.processes();
|
let procs = sys.processes();
|
||||||
@@ -94,14 +98,18 @@ impl Stack {
|
|||||||
let members = fs::read_dir(stack_dir.clone())?;
|
let members = fs::read_dir(stack_dir.clone())?;
|
||||||
for entry in members {
|
for entry in members {
|
||||||
let entry = entry?;
|
let entry = entry?;
|
||||||
let process_id = Pid::from_str(
|
let process_id = match Pid::from_str(match entry.file_name().to_str() {
|
||||||
entry
|
Some(value) => value,
|
||||||
.file_name()
|
None => return Err(Error::other("-- failed to convert file name to str")),
|
||||||
.to_str()
|
}) {
|
||||||
.expect("-- failed to convert file name to str"),
|
Ok(value) => value,
|
||||||
);
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
if !procs.contains_key(&process_id.expect("-- failed to convert filename to pid")) {
|
};
|
||||||
fs::remove_file(entry.path()).expect("-- failed to remove orphaned file");
|
if !procs.contains_key(&process_id) {
|
||||||
|
match fs::remove_file(entry.path()) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -118,6 +126,7 @@ impl Stack {
|
|||||||
// create stack file and store current path
|
// create stack file and store current path
|
||||||
File::create(self.path.clone())?;
|
File::create(self.path.clone())?;
|
||||||
}
|
}
|
||||||
|
self.cleanup_stack();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -133,15 +142,21 @@ impl Stack {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// remove invalid paths from stack
|
||||||
|
fn cleanup_stack(&mut self) {
|
||||||
|
if !self.stack.is_empty() {
|
||||||
|
self.stack.retain(|entry| entry.is_dir());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// write stack current stack to file to save it for next execution
|
/// write stack current stack to file to save it for next execution
|
||||||
fn write_stack_file(&mut self) -> Result<()> {
|
fn write_stack_file(&mut self) -> Result<()> {
|
||||||
let mut output = Vec::<&str>::new();
|
let mut output = Vec::<&str>::new();
|
||||||
for entry in &self.stack {
|
for entry in &self.stack {
|
||||||
output.push(
|
output.push(match entry.to_str() {
|
||||||
entry
|
Some(value) => value,
|
||||||
.to_str()
|
None => return Err(Error::other("-- failed to convert stack entry to string")),
|
||||||
.expect("-- failed to convert stack entry to string"),
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
fs::write(self.path.clone(), output.join("\n"))?;
|
fs::write(self.path.clone(), output.join("\n"))?;
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
# todos for 'navigation'
|
# todos for 'navigation'
|
||||||
|
|
||||||
- [ ] replace `expect` statements in 'stack.rs' with actual error handling
|
- [x] replace `expect` statements in 'stack.rs' with actual error handling
|
||||||
- [ ] pop several entries at a time
|
- [ ] pop several entries at a time
|
||||||
- [ ] dedup stack option
|
- [ ] dedup stack option
|
||||||
- [x] drop stack
|
- [x] drop stack
|
||||||
- [ ] config file
|
- [ ] config file
|
||||||
- [ ] bookmarks
|
- [x] bookmarks
|
||||||
|
- [ ] colored output > make it configurable through config file
|
||||||
|
|||||||
Reference in New Issue
Block a user