quicksave
This commit is contained in:
+93
-32
@@ -9,69 +9,98 @@ use std::io::{Error, Result};
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use crate::{RESET_SEQ, STYLES};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
conf_dir: PathBuf,
|
conf_dir: PathBuf,
|
||||||
settings: Settings,
|
pub settings: Settings,
|
||||||
bookmarks: HashMap<String, PathBuf>,
|
bookmarks: HashMap<String, PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Settings {}
|
pub struct Settings {
|
||||||
|
pub general: GeneralSettings,
|
||||||
|
pub styles: StyleSettings,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct GeneralSettings {
|
||||||
|
pub show_stack_on_push: bool,
|
||||||
|
pub show_stack_on_pop: bool,
|
||||||
|
pub show_stack_on_bookmark: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct StyleSettings {
|
||||||
|
pub note: String,
|
||||||
|
pub warning: String,
|
||||||
|
pub error: String,
|
||||||
|
pub stack_number: String,
|
||||||
|
pub stack_seperator: String,
|
||||||
|
pub stack_path: String,
|
||||||
|
pub bookmarks_key: String,
|
||||||
|
pub bookmarks_seperator: String,
|
||||||
|
pub bookmarks_path: String,
|
||||||
|
pub reset: String,
|
||||||
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
|
const CONFIG_FILE_NAME: &str = "navigate.conf";
|
||||||
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 = Config {
|
||||||
conf_dir: PathBuf::new(),
|
conf_dir: PathBuf::new(),
|
||||||
settings: Settings{},
|
settings: Settings {
|
||||||
|
general: GeneralSettings {
|
||||||
|
show_stack_on_push: false,
|
||||||
|
show_stack_on_pop: false,
|
||||||
|
show_stack_on_bookmark: false,
|
||||||
|
},
|
||||||
|
styles: StyleSettings {
|
||||||
|
note: "".to_owned(),
|
||||||
|
warning: "".to_owned(),
|
||||||
|
error: "".to_owned(),
|
||||||
|
stack_number: "".to_owned(),
|
||||||
|
stack_seperator: "".to_owned(),
|
||||||
|
stack_path: "".to_owned(),
|
||||||
|
bookmarks_key: "".to_owned(),
|
||||||
|
bookmarks_seperator: "".to_owned(),
|
||||||
|
bookmarks_path: "".to_owned(),
|
||||||
|
reset: RESET_SEQ.to_owned(),
|
||||||
|
},
|
||||||
|
},
|
||||||
bookmarks: HashMap::<String, PathBuf>::new(),
|
bookmarks: HashMap::<String, PathBuf>::new(),
|
||||||
};
|
};
|
||||||
|
// get home directory path
|
||||||
let home_dir = match var("HOME") {
|
let home_dir = match var("HOME") {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => return Err(Error::other(error.to_string())),
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
};
|
};
|
||||||
|
// create PathBuf object from home dir path
|
||||||
bookmarks.conf_dir = match PathBuf::from_str(&home_dir) {
|
bookmarks.conf_dir = match PathBuf::from_str(&home_dir) {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => return Err(Error::other(error.to_string())),
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
};
|
};
|
||||||
|
// expand home directory path to get configuration directory path
|
||||||
bookmarks.conf_dir.push(".config/navigate/");
|
bookmarks.conf_dir.push(".config/navigate/");
|
||||||
bookmarks.build_config()?;
|
bookmarks.build_bookmarks()?;
|
||||||
|
|
||||||
Ok(bookmarks)
|
Ok(bookmarks)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_bookmarks(&mut self) -> &mut HashMap<String, PathBuf> {
|
/// reads and parses the configuration file
|
||||||
&mut self.bookmarks
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn add_bookmark(&mut self, name: &String, path: &PathBuf) -> Result<()> {
|
|
||||||
if !path.is_dir() {
|
|
||||||
return Err(Error::other(
|
|
||||||
"-- provided path argument does not point to a valid directory",
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
self.bookmarks.insert(name.to_string(), path.to_path_buf());
|
|
||||||
self.write_bookmark_file()?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove_bookmark(&mut self, name: &String) -> Result<()> {
|
|
||||||
if self.bookmarks.contains_key(name) {
|
|
||||||
_ = self.bookmarks.remove(name);
|
|
||||||
self.write_bookmark_file()?;
|
|
||||||
} else {
|
|
||||||
return Err(Error::other(
|
|
||||||
"-- bookmark requested to delete does not exist",
|
|
||||||
));
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn build_config(&mut self) -> Result<()> {
|
fn build_config(&mut self) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// reads and parses the bookmarks file
|
||||||
|
fn build_bookmarks(&mut self) -> Result<()> {
|
||||||
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);
|
||||||
|
|
||||||
@@ -100,6 +129,7 @@ impl Config {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// writes the bookmarks file
|
||||||
fn write_bookmark_file(&self) -> Result<()> {
|
fn write_bookmark_file(&self) -> Result<()> {
|
||||||
let mut file_content = String::new();
|
let mut file_content = String::new();
|
||||||
for (mark, path) in self.bookmarks.iter() {
|
for (mark, path) in self.bookmarks.iter() {
|
||||||
@@ -112,4 +142,35 @@ impl Config {
|
|||||||
fs::write(path, file_content)?;
|
fs::write(path, file_content)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// returns a mutable reference to self.bookmarks
|
||||||
|
pub fn get_bookmarks(&mut self) -> &mut HashMap<String, PathBuf> {
|
||||||
|
&mut self.bookmarks
|
||||||
|
}
|
||||||
|
|
||||||
|
/// adds a key/value pair to bookmarks and writes the bookmarks file
|
||||||
|
pub fn add_bookmark(&mut self, name: &String, path: &PathBuf) -> Result<()> {
|
||||||
|
if !path.is_dir() {
|
||||||
|
return Err(Error::other(
|
||||||
|
"-- provided path argument does not point to a valid directory",
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
self.bookmarks.insert(name.to_string(), path.to_path_buf());
|
||||||
|
self.write_bookmark_file()?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// removes a the entry with key=name if it exists, then writes the bookmarks file
|
||||||
|
pub fn remove_bookmark(&mut self, name: &String) -> Result<()> {
|
||||||
|
if self.bookmarks.contains_key(name) {
|
||||||
|
_ = self.bookmarks.remove(name);
|
||||||
|
self.write_bookmark_file()?;
|
||||||
|
} else {
|
||||||
|
return Err(Error::other(
|
||||||
|
"-- bookmark requested to delete does not exist",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+135
-119
@@ -1,162 +1,178 @@
|
|||||||
use std::io::{Error, Result};
|
use std::io::{Error, Result};
|
||||||
|
|
||||||
|
pub const ESC: &'static str = "\x1B";
|
||||||
|
pub const PREFIX: &'static str = "\x1B[";
|
||||||
|
pub const RESET_ARG: &'static str = "0";
|
||||||
|
pub const TERMINATION: &'static str = "m";
|
||||||
|
pub const RESET_SEQ: &'static str = "\x1B[0m";
|
||||||
|
pub const FG: ColorContext = ColorContext::Foreground;
|
||||||
|
pub const BG: ColorContext = ColorContext::Background;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
pub const STYLES: Styles = Styles {
|
||||||
#[derive(Debug, Clone)]
|
set: StyleCodes {
|
||||||
pub struct Format {
|
bold: "1",
|
||||||
/// escape character to start a sequence
|
dim: "2",
|
||||||
pub escape: String,
|
italic: "3",
|
||||||
|
underline: "4",
|
||||||
|
blink: "5",
|
||||||
|
reverse: "7",
|
||||||
|
invisible: "8",
|
||||||
|
strikethrough: "9",
|
||||||
|
},
|
||||||
|
reset: StyleCodes {
|
||||||
|
bold: "22",
|
||||||
|
dim: "22",
|
||||||
|
italic: "23",
|
||||||
|
underline: "24",
|
||||||
|
blink: "25",
|
||||||
|
reverse: "27",
|
||||||
|
invisible: "28",
|
||||||
|
strikethrough: "29",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
/// prefix = escape + '['
|
pub const COLORS: Colors = Colors {
|
||||||
pub prefix: String,
|
fg: ColorCodes {
|
||||||
|
black: "30",
|
||||||
/// reset both style and color
|
red: "31",
|
||||||
pub reset_all: String,
|
green: "32",
|
||||||
|
yellow: "33",
|
||||||
/// text style
|
blue: "34",
|
||||||
pub style: Styles,
|
magenta: "35",
|
||||||
|
cyan: "36",
|
||||||
/// text color
|
white: "37",
|
||||||
pub color: Colors,
|
extended: "38",
|
||||||
}
|
default: "39",
|
||||||
|
},
|
||||||
|
bg: ColorCodes {
|
||||||
|
black: "40",
|
||||||
|
red: "41",
|
||||||
|
green: "42",
|
||||||
|
yellow: "43",
|
||||||
|
blue: "44",
|
||||||
|
magenta: "45",
|
||||||
|
cyan: "46",
|
||||||
|
white: "47",
|
||||||
|
extended: "48",
|
||||||
|
default: "49",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Styles {
|
pub struct Styles {
|
||||||
/// set style
|
|
||||||
pub set: StyleCodes,
|
pub set: StyleCodes,
|
||||||
|
|
||||||
/// reset style
|
|
||||||
pub reset: StyleCodes,
|
pub reset: StyleCodes,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct StyleCodes {
|
pub struct StyleCodes {
|
||||||
pub bold: String,
|
pub bold: &'static str,
|
||||||
pub dim: String,
|
pub dim: &'static str,
|
||||||
pub italic: String,
|
pub italic: &'static str,
|
||||||
pub underline: String,
|
pub underline: &'static str,
|
||||||
pub blink: String,
|
pub blink: &'static str,
|
||||||
pub reverse: String,
|
pub reverse: &'static str,
|
||||||
pub invisible: String,
|
pub invisible: &'static str,
|
||||||
pub strikethrough: String,
|
pub strikethrough: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Colors {
|
pub struct Colors {
|
||||||
pub foreground: ColorCodes,
|
pub fg: ColorCodes,
|
||||||
pub background: ColorCodes,
|
pub bg: ColorCodes,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ColorCodes {
|
pub struct ColorCodes {
|
||||||
pub black: String,
|
pub black: &'static str,
|
||||||
pub red: String,
|
pub red: &'static str,
|
||||||
pub green: String,
|
pub green: &'static str,
|
||||||
pub yellow: String,
|
pub yellow: &'static str,
|
||||||
pub blue: String,
|
pub blue: &'static str,
|
||||||
pub magenta: String,
|
pub magenta: &'static str,
|
||||||
pub cyan: String,
|
pub cyan: &'static str,
|
||||||
pub white: String,
|
pub white: &'static str,
|
||||||
pub default: String,
|
pub extended: &'static str,
|
||||||
|
pub default: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
impl Format {
|
#[derive(Debug, Clone)]
|
||||||
pub const ESC: &str = "\x1B";
|
pub enum ColorContext {
|
||||||
pub const PREFIX: &str = "\x1B[";
|
Foreground,
|
||||||
pub const TERMINATION: &str = "m";
|
Background,
|
||||||
pub const RESET_STYLE: &str = "\x1B[0m";
|
|
||||||
|
|
||||||
pub fn new() -> Format {
|
|
||||||
Format {
|
|
||||||
escape: String::from(Self::ESC),
|
|
||||||
prefix: String::from(Self::PREFIX),
|
|
||||||
reset_all: String::from("0"),
|
|
||||||
style: Styles {
|
|
||||||
set: StyleCodes {
|
|
||||||
bold: String::from("1"),
|
|
||||||
dim: String::from("2"),
|
|
||||||
italic: String::from("3"),
|
|
||||||
underline: String::from("4"),
|
|
||||||
blink: String::from("5"),
|
|
||||||
reverse: String::from("7"),
|
|
||||||
invisible: String::from("8"),
|
|
||||||
strikethrough: String::from("9"),
|
|
||||||
},
|
|
||||||
reset: StyleCodes {
|
|
||||||
bold: String::from("22"),
|
|
||||||
dim: String::from("22"),
|
|
||||||
italic: String::from("23"),
|
|
||||||
underline: String::from("24"),
|
|
||||||
blink: String::from("25"),
|
|
||||||
reverse: String::from("27"),
|
|
||||||
invisible: String::from("28"),
|
|
||||||
strikethrough: String::from("29"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
color: Colors {
|
|
||||||
foreground: ColorCodes {
|
|
||||||
black: String::from("30"),
|
|
||||||
red: String::from("31"),
|
|
||||||
green: String::from("32"),
|
|
||||||
yellow: String::from("33"),
|
|
||||||
blue: String::from("34"),
|
|
||||||
magenta: String::from("35"),
|
|
||||||
cyan: String::from("36"),
|
|
||||||
white: String::from("37"),
|
|
||||||
default: String::from("39"),
|
|
||||||
},
|
|
||||||
background: ColorCodes {
|
|
||||||
black: String::from("40"),
|
|
||||||
red: String::from("41"),
|
|
||||||
green: String::from("42"),
|
|
||||||
yellow: String::from("43"),
|
|
||||||
blue: String::from("44"),
|
|
||||||
magenta: String::from("45"),
|
|
||||||
cyan: String::from("46"),
|
|
||||||
white: String::from("47"),
|
|
||||||
default: String::from("49"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// generates a common style sequence of format
|
||||||
|
/// `\x1B[<styles>;<foreground-color>;<background-color>m`
|
||||||
|
/// all elements are optional, if none is supplied the function returns an error
|
||||||
pub fn generate_style_sequence(
|
pub fn generate_style_sequence(
|
||||||
&self,
|
style: Option<Vec<&str>>,
|
||||||
style: Option<&Vec<String>>,
|
foreground: Option<&str>,
|
||||||
foreground: Option<&String>,
|
background: Option<&str>,
|
||||||
background: Option<&String>,
|
) -> String {
|
||||||
) -> Result<String> {
|
// assemble sequence as vector of string
|
||||||
if style.is_none() && foreground.is_none() && background.is_none() {
|
// this way the semicolons to separate arguments
|
||||||
return Err(Error::other("-- generate_style_sequence called without arguments"));
|
// can be inserted with 'join()'
|
||||||
}
|
let mut sequence = PREFIX.to_owned();
|
||||||
|
let mut arguments = Vec::<String>::new();
|
||||||
// assemble sequence if called with arguments
|
|
||||||
let mut sequence = Vec::<String>::new();
|
|
||||||
if let Some(item) = style {
|
if let Some(item) = style {
|
||||||
sequence.push_str(item);
|
for entry in item {
|
||||||
sequence.push(';');
|
arguments.push(entry.to_owned());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if let Some(item) = foreground {
|
if let Some(item) = foreground {
|
||||||
sequence.push_str(item);
|
arguments.push(item.to_owned());
|
||||||
sequence.push(';');
|
|
||||||
}
|
}
|
||||||
if let Some(item) = background {
|
if let Some(item) = background {
|
||||||
sequence.push_str(item);
|
arguments.push(item.to_owned());
|
||||||
sequence.push(';');
|
|
||||||
}
|
}
|
||||||
sequence.pop(); // remove final semicolon
|
|
||||||
sequence.push_str(Self::TERMINATION); // terminate sequence
|
|
||||||
|
|
||||||
|
// panic if no arguments provided since this is a programming mistake
|
||||||
|
// which should not
|
||||||
|
if arguments.is_empty() {
|
||||||
|
panic!("no arguments provided to 'generate_style_sequence()'");
|
||||||
|
}
|
||||||
|
sequence.push_str(&arguments.join(";"));
|
||||||
|
sequence.push_str(TERMINATION); // terminate sequence with the termination character
|
||||||
sequence
|
sequence
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_rgb_sequence(&self, ) -> String {
|
/// generates a 256 color sequence
|
||||||
|
/// see `generate_rgb_sequence(..)` for details
|
||||||
|
pub fn generate_256color_sequence(context: ColorContext, color: u8) -> String {
|
||||||
|
let mut sequence = PREFIX.to_owned();
|
||||||
|
// choose context
|
||||||
|
match context {
|
||||||
|
ColorContext::Foreground => sequence.push_str(COLORS.fg.extended),
|
||||||
|
ColorContext::Background => sequence.push_str(COLORS.bg.extended),
|
||||||
|
};
|
||||||
|
// make it a rgb sequence
|
||||||
|
sequence.push_str(";5;");
|
||||||
|
sequence.push_str(&format!("{color}m"));
|
||||||
|
sequence
|
||||||
|
}
|
||||||
|
|
||||||
String::from("hi")
|
/// generates a rgb color sequence
|
||||||
}
|
/// **note**: not all terminal emulators support rgb colors
|
||||||
|
///
|
||||||
|
/// rgb sequences are built the same as 256 color sequences:
|
||||||
|
/// `\x1B[<context>;2;<r>;<g>;<b>m`
|
||||||
|
/// where *context* is either '38' or '48' for foreground and background respectively
|
||||||
|
/// and *r,g,b* are the values of each color channel
|
||||||
|
pub fn generate_rgb_sequence(context: ColorContext, red: u8, green: u8, blue: u8) -> String {
|
||||||
|
let mut sequence = PREFIX.to_owned();
|
||||||
|
// choose context
|
||||||
|
match context {
|
||||||
|
ColorContext::Foreground => sequence.push_str(COLORS.fg.extended),
|
||||||
|
ColorContext::Background => sequence.push_str(COLORS.bg.extended),
|
||||||
|
};
|
||||||
|
// make it a rgb sequence
|
||||||
|
sequence.push_str(";2;");
|
||||||
|
sequence.push_str(&format!("{red};{green};{blue}m"));
|
||||||
|
sequence
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-28
@@ -5,8 +5,8 @@ mod stack;
|
|||||||
|
|
||||||
use arguments::*;
|
use arguments::*;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use config::Config;
|
use config::*;
|
||||||
use format::Format;
|
use format::*;
|
||||||
use stack::Stack;
|
use stack::Stack;
|
||||||
use std::env::{current_dir, var};
|
use std::env::{current_dir, var};
|
||||||
use std::io::{Error, Result};
|
use std::io::{Error, Result};
|
||||||
@@ -14,45 +14,51 @@ use std::path::{Path, PathBuf};
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let format: Format = format::Format::new();
|
let style_error =
|
||||||
|
generate_style_sequence(Some(vec![STYLES.set.bold]), Some(COLORS.fg.red), None);
|
||||||
let args = match Arguments::try_parse() {
|
let args = match Arguments::try_parse() {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
print!(
|
print!("echo '{}{}{}' && false", style_error, e, RESET_SEQ);
|
||||||
"echo '{}{}m{}{}{}m'",
|
return Ok(());
|
||||||
format.prefix, format.color.foreground.red, e, format.prefix, format.reset_all
|
}
|
||||||
);
|
};
|
||||||
|
let mut config = match Config::new() {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(error) => {
|
||||||
|
print!("echo '{}{}{}' && false", style_error, error, RESET_SEQ);
|
||||||
return Ok(());
|
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(_) => {
|
||||||
print!("echo ");
|
print!(
|
||||||
return Err(Error::other("-- failed to build stack"));
|
"echo '{}-- failed to build stack{}' && false",
|
||||||
|
style_error, RESET_SEQ
|
||||||
|
);
|
||||||
|
return Err(Error::other(""));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let res = match args.action {
|
let res = match args.action {
|
||||||
Action::push(push_args) => handle_push(&push_args, &mut stack),
|
Action::push(push_args) => handle_push(&push_args, &mut config, &mut stack),
|
||||||
Action::pop(pop_args) => handle_pop(&pop_args, &mut stack),
|
Action::pop(pop_args) => handle_pop(&pop_args, &mut config, &mut stack),
|
||||||
Action::stack(stack_args) => handle_stack(&stack_args, &mut stack),
|
Action::stack(stack_args) => handle_stack(&stack_args, &mut config, &mut stack),
|
||||||
Action::bookmark(bookmark_args) => handle_bookmark(&bookmark_args, &mut stack),
|
Action::bookmark(bookmark_args) => handle_bookmark(&bookmark_args, &mut config, &mut stack),
|
||||||
};
|
};
|
||||||
|
|
||||||
if res.is_err() {
|
if res.is_err() {
|
||||||
print!(
|
print!(
|
||||||
"echo '{}{}m{}{}{}m'",
|
"echo '{}{}{}' && false",
|
||||||
format.prefix,
|
style_error,
|
||||||
format.color.foreground.red,
|
|
||||||
res.unwrap_err(),
|
res.unwrap_err(),
|
||||||
format.prefix,
|
RESET_SEQ,
|
||||||
format.reset_all
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_push(args: &PushArgs, stack: &mut Stack) -> Result<()> {
|
fn handle_push(args: &PushArgs, _config: &mut 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 => {
|
||||||
@@ -70,7 +76,7 @@ fn handle_push(args: &PushArgs, stack: &mut Stack) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_pop(args: &PopArgs, stack: &mut Stack) -> Result<()> {
|
fn handle_pop(args: &PopArgs, _config: &mut 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 -- {}",
|
||||||
@@ -82,10 +88,10 @@ fn handle_pop(args: &PopArgs, stack: &mut Stack) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_stack(args: &StackArgs, stack: &mut Stack) -> Result<()> {
|
fn handle_stack(args: &StackArgs, config: &mut 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(),
|
StackAction::clear(_) => return stack.clear_stack(config),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// retrieve stack
|
// retrieve stack
|
||||||
@@ -94,11 +100,7 @@ fn handle_stack(args: &StackArgs, stack: &mut Stack) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_bookmark(args: &BookmarkArgs, stack: &mut Stack) -> Result<()> {
|
fn handle_bookmark(args: &BookmarkArgs, config: &mut Config, stack: &mut Stack) -> Result<()> {
|
||||||
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() {
|
||||||
if args.bookmark_action.is_some() {
|
if args.bookmark_action.is_some() {
|
||||||
match args.bookmark_action.clone().unwrap() {
|
match args.bookmark_action.clone().unwrap() {
|
||||||
@@ -109,7 +111,7 @@ fn handle_bookmark(args: &BookmarkArgs, stack: &mut Stack) -> Result<()> {
|
|||||||
} 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 config.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")),
|
||||||
};
|
};
|
||||||
push_path(path, stack)?;
|
push_path(path, stack)?;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
+5
-3
@@ -4,7 +4,7 @@ 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::Settings;
|
use super::config::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Stack {
|
pub struct Stack {
|
||||||
@@ -39,8 +39,10 @@ impl Stack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// clear stack by deleting the associated stack file
|
/// clear stack by deleting the associated stack file
|
||||||
pub fn clear_stack(&mut self) -> Result<()> {
|
pub fn clear_stack(&mut self, config: &mut 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);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// push entry to stack
|
/// push entry to stack
|
||||||
|
|||||||
Reference in New Issue
Block a user