quicksave

This commit is contained in:
2024-12-08 10:08:09 +01:00
parent fd8c2438c2
commit 036e3d3574
4 changed files with 49 additions and 30 deletions
+2 -2
View File
@@ -18,12 +18,12 @@ pub struct Bookmarks {
} }
impl Config { impl Bookmarks {
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 = Bookmarks {
conf_dir: PathBuf::new(), conf_dir: PathBuf::new(),
bookmarks: HashMap::<String, PathBuf>::new(), bookmarks: HashMap::<String, PathBuf>::new(),
}; };
+26 -19
View File
@@ -1,7 +1,7 @@
mod arguments; mod arguments;
mod format;
mod config; mod config;
mod bookmarks; mod bookmarks;
mod format;
mod stack; mod stack;
use arguments::*; use arguments::*;
@@ -32,6 +32,13 @@ fn main() -> Result<()> {
return Ok(()); return Ok(());
} }
}; };
let mut bookmarks = match Bookmarks::new() {
Ok(value) => value,
Err(error) => {
print!("echo '{}{}{}' && false", style_error, error, RESET_SEQ);
return Ok(());
}
};
let mut stack = match Stack::new(args.pid) { let mut stack = match Stack::new(args.pid) {
Ok(stack) => stack, Ok(stack) => stack,
Err(_) => { Err(_) => {
@@ -43,10 +50,10 @@ fn main() -> Result<()> {
} }
}; };
let res = match args.action { let res = match args.action {
Action::push(push_args) => handle_push(&push_args, &mut config, &mut stack), Action::push(push_args) => handle_push(&push_args, &config, &mut stack),
Action::pop(pop_args) => handle_pop(&pop_args, &mut config, &mut stack), Action::pop(pop_args) => handle_pop(&pop_args, &config, &mut stack),
Action::stack(stack_args) => handle_stack(&stack_args, &mut config, &mut stack), Action::stack(stack_args) => handle_stack(&stack_args, &config, &mut stack),
Action::bookmark(bookmark_args) => handle_bookmark(&bookmark_args, &mut config, &mut stack), Action::bookmark(bookmark_args) => handle_bookmark(&bookmark_args, &config, &mut bookmarks, &mut stack),
}; };
if res.is_err() { if res.is_err() {
@@ -60,7 +67,7 @@ fn main() -> Result<()> {
Ok(()) Ok(())
} }
fn handle_push(args: &PushArgs, _config: &mut Config, stack: &mut Stack) -> Result<()> { fn handle_push(args: &PushArgs, _config: &Config, stack: &mut Stack) -> Result<()> {
let path = match args.path.clone() { let path = match args.path.clone() {
Some(value) => value, Some(value) => value,
None => { None => {
@@ -78,7 +85,7 @@ fn handle_push(args: &PushArgs, _config: &mut Config, stack: &mut Stack) -> Resu
Ok(()) Ok(())
} }
fn handle_pop(args: &PopArgs, _config: &mut Config, stack: &mut Stack) -> Result<()> { fn handle_pop(args: &PopArgs, _config: &Config, stack: &mut Stack) -> Result<()> {
let path = stack.pop_entry(args.num_entries)?; let path = stack.pop_entry(args.num_entries)?;
println!( println!(
"cd -- {}", "cd -- {}",
@@ -90,7 +97,7 @@ fn handle_pop(args: &PopArgs, _config: &mut Config, stack: &mut Stack) -> Result
Ok(()) Ok(())
} }
fn handle_stack(args: &StackArgs, config: &mut Config, stack: &mut Stack) -> Result<()> { fn handle_stack(args: &StackArgs, config: &Config, stack: &mut Stack) -> Result<()> {
if args.stack_action.is_some() { if args.stack_action.is_some() {
match args.stack_action.clone().unwrap() { match args.stack_action.clone().unwrap() {
StackAction::clear(_) => return stack.clear_stack(config), StackAction::clear(_) => return stack.clear_stack(config),
@@ -102,16 +109,16 @@ fn handle_stack(args: &StackArgs, config: &mut Config, stack: &mut Stack) -> Res
Ok(()) Ok(())
} }
fn handle_bookmark(args: &BookmarkArgs, config: &mut Config, stack: &mut Stack) -> Result<()> { fn handle_bookmark(args: &BookmarkArgs, config: &Config, bookmarks: &mut Bookmarks, stack: &mut Stack) -> Result<()> {
// if args.bookmark_action.is_some() { // if args.bookmark_action.is_some() {
if args.bookmark_action.is_some() { if args.bookmark_action.is_some() {
match args.bookmark_action.clone().unwrap() { match args.bookmark_action.clone().unwrap() {
BookmarkAction::list(_) => list_bookmarks(&mut config)?, BookmarkAction::list(_) => list_bookmarks(config, bookmarks)?,
BookmarkAction::add(args) => add_bookmarks(&args, &mut config)?, BookmarkAction::add(args) => add_bookmarks(&args, config, bookmarks)?,
BookmarkAction::remove(args) => remove_bookmarks(&args, &mut config)?, BookmarkAction::remove(args) => remove_bookmarks(&args, config, bookmarks)?,
}; };
} else if args.name.is_some() { } else if args.name.is_some() {
let path = match config.get_bookmarks().get(args.name.as_ref().unwrap()) { let path = match bookmarks.get_bookmarks().get(args.name.as_ref().unwrap()) {
Some(value) => value, Some(value) => value,
None => return Err(Error::other("-- requested bookmark does not exist")), None => return Err(Error::other("-- requested bookmark does not exist")),
}; };
@@ -124,26 +131,26 @@ fn handle_bookmark(args: &BookmarkArgs, config: &mut Config, stack: &mut Stack)
Ok(()) Ok(())
} }
fn list_bookmarks(config: &mut Config) -> Result<()> { fn list_bookmarks(config: &Config, bookmarks: &mut Bookmarks) -> Result<()> {
let mut buffer = String::new(); let mut buffer = String::new();
for (mark, path) in config.get_bookmarks() { for (mark, path) in bookmarks.get_bookmarks() {
buffer.push_str(&format!("{} : {}\n", mark, path.to_str().unwrap())); buffer.push_str(&format!("{} : {}\n", mark, path.to_str().unwrap()));
} }
println!("echo '{}'", buffer); println!("echo '{}'", buffer);
Ok(()) Ok(())
} }
fn add_bookmarks(args: &BookmarkSubArgs, config: &mut Config) -> Result<()> { fn add_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookmarks) -> Result<()> {
if args.path.is_none() { if args.path.is_none() {
return Err(Error::other("-- missing path argument")); return Err(Error::other("-- missing path argument"));
} else { } else {
config.add_bookmark(&args.name, &args.path.clone().unwrap())?; bookmarks.add_bookmark(&args.name, &args.path.clone().unwrap())?;
} }
Ok(()) Ok(())
} }
fn remove_bookmarks(args: &BookmarkSubArgs, config: &mut Config) -> Result<()> { fn remove_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookmarks) -> Result<()> {
config.remove_bookmark(&args.name)?; bookmarks.remove_bookmark(&args.name)?;
Ok(()) Ok(())
} }
+17 -7
View File
@@ -1,10 +1,10 @@
use super::config::*;
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use std::io::{Error, Result}; 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::*;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Stack { pub struct Stack {
@@ -31,17 +31,20 @@ impl Stack {
return Err(Error::other("-- the stack is empty")); return Err(Error::other("-- the stack is empty"));
} }
// print stack to string // print stack to string
let mut output : String = "".to_string(); let mut output: String = "".to_string();
for (n, item) in self.stack.iter().rev().enumerate() { for (n, item) in self.stack.iter().rev().enumerate() {
output.push_str(&format!("'{} - {}'\n", n, item.to_str().unwrap())); output.push_str(&format!("'{} - {}'\n", n, item.to_str().unwrap()));
} }
Ok(output) Ok(output)
} }
/// clear stack by deleting the associated stack file /// clear stack by deleting the associated stack file
pub fn clear_stack(&mut self, config: &mut Config) -> Result<()> { pub fn clear_stack(&mut self, config: &Config) -> Result<()> {
fs::remove_file(self.path.clone())?; fs::remove_file(self.path.clone())?;
print!("echo '{}stack cleared successfully.{}'", config.settings.styles.note, config.settings.styles.reset); print!(
"echo '{}stack cleared successfully.{}'",
config.settings.styles.note, config.settings.styles.reset
);
Ok(()) Ok(())
} }
@@ -56,8 +59,15 @@ impl Stack {
/// pop entry from stack /// pop entry from stack
/// return popped entry /// return popped entry
pub fn pop_entry(&mut self, _num_entries: Option<usize>) -> Result<PathBuf> { pub fn pop_entry(&mut self, num_entries: Option<usize>) -> Result<PathBuf> {
let entry = self.stack.pop(); let mut num = 1;
let mut entry: Option<PathBuf>;
if let Some(value) = num_entries {
num = value;
}
for _ in 0..num {
entry = self.stack.pop();
}
self.write_stack_file()?; self.write_stack_file()?;
match entry { match entry {
Some(entry) => Ok(entry), Some(entry) => Ok(entry),
+4 -2
View File
@@ -2,8 +2,10 @@
- [x] 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
- [x] drop stack - [x] drop stack
- [ ] config file - [ ] config file
- [ ] dedup stack option
- [ ] parse config file
- [ ] apply config
- [ ] colored output > make it configurable through config file
- [x] bookmarks - [x] bookmarks
- [ ] colored output > make it configurable through config file