implemented parsing, listing and pushing of bookmarks
This commit is contained in:
+10
-3
@@ -4,15 +4,22 @@ export PATH="$PATH:$PWD/target/debug/"
|
|||||||
pid=( $(ps -o ppid) )
|
pid=( $(ps -o ppid) )
|
||||||
arg_pid=" --pid ${pid[-2]} "
|
arg_pid=" --pid ${pid[-2]} "
|
||||||
|
|
||||||
|
__call_navigate() {
|
||||||
|
eval "$(navigate ${arg_pid} $@)"
|
||||||
|
}
|
||||||
|
|
||||||
push() {
|
push() {
|
||||||
\builtin cd -- "$(navigate ${arg_pid} push $@)"
|
__call_navigate "push $@"
|
||||||
}
|
}
|
||||||
|
|
||||||
pop() {
|
pop() {
|
||||||
\builtin cd -- "$(navigate ${arg_pid} pop $@)"
|
__call_navigate "pop $@"
|
||||||
}
|
}
|
||||||
|
|
||||||
stack() {
|
stack() {
|
||||||
echo "$(navigate ${arg_pid} stack $@)"
|
__call_navigate "stack $@"
|
||||||
|
}
|
||||||
|
|
||||||
|
book() {
|
||||||
|
__call_navigate "bookmark $@"
|
||||||
}
|
}
|
||||||
|
|||||||
+33
-9
@@ -38,7 +38,7 @@ pub struct PushArgs {
|
|||||||
pub show_stack: Option<bool>,
|
pub show_stack: Option<bool>,
|
||||||
|
|
||||||
/// change to <path>
|
/// change to <path>
|
||||||
pub path: PathBuf,
|
pub path: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
@@ -70,17 +70,41 @@ pub struct StackArgs {
|
|||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub enum StackAction {
|
pub enum StackAction {
|
||||||
/// clear stack
|
/// clear stack
|
||||||
clear(ClearArgs),
|
clear(EmptyArgs),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
|
||||||
pub struct ClearArgs {}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Args)]
|
#[derive(Debug, Clone, Args)]
|
||||||
pub struct BookmarkArgs {
|
pub struct BookmarkArgs {
|
||||||
/// show stack
|
/// bookmark subcommand
|
||||||
#[arg(short, long)]
|
#[command(subcommand)]
|
||||||
pub show_stack: Option<bool>,
|
pub bookmark_action: Option<BookmarkAction>,
|
||||||
|
|
||||||
pub mark: String,
|
/// name of bookmark to push
|
||||||
|
pub name: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Subcommand)]
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
pub enum BookmarkAction {
|
||||||
|
/// list all bookmarks
|
||||||
|
list(EmptyArgs),
|
||||||
|
|
||||||
|
/// add a bookmark with `book add <name> <path>`
|
||||||
|
add(BookmarkSubArgs),
|
||||||
|
|
||||||
|
/// remove a bookmark by name `book remove <name>`
|
||||||
|
remove(BookmarkSubArgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Args)]
|
||||||
|
pub struct BookmarkSubArgs {
|
||||||
|
/// name of bookmark to add/remove
|
||||||
|
pub name: String,
|
||||||
|
|
||||||
|
/// path of bookmark to add
|
||||||
|
pub path: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// empty struct for subcommands with no arguments
|
||||||
|
#[derive(Debug, Clone, Args)]
|
||||||
|
pub struct EmptyArgs {}
|
||||||
|
|||||||
+38
-22
@@ -3,48 +3,64 @@
|
|||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Error, ErrorKind, Result};
|
use std::io::{Error, Result};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{PathBuf};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use sysinfo::{Pid, System};
|
use std::collections::HashMap;
|
||||||
|
use std::env::var;
|
||||||
const config_dir_path: &str = "~/.config/navigate/";
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub bookmarks: Vec<PathBuf>,
|
pub conf_dir: PathBuf,
|
||||||
|
pub bookmarks: HashMap<String, PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
/// 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 config = Config {
|
let mut bookmarks = Config {
|
||||||
bookmarks: Vec::<PathBuf>::new(),
|
conf_dir: PathBuf::new(),
|
||||||
|
bookmarks: HashMap::<String, PathBuf>::new(),
|
||||||
};
|
};
|
||||||
|
let home_dir = match var("HOME") {
|
||||||
// stack.build_stack()?;
|
Ok(value) => value,
|
||||||
if !config.bookmarks[0].is_dir() {
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
config.bookmarks.remove(0);
|
|
||||||
};
|
};
|
||||||
|
bookmarks.conf_dir = match PathBuf::from_str(&home_dir) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
|
};
|
||||||
|
bookmarks.conf_dir.push(".config/navigate/");
|
||||||
|
bookmarks.build_config()?;
|
||||||
|
|
||||||
Ok(config)
|
Ok(bookmarks)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_config(&mut self) -> Result<()> {
|
fn build_config(&mut self) -> Result<()> {
|
||||||
let config_dir = match PathBuf::from_str(config_dir_path) {
|
let mut bookmark_file = self.conf_dir.clone();
|
||||||
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");
|
bookmark_file.push("bookmarks.conf");
|
||||||
|
|
||||||
if !bookmark_file.is_file() {
|
if !bookmark_file.is_file() {
|
||||||
_ = File::create(bookmark_file.clone())?;
|
_ = File::create(bookmark_file.clone())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut bookmarks = fs::read_to_string(bookmark_file.clone())?;
|
let bookmarks = fs::read_to_string(bookmark_file)?;
|
||||||
// TODO: parse bookmarks
|
let bookmarks = bookmarks.split("\n");
|
||||||
|
for entry in bookmarks {
|
||||||
|
let tokens: Vec<&str> = entry.split("=").collect();
|
||||||
|
if tokens.len() != 2 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let key: String = String::from(tokens[0]);
|
||||||
|
let path = match PathBuf::from_str(tokens[1]) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(err) => return Err(Error::other(err.to_string())),
|
||||||
|
};
|
||||||
|
if !path.is_dir() {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
self.bookmarks.insert(key, path);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
+107
-22
@@ -3,46 +3,74 @@ mod config;
|
|||||||
mod stack;
|
mod stack;
|
||||||
|
|
||||||
use arguments::*;
|
use arguments::*;
|
||||||
use clap::{Parser};
|
use clap::Parser;
|
||||||
|
use config::Config;
|
||||||
use stack::Stack;
|
use stack::Stack;
|
||||||
use std::env::current_dir;
|
use std::env::{current_dir, var};
|
||||||
use std::io::{Error, Result};
|
use std::io::{Error, Result};
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let args = Arguments::parse();
|
let args = match Arguments::try_parse() {
|
||||||
|
Ok(a) => a,
|
||||||
|
Err(e) => {
|
||||||
|
print!("echo '{}'", e);
|
||||||
|
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(_) => return Err(Error::other("-- failed to build stack")),
|
Err(_) => {
|
||||||
|
print!("echo ");
|
||||||
|
return Err(Error::other("-- failed to build stack"));
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
let res = 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::stack(stack_args) => handle_stack(&stack_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),
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
pub fn handle_push(args: &PushArgs, stack: &mut Stack) -> Result<()> {
|
if res.is_err() {
|
||||||
// TODO: handle arguments
|
print!("echo '{}'", res.unwrap_err());
|
||||||
if !args.path.is_dir() {
|
|
||||||
return Err(Error::other("-- invalid path argument"));
|
|
||||||
}
|
}
|
||||||
let current_path = current_dir()?;
|
|
||||||
stack.push_entry(¤t_path)?;
|
|
||||||
println!("{}", args.path.to_str().unwrap());
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_pop(args: &PopArgs, stack: &mut Stack) -> Result<()> {
|
fn handle_push(args: &PushArgs, stack: &mut Stack) -> Result<()> {
|
||||||
|
let path = match args.path.clone() {
|
||||||
|
Some(value) => value,
|
||||||
|
None => {
|
||||||
|
let home_dir = match var("HOME") {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
|
};
|
||||||
|
match PathBuf::from_str(&home_dir) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
push_path(&path, stack)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_pop(_args: &PopArgs, stack: &mut Stack) -> Result<()> {
|
||||||
// TODO: handle arguments
|
// TODO: handle arguments
|
||||||
let path = stack.pop_entry()?;
|
let path = stack.pop_entry()?;
|
||||||
println!("{}", path.to_str().unwrap());
|
println!(
|
||||||
|
"cd -- {}",
|
||||||
|
match path.to_str() {
|
||||||
|
Some(value) => value,
|
||||||
|
None => return Err(Error::other("-- failed to print popped path as string")),
|
||||||
|
}
|
||||||
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_stack(args: &StackArgs, stack: &mut Stack) -> Result<()> {
|
fn handle_stack(args: &StackArgs, stack: &mut Stack) -> Result<()> {
|
||||||
// TODO: handle arguments
|
|
||||||
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(),
|
StackAction::clear(_) => return stack.clear_stack(),
|
||||||
@@ -55,12 +83,69 @@ pub fn handle_stack(args: &StackArgs, stack: &mut Stack) -> Result<()> {
|
|||||||
}
|
}
|
||||||
// print stack to standard output
|
// print stack to standard output
|
||||||
for (n, item) in output.iter().rev().enumerate() {
|
for (n, item) in output.iter().rev().enumerate() {
|
||||||
println!("{} {}", n, item.to_str().unwrap());
|
println!("echo '{} - {}'", n, item.to_str().unwrap());
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_bookmark(args: &BookmarkArgs, stack: &mut Stack) -> Result<()> {
|
fn handle_bookmark(args: &BookmarkArgs, stack: &mut Stack) -> Result<()> {
|
||||||
// TODO: handle arguments
|
let mut config = match Config::new() {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
|
};
|
||||||
|
// if args.bookmark_action.is_some() {
|
||||||
|
if args.bookmark_action.is_some() {
|
||||||
|
match args.bookmark_action.clone().unwrap() {
|
||||||
|
BookmarkAction::list(_) => list_bookmarks(&config)?,
|
||||||
|
BookmarkAction::add(args) => add_bookmarks(&args, &mut config)?,
|
||||||
|
BookmarkAction::remove(args) => remove_bookmarks(&args, &mut config)?,
|
||||||
|
};
|
||||||
|
} else if args.name.is_some() {
|
||||||
|
let path = match config
|
||||||
|
.bookmarks
|
||||||
|
.get(args.name.as_ref().unwrap())
|
||||||
|
{
|
||||||
|
Some(value) => value,
|
||||||
|
None => return Err(Error::other("requested bookmark does not exist")),
|
||||||
|
};
|
||||||
|
push_path(path, stack)?;
|
||||||
|
} else {
|
||||||
|
return Err(Error::other("-- provide either a `subcommand` or a `bookmark name`"));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn list_bookmarks(config: &Config) -> Result<()> {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
for (mark, path) in &config.bookmarks {
|
||||||
|
buffer.push_str(&format!("{} : {}\n", mark, path.to_str().unwrap()));
|
||||||
|
}
|
||||||
|
println!("echo '{}'", buffer);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_bookmarks(_args: &BookmarkSubArgs, _bookmarks: &mut Config) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_bookmarks(_args: &BookmarkSubArgs, _bookmarks: &mut Config) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// push path to stack and print command to navigate to provided path
|
||||||
|
fn push_path(path: &Path, stack: &mut Stack) -> Result<()> {
|
||||||
|
if !path.is_dir() {
|
||||||
|
return Err(Error::other("-- invalid path argument"));
|
||||||
|
}
|
||||||
|
let current_path = current_dir()?;
|
||||||
|
let next_path = path.canonicalize()?;
|
||||||
|
stack.push_entry(¤t_path)?;
|
||||||
|
println!(
|
||||||
|
"cd -- {}",
|
||||||
|
match next_path.to_str() {
|
||||||
|
Some(value) => value,
|
||||||
|
None => return Err(Error::other("-- failed to print provided path as string")),
|
||||||
|
}
|
||||||
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-13
@@ -1,6 +1,6 @@
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Error, ErrorKind, 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};
|
||||||
@@ -58,9 +58,8 @@ impl Stack {
|
|||||||
|
|
||||||
match entry {
|
match entry {
|
||||||
Some(entry) => Ok(entry),
|
Some(entry) => Ok(entry),
|
||||||
None => Err(Error::new(
|
None => Err(Error::other(
|
||||||
ErrorKind::Other,
|
"-- pop failed to retrieve item from stack, it might be empty",
|
||||||
"pop failed to retrieve item from stack",
|
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,12 +72,11 @@ impl Stack {
|
|||||||
self.stack
|
self.stack
|
||||||
.len()
|
.len()
|
||||||
.checked_sub(entry_number)
|
.checked_sub(entry_number)
|
||||||
.expect("requested entry number is out of bounds"),
|
.expect("-- requested entry number is out of bounds"),
|
||||||
) {
|
) {
|
||||||
Some(item) => Ok(item),
|
Some(item) => Ok(item),
|
||||||
None => Err(Error::new(
|
None => Err(Error::other(
|
||||||
ErrorKind::Other,
|
"-- failed to retrieve stack entry by number",
|
||||||
"failed to retrieve stack entry by number",
|
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,7 +84,7 @@ impl Stack {
|
|||||||
/// 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 = PathBuf::from_str("/tmp/navigation/")
|
||||||
.expect("failed to create path object of '/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();
|
||||||
@@ -100,10 +98,10 @@ impl Stack {
|
|||||||
entry
|
entry
|
||||||
.file_name()
|
.file_name()
|
||||||
.to_str()
|
.to_str()
|
||||||
.expect("failed to convert 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 {
|
||||||
@@ -142,7 +140,7 @@ impl Stack {
|
|||||||
output.push(
|
output.push(
|
||||||
entry
|
entry
|
||||||
.to_str()
|
.to_str()
|
||||||
.expect("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"))?;
|
||||||
|
|||||||
Reference in New Issue
Block a user