implemented push and pop commands, push command saves the wrong path though
This commit is contained in:
+4
-7
@@ -4,18 +4,15 @@ export PATH="$PATH:$PWD/target/debug/"
|
|||||||
pid=( $(ps -o ppid) )
|
pid=( $(ps -o ppid) )
|
||||||
arg_pid=" --pid ${pid[-2]} "
|
arg_pid=" --pid ${pid[-2]} "
|
||||||
|
|
||||||
nav() {
|
|
||||||
cd "$(navigate ${arg_pid} $*)"
|
|
||||||
}
|
|
||||||
|
|
||||||
push() {
|
push() {
|
||||||
cd "$(navigate push ${arg_pid} $*)"
|
\builtin cd -- "$(navigate ${arg_pid} push $@)"
|
||||||
}
|
}
|
||||||
|
|
||||||
pop() {
|
pop() {
|
||||||
cd "$(navigate pop ${arg_pid} $*)"
|
\builtin cd -- "$(navigate ${arg_pid} pop $@)"
|
||||||
}
|
}
|
||||||
|
|
||||||
stack() {
|
stack() {
|
||||||
"navigate stack ${arg_pid}"
|
echo "$(navigate ${arg_pid} show $*)"
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-13
@@ -1,17 +1,18 @@
|
|||||||
use clap::{Parser, Args, Subcommand};
|
use clap::{Parser, Args, Subcommand};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
|
||||||
/// implements stack for cd wrapper script
|
/// implements stack for cd wrapper script
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[clap(author, version, about, long_about=None)]
|
#[clap(author, version, about, long_about=None)]
|
||||||
pub struct Arguments {
|
pub struct Arguments {
|
||||||
/// subcommand
|
|
||||||
#[command(subcommand)]
|
|
||||||
pub action: Action,
|
|
||||||
|
|
||||||
/// process id of parent shell
|
/// process id of parent shell
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
pub pid: u32,
|
pub pid: u32,
|
||||||
|
|
||||||
|
/// subcommand
|
||||||
|
#[command(subcommand)]
|
||||||
|
pub action: Action,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Subcommand)]
|
#[derive(Debug, Clone, Subcommand)]
|
||||||
@@ -22,8 +23,8 @@ pub enum Action {
|
|||||||
/// navigate to last entry in stack and remove it
|
/// navigate to last entry in stack and remove it
|
||||||
pop(PopArgs),
|
pop(PopArgs),
|
||||||
|
|
||||||
/// display stack
|
/// show stack
|
||||||
stack(StackArgs),
|
show(ShowArgs),
|
||||||
|
|
||||||
/// navigate to bookmark and add current path to the stack
|
/// navigate to bookmark and add current path to the stack
|
||||||
bookmark(BookmarkArgs),
|
bookmark(BookmarkArgs),
|
||||||
@@ -33,33 +34,35 @@ pub enum Action {
|
|||||||
pub struct PushArgs {
|
pub struct PushArgs {
|
||||||
/// show stack
|
/// show stack
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
show_stack: Option<bool>,
|
pub show_stack: Option<bool>,
|
||||||
|
|
||||||
/// change to <path>
|
/// change to <path>
|
||||||
path: String,
|
pub path: PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct PopArgs {
|
pub struct PopArgs {
|
||||||
/// show stack
|
/// show stack
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
show_stack: Option<bool>,
|
pub show_stack: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct StackArgs {
|
pub struct ShowArgs {
|
||||||
/// hide entry numbers
|
/// hide entry numbers
|
||||||
#[arg(short='n', long)]
|
#[arg(short='n', long)]
|
||||||
hide_numbers: Option<bool>,
|
pub hide_numbers: Option<bool>,
|
||||||
|
|
||||||
/// show n entries
|
/// show n entries
|
||||||
#[arg(short, long="lines")]
|
#[arg(short, long="lines")]
|
||||||
lines: Option<u32>,
|
pub lines: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct BookmarkArgs {
|
pub struct BookmarkArgs {
|
||||||
/// show stack
|
/// show stack
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
show_stack: Option<bool>,
|
pub show_stack: Option<bool>,
|
||||||
|
|
||||||
|
pub mark: String,
|
||||||
}
|
}
|
||||||
+38
-5
@@ -1,13 +1,46 @@
|
|||||||
mod arguments;
|
mod arguments;
|
||||||
mod stack;
|
mod stack;
|
||||||
|
|
||||||
use clap::Parser;
|
use std::io::{Result, Error, ErrorKind};
|
||||||
use arguments::Arguments;
|
use std::path::PathBuf;
|
||||||
|
use clap::{FromArgMatches, Parser};
|
||||||
|
use arguments::*;
|
||||||
use stack::Stack;
|
use stack::Stack;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() -> Result<()> {
|
||||||
let args = Arguments::parse();
|
let args = Arguments::parse();
|
||||||
|
let mut stack = Stack::new(args.pid).expect("failed to build stack");
|
||||||
|
|
||||||
let stack = Stack::new(args.pid);
|
return match args.action {
|
||||||
}
|
Action::push(push_args) => handle_push(&push_args, &mut stack),
|
||||||
|
Action::pop(pop_args) => handle_pop(&pop_args, &mut stack),
|
||||||
|
Action::show(show_args) => handle_show(&show_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
|
||||||
|
stack.push_entry(&args.path)?;
|
||||||
|
println!("{}", args.path.to_str().unwrap());
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_pop (args: &PopArgs, stack: &mut Stack) -> Result<()> { // TODO: handle arguments
|
||||||
|
let path = stack.pop_entry()?;
|
||||||
|
println!("{}", path.to_str().unwrap());
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_show (args: &ShowArgs, stack: &mut Stack) -> Result<()> { // TODO: handle arguments
|
||||||
|
let output = stack.get_stack()?;
|
||||||
|
for item in output {
|
||||||
|
println!("{}", item.to_str().unwrap());
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn handle_bookmark (args: &BookmarkArgs, stack: &mut Stack) -> Result<()> { // TODO: handle arguments
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|||||||
+55
-13
@@ -1,9 +1,9 @@
|
|||||||
|
|
||||||
use std::io::{Result};
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::path::{PathBuf};
|
use std::path::{PathBuf};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
use std::io::{Result, Error, ErrorKind};
|
||||||
use sysinfo::{System, Pid};
|
use sysinfo::{System, Pid};
|
||||||
|
|
||||||
|
|
||||||
@@ -11,6 +11,7 @@ use sysinfo::{System, Pid};
|
|||||||
pub struct Stack
|
pub struct Stack
|
||||||
{
|
{
|
||||||
pid: u32,
|
pid: u32,
|
||||||
|
path: PathBuf,
|
||||||
stack: Vec<PathBuf>,
|
stack: Vec<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,29 +21,57 @@ 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(),
|
||||||
stack : Vec::<PathBuf>::new(),
|
stack : Vec::<PathBuf>::new(),
|
||||||
};
|
};
|
||||||
|
|
||||||
stack.stack = stack.build_stack()?;
|
_ = stack.build_stack()?;
|
||||||
|
|
||||||
return Ok(stack);
|
return Ok(stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return stack
|
||||||
|
*/
|
||||||
|
pub fn get_stack(&mut self) -> Result<&Vec<PathBuf>> {
|
||||||
|
return Ok(&self.stack);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* push entry to stack
|
* push entry to stack
|
||||||
* returns updated stack
|
* returns updated stack
|
||||||
*/
|
*/
|
||||||
pub fn push_entry(&mut self, path: PathBuf) -> Vec<PathBuf> {
|
pub fn push_entry(&mut self, path: &PathBuf) -> Result<&Vec<PathBuf>> {
|
||||||
self.stack.push(path);
|
let abs_path = path.canonicalize()?;
|
||||||
return self.stack.clone();
|
self.stack.push(abs_path);
|
||||||
|
self.write_stack_file()?;
|
||||||
|
return Ok(&self.stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* pop entry from stack
|
* pop entry from stack
|
||||||
* return poppe entry
|
* return poppe entry
|
||||||
*/
|
*/
|
||||||
pub fn pop_entry(&mut self) -> PathBuf {
|
pub fn pop_entry(&mut self) -> Result<PathBuf> {
|
||||||
return self.stack.pop().unwrap();
|
let entry = self.stack.pop();
|
||||||
|
self.write_stack_file()?;
|
||||||
|
|
||||||
|
return match entry {
|
||||||
|
Some(entry) => Ok(entry),
|
||||||
|
None => Err(Error::new(ErrorKind::Other, "pop failed to retrieve item from stack")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get entry by number
|
||||||
|
* return nth last entry
|
||||||
|
*/
|
||||||
|
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
|
||||||
|
return Ok(&self.stack[self.stack.len() - (entry_number as usize)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,14 +98,14 @@ impl Stack
|
|||||||
let _ = fs::create_dir(stack_dir.clone())?;
|
let _ = fs::create_dir(stack_dir.clone())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut stack_file_path = stack_dir.clone();
|
self.path = stack_dir.clone();
|
||||||
stack_file_path.push(PathBuf::from(&self.pid.to_string()));
|
self.path.push(PathBuf::from(&self.pid.to_string()));
|
||||||
if stack_file_path.is_file() {
|
if self.path.is_file() {
|
||||||
// read and parse stack file
|
// read and parse stack file
|
||||||
let _ = self.parse_stack_file(stack_file_path);
|
_ = self.read_stack_file(&self.path.clone())?;
|
||||||
} else {
|
} else {
|
||||||
// create stack file and store current path
|
// create stack file and store current path
|
||||||
let _ = File::create(stack_file_path.clone())?;
|
_ = File::create(self.path.clone())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(vec![stack_dir]);
|
return Ok(vec![stack_dir]);
|
||||||
@@ -85,7 +114,7 @@ impl Stack
|
|||||||
/**
|
/**
|
||||||
* parse stack file
|
* parse stack file
|
||||||
*/
|
*/
|
||||||
fn parse_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");
|
||||||
for entry in stack {
|
for entry in stack {
|
||||||
@@ -95,5 +124,18 @@ impl Stack
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* write stack current stack to file to save it for next execution
|
||||||
|
*/
|
||||||
|
fn write_stack_file(&mut self) -> Result<()> {
|
||||||
|
let mut output = Vec::<&str>::new();
|
||||||
|
for entry in &self.stack {
|
||||||
|
output.push(entry.to_str().expect("failed to convert stack entry to string"));
|
||||||
|
}
|
||||||
|
fs::write(self.path.clone(), output.join("\n"))?;
|
||||||
|
|
||||||
|
return Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
} // end `impl database`
|
} // end `impl database`
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user