added clear function as optional subcommand to stack command

This commit is contained in:
2024-11-29 08:08:51 +01:00
parent e6879afd58
commit dce08c9b9a
6 changed files with 200 additions and 94 deletions
+1 -1
View File
@@ -14,5 +14,5 @@ pop() {
} }
stack() { stack() {
echo "$(navigate ${arg_pid} show $*)" echo "$(navigate ${arg_pid} stack $@)"
} }
+24 -6
View File
@@ -1,5 +1,5 @@
use clap::{Parser, Args, Subcommand}; use clap::{Args, Parser, Subcommand};
use std::path::PathBuf; use std::{path::PathBuf};
/// implements stack for cd wrapper script /// implements stack for cd wrapper script
@@ -16,6 +16,7 @@ pub struct Arguments {
} }
#[derive(Debug, Clone, Subcommand)] #[derive(Debug, Clone, Subcommand)]
#[allow(non_camel_case_types)]
pub enum Action { pub enum Action {
/// navigate to path and add current path to the stack /// navigate to path and add current path to the stack
push(PushArgs), push(PushArgs),
@@ -24,7 +25,7 @@ pub enum Action {
pop(PopArgs), pop(PopArgs),
/// show stack /// show stack
show(ShowArgs), stack(StackArgs),
/// navigate to bookmark and add current path to the stack /// navigate to bookmark and add current path to the stack
bookmark(BookmarkArgs), bookmark(BookmarkArgs),
@@ -45,19 +46,36 @@ pub struct PopArgs {
/// show stack /// show stack
#[arg(short, long)] #[arg(short, long)]
pub show_stack: Option<bool>, pub show_stack: Option<bool>,
/// pop multiple entries and navigate to last retrieved path
pub num_entries: Option<usize>,
} }
#[derive(Debug, Clone, Args)] #[derive(Debug, Clone, Args)]
pub struct ShowArgs { pub struct StackArgs {
/// hide entry numbers /// hide entry numbers
#[arg(short='n', long)] #[arg(short = 'n', long)]
pub hide_numbers: Option<bool>, pub hide_numbers: Option<bool>,
/// show n entries /// show n entries
#[arg(short, long="lines")] #[arg(short, long = "lines")]
pub lines: Option<u32>, pub lines: Option<u32>,
/// stack subcommand
#[command(subcommand)]
pub stack_action: Option<StackAction>,
} }
#[derive(Debug, Clone, Subcommand)]
#[allow(non_camel_case_types)]
pub enum StackAction {
/// clear stack
clear(ClearArgs),
}
#[derive(Debug, Clone, Args)]
pub struct ClearArgs {}
#[derive(Debug, Clone, Args)] #[derive(Debug, Clone, Args)]
pub struct BookmarkArgs { pub struct BookmarkArgs {
/// show stack /// show stack
+51
View File
@@ -0,0 +1,51 @@
//! handle the config file and bookmarks stored
//! in said config file
use std::fs;
use std::fs::File;
use std::io::{Error, ErrorKind, Result};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use sysinfo::{Pid, System};
const config_dir_path: &str = "~/.config/navigate/";
#[derive(Debug, Clone)]
pub struct Config {
pub bookmarks: Vec<PathBuf>,
}
impl Config {
/// generates and populates a new instance of Config
pub fn new() -> Result<Self> {
let mut config = Config {
bookmarks: Vec::<PathBuf>::new(),
};
// stack.build_stack()?;
if !config.bookmarks[0].is_dir() {
config.bookmarks.remove(0);
};
Ok(config)
}
pub fn build_config(&mut self) -> Result<()> {
let config_dir = match PathBuf::from_str(config_dir_path) {
Ok(result) => result,
Err(_) => return Err(Error::other("failed to create path object for config file")),
};
let mut bookmark_file = config_dir.clone();
bookmark_file.push("bookmarks.conf");
if !bookmark_file.is_file() {
_ = File::create(bookmark_file.clone())?;
}
let mut bookmarks = fs::read_to_string(bookmark_file.clone())?;
// TODO: parse bookmarks
Ok(())
}
}
+39 -21
View File
@@ -1,48 +1,66 @@
mod arguments; mod arguments;
mod config;
mod stack; mod stack;
use std::io::{Result, Error, ErrorKind};
use std::path::PathBuf;
use std::env::current_dir;
use clap::{FromArgMatches, Parser};
use arguments::*; use arguments::*;
use clap::{Parser};
use stack::Stack; use stack::Stack;
use std::env::current_dir;
use std::io::{Error, Result};
fn main() -> Result<()> { fn main() -> Result<()> {
let args = Arguments::parse(); let args = Arguments::parse();
let mut stack = Stack::new(args.pid).expect("failed to build stack"); let mut stack = match Stack::new(args.pid) {
Ok(stack) => stack,
Err(_) => return Err(Error::other("-- failed to build stack")),
};
return match args.action { match args.action {
Action::push(push_args) => handle_push(&push_args, &mut stack), Action::push(push_args) => handle_push(&push_args, &mut stack),
Action::pop(pop_args) => handle_pop(&pop_args, &mut stack), Action::pop(pop_args) => handle_pop(&pop_args, &mut stack),
Action::show(show_args) => handle_show(&show_args, &mut stack), Action::stack(stack_args) => handle_stack(&stack_args, &mut stack),
Action::bookmark(bookmark_args) => handle_bookmark(&bookmark_args, &mut stack), Action::bookmark(bookmark_args) => handle_bookmark(&bookmark_args, &mut stack),
_ => Err(Error::new(ErrorKind::Other, "unknown subcommand")), }
};
} }
pub fn handle_push (args: &PushArgs, stack: &mut Stack) -> Result<()> { // TODO: handle arguments pub fn handle_push(args: &PushArgs, stack: &mut Stack) -> Result<()> {
// TODO: handle arguments
if !args.path.is_dir() {
return Err(Error::other("-- invalid path argument"));
}
let current_path = current_dir()?; let current_path = current_dir()?;
stack.push_entry(&current_path)?; stack.push_entry(&current_path)?;
println!("{}", args.path.to_str().unwrap()); println!("{}", args.path.to_str().unwrap());
return Ok(()); Ok(())
} }
pub fn handle_pop (args: &PopArgs, stack: &mut Stack) -> Result<()> { // TODO: handle arguments pub fn handle_pop(args: &PopArgs, stack: &mut Stack) -> Result<()> {
// TODO: handle arguments
let path = stack.pop_entry()?; let path = stack.pop_entry()?;
println!("{}", path.to_str().unwrap()); println!("{}", path.to_str().unwrap());
return Ok(()); Ok(())
} }
pub fn handle_show (args: &ShowArgs, stack: &mut Stack) -> Result<()> { // TODO: handle arguments pub fn handle_stack(args: &StackArgs, stack: &mut Stack) -> Result<()> {
let output = stack.get_stack()?; // TODO: handle arguments
for item in output { if args.stack_action.is_some() {
println!("{}", item.to_str().unwrap()); match args.stack_action.clone().unwrap() {
StackAction::clear(_) => return stack.clear_stack(),
}
} }
return Ok(()); // retrieve stack
let output = stack.get_stack()?;
if output.is_empty() {
return Err(Error::other("-- the stack is empty"));
}
// print stack to standard output
for (n, item) in output.iter().rev().enumerate() {
println!("{} {}", n, item.to_str().unwrap());
}
Ok(())
} }
pub fn handle_bookmark (args: &BookmarkArgs, stack: &mut Stack) -> Result<()> { // TODO: handle arguments pub fn handle_bookmark(args: &BookmarkArgs, stack: &mut Stack) -> Result<()> {
return Ok(()); // TODO: handle arguments
Ok(())
} }
+73 -62
View File
@@ -1,84 +1,92 @@
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use std::path::{PathBuf}; use std::io::{Error, ErrorKind, Result};
use std::path::{Path, PathBuf};
use std::str::FromStr; use std::str::FromStr;
use std::io::{Result, Error, ErrorKind}; use sysinfo::{Pid, System};
use sysinfo::{System, Pid};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Stack pub struct Stack {
{
pid: u32, pid: u32,
path: PathBuf, path: PathBuf,
stack: Vec<PathBuf>, stack: Vec<PathBuf>,
} }
impl Stack impl Stack {
{
pub fn new(process_id: u32) -> Result<Self> { pub fn new(process_id: u32) -> Result<Self> {
let mut stack: Stack = Stack{ let mut stack: Stack = Stack {
pid : process_id, pid: process_id,
path : PathBuf::new(), path: PathBuf::new(),
stack : Vec::<PathBuf>::new(), stack: Vec::<PathBuf>::new(),
}; };
_ = stack.build_stack()?; // 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()?;
if !stack.stack[0].is_dir() {
stack.stack.remove(0);
}
return Ok(stack); Ok(stack)
} }
/** // return stack
* return stack
*/
pub fn get_stack(&mut self) -> Result<&Vec<PathBuf>> { pub fn get_stack(&mut self) -> Result<&Vec<PathBuf>> {
return Ok(&self.stack); Ok(&self.stack)
} }
/** /// clear stack by deleting the associated stack file
* push entry to stack pub fn clear_stack(&mut self) -> Result<()> {
* returns updated stack fs::remove_file(self.path.clone())
*/ }
pub fn push_entry(&mut self, path: &PathBuf) -> Result<&Vec<PathBuf>> {
/// push entry to stack
/// returns updated stack
pub fn push_entry(&mut self, path: &Path) -> Result<&Vec<PathBuf>> {
let abs_path = path.canonicalize()?; let abs_path = path.canonicalize()?;
self.stack.push(abs_path); self.stack.push(abs_path);
self.write_stack_file()?; self.write_stack_file()?;
return Ok(&self.stack); Ok(&self.stack)
} }
/** /// pop entry from stack
* pop entry from stack /// return popped entry
* return poppe entry
*/
pub fn pop_entry(&mut self) -> Result<PathBuf> { pub fn pop_entry(&mut self) -> Result<PathBuf> {
let entry = self.stack.pop(); let entry = self.stack.pop();
self.write_stack_file()?; self.write_stack_file()?;
return match entry { match entry {
Some(entry) => Ok(entry), Some(entry) => Ok(entry),
None => Err(Error::new(ErrorKind::Other, "pop failed to retrieve item from stack")), None => Err(Error::new(
ErrorKind::Other,
"pop failed to retrieve item from stack",
)),
} }
} }
/** /// get entry by number without removing it from the stack
* get entry by number /// 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: u32) -> Result<&PathBuf> {
if entry_number > (self.stack.len() as u32 - 1) {
return Err(Error::new(ErrorKind::Other, "requested entry number is out of bounds"));
}
// 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
return Ok(&self.stack[self.stack.len() - (entry_number as usize)]); match self.stack.get(
self.stack
.len()
.checked_sub(entry_number)
.expect("requested entry number is out of bounds"),
) {
Some(item) => Ok(item),
None => Err(Error::new(
ErrorKind::Other,
"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<()> {
*/ let stack_dir: PathBuf = PathBuf::from_str("/tmp/navigation/")
fn build_stack(&mut self) -> Result< Vec<PathBuf> > { .expect("failed to create path object of '/tmp/navigation'");
let stack_dir: PathBuf = PathBuf::from_str("/tmp/navigation/").expect("failed to create path object of '/tmp/navigation'");
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();
@@ -88,32 +96,35 @@ 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(entry.file_name().to_str().expect("failed to convert file name to str")); let process_id = Pid::from_str(
entry
.file_name()
.to_str()
.expect("failed to convert file name to str"),
);
if !procs.contains_key(&process_id.expect("failed to convert filename to pid")) { if !procs.contains_key(&process_id.expect("failed to convert filename to pid")) {
fs::remove_file(entry.path()).expect("failed to remove orphaned file"); fs::remove_file(entry.path()).expect("failed to remove orphaned file");
} }
} }
} else { } else {
// create temporary directory to store the stack // create temporary directory to store the stack
let _ = fs::create_dir(stack_dir.clone())?; fs::create_dir(stack_dir.clone())?;
} }
self.path = stack_dir.clone(); self.path = stack_dir.clone();
self.path.push(PathBuf::from(&self.pid.to_string())); self.path.push(PathBuf::from(&self.pid.to_string()));
if self.path.is_file() { if self.path.is_file() {
// read and parse stack file // read and parse stack file
_ = self.read_stack_file(&self.path.clone())?; self.read_stack_file(&self.path.clone())?;
} else { } else {
// create stack file and store current path // create stack file and store current path
_ = File::create(self.path.clone())?; File::create(self.path.clone())?;
} }
return Ok(vec![stack_dir]); Ok(())
} }
/** /// parse stack file
* parse stack file
*/
fn read_stack_file(&mut self, stack_file_path: &PathBuf) -> Result<()> { fn read_stack_file(&mut self, stack_file_path: &PathBuf) -> Result<()> {
let stack = fs::read_to_string(stack_file_path)?; let stack = fs::read_to_string(stack_file_path)?;
let stack = stack.split("\n"); let stack = stack.split("\n");
@@ -121,21 +132,21 @@ impl Stack
self.stack.push(PathBuf::from(entry)); self.stack.push(PathBuf::from(entry));
} }
return Ok(()); Ok(())
} }
/** /// 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(entry.to_str().expect("failed to convert stack entry to string")); output.push(
entry
.to_str()
.expect("failed to convert stack entry to string"),
);
} }
fs::write(self.path.clone(), output.join("\n"))?; fs::write(self.path.clone(), output.join("\n"))?;
return Ok(()) Ok(())
} }
} // end `impl database` } // end `impl database`
+8
View File
@@ -0,0 +1,8 @@
# todos for 'navigation'
- [ ] replace `expect` statements in 'stack.rs' with actual error handling
- [ ] pop several entries at a time
- [ ] dedup stack option
- [x] drop stack
- [ ] config file
- [ ] bookmarks