implemented popping multiple entries at once

This commit is contained in:
2024-12-08 11:29:15 +01:00
parent 036e3d3574
commit e3d43755b1
4 changed files with 17 additions and 10 deletions
+7 -1
View File
@@ -17,7 +17,6 @@ pub struct Bookmarks {
bookmarks: HashMap<String, PathBuf>, bookmarks: HashMap<String, PathBuf>,
} }
impl Bookmarks { impl Bookmarks {
const BOOKMARK_FILE_NAME: &str = "bookmarks.conf"; const BOOKMARK_FILE_NAME: &str = "bookmarks.conf";
@@ -46,9 +45,16 @@ impl Bookmarks {
/// reads and parses the bookmarks file /// reads and parses the bookmarks file
fn build_bookmarks(&mut self) -> Result<()> { fn build_bookmarks(&mut self) -> Result<()> {
// check if configuration directory exists, if not create it
if !self.conf_dir.is_dir() {
fs::create_dir(self.conf_dir.clone())?;
}
let mut bookmark_file = self.conf_dir.clone(); let mut bookmark_file = self.conf_dir.clone();
bookmark_file.push(Self::BOOKMARK_FILE_NAME); bookmark_file.push(Self::BOOKMARK_FILE_NAME);
// check if bookmarks file exists, if not create it
if !bookmark_file.is_file() { if !bookmark_file.is_file() {
_ = File::create(bookmark_file.clone())?; _ = File::create(bookmark_file.clone())?;
} }
+1 -1
View File
@@ -25,7 +25,7 @@ fn main() -> Result<()> {
return Ok(()); return Ok(());
} }
}; };
let mut config = match Config::new() { let config = match Config::new() {
Ok(value) => value, Ok(value) => value,
Err(error) => { Err(error) => {
print!("echo '{}{}{}' && false", style_error, error, RESET_SEQ); print!("echo '{}{}{}' && false", style_error, error, RESET_SEQ);
+8 -7
View File
@@ -60,14 +60,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 mut num = 1; let mut num = num_entries.unwrap_or(1);
let mut entry: Option<PathBuf>; if num < 1 {
if let Some(value) = num_entries { num = 1;
num = value; } else if num > self.stack.len() {
} num = self.stack.len();
for _ in 0..num {
entry = self.stack.pop();
} }
let mut dropped_entries = self.stack.drain((self.stack.len()-num)..);
let entry = dropped_entries.nth(0);
drop(dropped_entries);
self.write_stack_file()?; self.write_stack_file()?;
match entry { match entry {
Some(entry) => Ok(entry), Some(entry) => Ok(entry),
+1 -1
View File
@@ -1,7 +1,7 @@
# todos for 'navigation' # todos for 'navigation'
- [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 - [x] pop several entries at a time
- [x] drop stack - [x] drop stack
- [ ] config file - [ ] config file
- [ ] dedup stack option - [ ] dedup stack option