added src/output.rs to handle command output
adjusted todo.md
This commit is contained in:
+16
-31
@@ -1,5 +1,4 @@
|
|||||||
//! handle the config file and bookmarks stored
|
//! implements a struct and methods for bookmarks
|
||||||
//! in said config file
|
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
@@ -14,50 +13,32 @@ use config_parser::{make_padding_string, apply_format};
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Bookmarks {
|
pub struct Bookmarks {
|
||||||
conf_dir: PathBuf,
|
|
||||||
bookmarks: BTreeMap<String, PathBuf>,
|
bookmarks: BTreeMap<String, PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bookmarks {
|
impl Bookmarks {
|
||||||
const BOOKMARK_FILE_NAME: &str = "bookmarks.conf";
|
const BOOKMARK_FILE_PATH: &str = "navigate/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 = Bookmarks {
|
let mut bookmarks = Bookmarks {
|
||||||
conf_dir: PathBuf::new(),
|
|
||||||
bookmarks: BTreeMap::<String, PathBuf>::new(),
|
bookmarks: BTreeMap::<String, PathBuf>::new(),
|
||||||
};
|
};
|
||||||
// get home directory path
|
// get home directory path
|
||||||
bookmarks.conf_dir = match config_dir() {
|
let mut bookmark_file = match config_dir() {
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
None => return Err(Error::other("-- failed to find configuration directory")),
|
None => return Err(Error::other("-- failed to find configuration directory")),
|
||||||
};
|
};
|
||||||
// expand home directory path to get configuration directory path
|
// expand home directory path to get configuration directory path
|
||||||
bookmarks.conf_dir.push("navigate/");
|
bookmark_file.push(Self::BOOKMARK_FILE_PATH);
|
||||||
bookmarks.build_bookmarks()?;
|
|
||||||
|
|
||||||
Ok(bookmarks)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// reads and parses the bookmarks file
|
|
||||||
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();
|
|
||||||
|
|
||||||
bookmark_file.push(Self::BOOKMARK_FILE_NAME);
|
|
||||||
|
|
||||||
// check if bookmarks file exists, if not create it
|
// 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())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let bookmarks = fs::read_to_string(bookmark_file)?;
|
let bookmarks_str = fs::read_to_string(bookmark_file)?;
|
||||||
let bookmarks = bookmarks.lines();
|
for entry in bookmarks_str.lines() {
|
||||||
for entry in bookmarks {
|
|
||||||
let tokens: Vec<&str> = entry.split("=").collect();
|
let tokens: Vec<&str> = entry.split("=").collect();
|
||||||
if tokens.len() != 2 {
|
if tokens.len() != 2 {
|
||||||
continue;
|
continue;
|
||||||
@@ -70,10 +51,9 @@ impl Bookmarks {
|
|||||||
if !path.is_dir() {
|
if !path.is_dir() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
self.bookmarks.insert(key, path);
|
bookmarks.bookmarks.insert(key, path);
|
||||||
}
|
}
|
||||||
|
Ok(bookmarks)
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// returns path of bookmark if it exists
|
/// returns path of bookmark if it exists
|
||||||
@@ -114,7 +94,7 @@ impl Bookmarks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// formats and prints bookmarks to string
|
/// formats and prints bookmarks to string
|
||||||
pub fn to_formatted_string(&self, config: &Settings) -> Result<String> {
|
pub fn to_formatted_string(&self, config: &Config) -> Result<String> {
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
|
|
||||||
if self.bookmarks.is_empty() {
|
if self.bookmarks.is_empty() {
|
||||||
@@ -149,8 +129,13 @@ impl Bookmarks {
|
|||||||
file_content.push_str(&format!("{}={}\n", mark, path.to_str().unwrap()));
|
file_content.push_str(&format!("{}={}\n", mark, path.to_str().unwrap()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut path = self.conf_dir.clone();
|
let path = match config_dir() {
|
||||||
path.push(Self::BOOKMARK_FILE_NAME);
|
Some(mut value) => {
|
||||||
|
value.push(Self::BOOKMARK_FILE_PATH);
|
||||||
|
value
|
||||||
|
}
|
||||||
|
None => return Err(Error::other("-- failed to find configuration directory")),
|
||||||
|
};
|
||||||
|
|
||||||
fs::write(path, file_content)?;
|
fs::write(path, file_content)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
+31
-81
@@ -6,17 +6,10 @@
|
|||||||
use dirs::config_dir;
|
use dirs::config_dir;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::{Error, Result};
|
use std::io::{Error, Result};
|
||||||
use std::path::PathBuf;
|
|
||||||
use config_parser::*;
|
use config_parser::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct Config {
|
|
||||||
conf_file: PathBuf,
|
|
||||||
pub settings: Settings,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, ConfigParser)]
|
#[derive(Debug, Clone, Default, ConfigParser)]
|
||||||
pub struct Settings {
|
pub struct Config {
|
||||||
#[nested_config]
|
#[nested_config]
|
||||||
pub general: GeneralSettings,
|
pub general: GeneralSettings,
|
||||||
#[nested_config]
|
#[nested_config]
|
||||||
@@ -41,6 +34,10 @@ pub struct FormatSettings {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Default, ConfigParser)]
|
#[derive(Debug, Clone, Default, ConfigParser)]
|
||||||
pub struct StyleSettings {
|
pub struct StyleSettings {
|
||||||
|
#[style_config]
|
||||||
|
pub warning_style: String,
|
||||||
|
#[style_config]
|
||||||
|
pub error_style: String,
|
||||||
#[style_config]
|
#[style_config]
|
||||||
pub stack_number_style: String,
|
pub stack_number_style: String,
|
||||||
#[style_config]
|
#[style_config]
|
||||||
@@ -61,30 +58,29 @@ impl Config {
|
|||||||
/// generates and populates a new instance of Config
|
/// generates and populates a new instance of Config
|
||||||
pub fn new(styles_as_ansi_sequences: bool) -> Result<Self> {
|
pub fn new(styles_as_ansi_sequences: bool) -> Result<Self> {
|
||||||
let mut config = Config {
|
let mut config = Config {
|
||||||
conf_file: PathBuf::new(),
|
general: GeneralSettings {
|
||||||
settings: Settings {
|
show_stack_on_push: false,
|
||||||
general: GeneralSettings {
|
show_stack_on_pop: false,
|
||||||
show_stack_on_push: false,
|
show_books_on_bookmark: false,
|
||||||
show_stack_on_pop: false,
|
},
|
||||||
show_books_on_bookmark: false,
|
format: FormatSettings {
|
||||||
},
|
bookmarks_separator: String::new(),
|
||||||
format: FormatSettings {
|
stack_separator: String::new(),
|
||||||
bookmarks_separator: String::new(),
|
align_separators: false,
|
||||||
stack_separator: String::new(),
|
},
|
||||||
align_separators: false,
|
styles: StyleSettings {
|
||||||
},
|
warning_style: String::new(),
|
||||||
styles: StyleSettings {
|
error_style: String::new(),
|
||||||
stack_number_style: String::new(),
|
stack_number_style: String::new(),
|
||||||
stack_separator_style: String::new(),
|
stack_separator_style: String::new(),
|
||||||
stack_path_style: String::new(),
|
stack_path_style: String::new(),
|
||||||
bookmarks_name_style: String::new(),
|
bookmarks_name_style: String::new(),
|
||||||
bookmarks_seperator_style: String::new(),
|
bookmarks_seperator_style: String::new(),
|
||||||
bookmarks_path_style: String::new(),
|
bookmarks_path_style: String::new(),
|
||||||
},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
// get configuration directory
|
// get configuration directory
|
||||||
config.conf_file = match config_dir() {
|
let mut conf_file = match config_dir() {
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
None => {
|
None => {
|
||||||
return Err(Error::other(
|
return Err(Error::other(
|
||||||
@@ -93,27 +89,21 @@ impl Config {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
// expand path to configuration file
|
// expand path to configuration file
|
||||||
config
|
conf_file.push(format!("navigate/{}", Self::CONFIG_FILE_NAME));
|
||||||
.conf_file
|
|
||||||
.push(format!("navigate/{}", Self::CONFIG_FILE_NAME));
|
|
||||||
|
|
||||||
// parse configuration file and populate config struct
|
// parse configuration file and populate config struct
|
||||||
if config.conf_file.is_file() {
|
if conf_file.is_file() {
|
||||||
let config_str = match fs::read_to_string(&config.conf_file) {
|
let config_str = match fs::read_to_string(&conf_file) {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => return Err(error),
|
Err(error) => return Err(error),
|
||||||
};
|
};
|
||||||
_ = config.settings.parse_from_string(&config_str);
|
_ = config.parse_from_string(&config_str);
|
||||||
} else {
|
} else {
|
||||||
// TODO: write default configuration
|
// TODO: write default configuration
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: ALSDKJFJFJASDkk
|
|
||||||
//config.set_default_settings()?;
|
|
||||||
//config.parse_color_settings()?;
|
|
||||||
|
|
||||||
if styles_as_ansi_sequences {
|
if styles_as_ansi_sequences {
|
||||||
config.settings.to_ansi_sequences()?;
|
config.to_ansi_sequences()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(config)
|
Ok(config)
|
||||||
@@ -121,47 +111,7 @@ impl Config {
|
|||||||
|
|
||||||
/// formats and prints config to string
|
/// formats and prints config to string
|
||||||
pub fn to_formatted_string(&self) -> Result<String> {
|
pub fn to_formatted_string(&self) -> Result<String> {
|
||||||
Ok(format!("{:#?}", self.settings))
|
Ok(format!("{:#?}", self))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// reads and parses the configuration file
|
|
||||||
fn build_settings(&mut self) -> Result<()> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// sets defaults for settings not found in the configuration file
|
|
||||||
fn set_default_settings(&mut self) -> Result<()> {
|
|
||||||
let default_separator = " - ".to_owned();
|
|
||||||
let default_number_color = "default".to_owned();
|
|
||||||
let default_separator_color = "cyan".to_owned();
|
|
||||||
let default_path_color = "default".to_owned();
|
|
||||||
|
|
||||||
if self.settings.format.stack_separator.is_empty() {
|
|
||||||
self.settings.format.stack_separator = default_separator.clone();
|
|
||||||
}
|
|
||||||
if self.settings.format.bookmarks_separator.is_empty() {
|
|
||||||
self.settings.format.bookmarks_separator = default_separator.clone();
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.settings.styles.stack_number_style.is_empty() {
|
|
||||||
self.settings.styles.stack_number_style = default_number_color.clone();
|
|
||||||
}
|
|
||||||
if self.settings.styles.stack_separator_style.is_empty() {
|
|
||||||
self.settings.styles.stack_separator_style = default_separator_color.clone();
|
|
||||||
}
|
|
||||||
if self.settings.styles.stack_path_style.is_empty() {
|
|
||||||
self.settings.styles.stack_path_style = default_path_color.clone();
|
|
||||||
}
|
|
||||||
if self.settings.styles.bookmarks_name_style.is_empty() {
|
|
||||||
self.settings.styles.bookmarks_name_style = default_number_color.clone();
|
|
||||||
}
|
|
||||||
if self.settings.styles.bookmarks_seperator_style.is_empty() {
|
|
||||||
self.settings.styles.bookmarks_seperator_style = default_separator_color.clone();
|
|
||||||
}
|
|
||||||
if self.settings.styles.bookmarks_path_style.is_empty() {
|
|
||||||
self.settings.styles.bookmarks_path_style = default_path_color.clone();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+73
-66
@@ -2,6 +2,7 @@ mod arguments;
|
|||||||
mod config;
|
mod config;
|
||||||
mod bookmarks;
|
mod bookmarks;
|
||||||
mod stack;
|
mod stack;
|
||||||
|
mod output;
|
||||||
mod debug;
|
mod debug;
|
||||||
|
|
||||||
use arguments::*;
|
use arguments::*;
|
||||||
@@ -9,6 +10,7 @@ use clap::Parser;
|
|||||||
use config::*;
|
use config::*;
|
||||||
use bookmarks::*;
|
use bookmarks::*;
|
||||||
use config_parser::*;
|
use config_parser::*;
|
||||||
|
use output::Output;
|
||||||
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};
|
||||||
@@ -16,59 +18,58 @@ use std::path::{Path, PathBuf};
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let style_error =
|
let mut output = Output::new();
|
||||||
generate_style_sequence(Some(STYLES.set.bold), Some(COLORS.fg.red), None);
|
|
||||||
let args = match Arguments::try_parse() {
|
|
||||||
Ok(a) => a,
|
|
||||||
Err(e) => {
|
|
||||||
print!("echo '{}{}{}' && false", style_error, e, RESET_SEQ);
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
let config = match Config::new(true) {
|
let config = match Config::new(true) {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
print!("echo '{}{}{}' && false", style_error, error, RESET_SEQ);
|
// config object is not ready at this point so the style
|
||||||
|
// has to be created by hand
|
||||||
|
print!("echo '{}{}{}' && false", generate_style_sequence(None, Some(COLORS.fg.red), None), error, RESET_SEQ);
|
||||||
|
return Ok(())
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let args = match Arguments::try_parse() {
|
||||||
|
Ok(a) => a,
|
||||||
|
Err(error) => {
|
||||||
|
output.push_error(&error.to_string());
|
||||||
|
output.print_output(&config);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let mut bookmarks = match Bookmarks::new() {
|
let mut bookmarks = match Bookmarks::new() {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
print!("echo '{}{}{}' && false", style_error, error, RESET_SEQ);
|
output.push_error(&error.to_string());
|
||||||
|
output.print_output(&config);
|
||||||
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!(
|
output.push_error(&"-- failed to build stack".to_string());
|
||||||
"echo '{}-- failed to build stack{}' && false",
|
|
||||||
style_error, RESET_SEQ
|
|
||||||
);
|
|
||||||
return Err(Error::other(""));
|
return Err(Error::other(""));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let res = match args.action {
|
let res = match args.action {
|
||||||
Action::push(push_args) => handle_push(&push_args, &config, &mut stack),
|
Action::push(push_args) => handle_push(&push_args, &config, &mut stack, &mut output),
|
||||||
Action::pop(pop_args) => handle_pop(&pop_args, &config, &mut stack),
|
Action::pop(pop_args) => handle_pop(&pop_args, &config, &mut stack, &mut output),
|
||||||
Action::stack(stack_args) => handle_stack(&stack_args, &config, &mut stack),
|
Action::stack(stack_args) => handle_stack(&stack_args, &config, &mut stack, &mut output),
|
||||||
Action::bookmark(bookmark_args) => handle_bookmark(&bookmark_args, &config, &mut bookmarks, &mut stack),
|
Action::bookmark(bookmark_args) => handle_bookmark(&bookmark_args, &config, &mut bookmarks, &mut stack, &mut output),
|
||||||
// Action::config(config_args) => handle_config(&config_args, &config),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if res.is_err() {
|
if res.is_err() {
|
||||||
print!(
|
output.push_error(&res.unwrap_err().to_string());
|
||||||
"echo '{}{}{}' && false",
|
output.push_command(&"false".to_owned());
|
||||||
style_error,
|
|
||||||
res.unwrap_err(),
|
|
||||||
RESET_SEQ,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// print output and command
|
||||||
|
output.print_output(&config);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_push(args: &PushArgs, config: &Config, stack: &mut Stack) -> Result<()> {
|
fn handle_push(args: &PushArgs, config: &Config, stack: &mut Stack, output: &mut Output) -> Result<()> {
|
||||||
let path = match args.path.clone() {
|
let path = match args.path.clone() {
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
None => {
|
None => {
|
||||||
@@ -82,11 +83,11 @@ fn handle_push(args: &PushArgs, config: &Config, stack: &mut Stack) -> Result<()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
push_path(&path, stack, config)?;
|
push_path(&path, stack, config, output)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_pop(args: &PopArgs, config: &Config, stack: &mut Stack) -> Result<()> {
|
fn handle_pop(args: &PopArgs, config: &Config, stack: &mut Stack, output: &mut Output) -> Result<()> {
|
||||||
let mut num : Option<usize> = None;
|
let mut num : Option<usize> = None;
|
||||||
if let Some(a) = &args.action {
|
if let Some(a) = &args.action {
|
||||||
match a {
|
match a {
|
||||||
@@ -96,46 +97,44 @@ fn handle_pop(args: &PopArgs, config: &Config, stack: &mut Stack) -> Result<()>
|
|||||||
num = Some(*n);
|
num = Some(*n);
|
||||||
}
|
}
|
||||||
let path = stack.pop_entry(num)?;
|
let path = stack.pop_entry(num)?;
|
||||||
if config.settings.general.show_stack_on_push {
|
if config.general.show_stack_on_push {
|
||||||
print!("echo '{}' && ", stack.to_formatted_string(&config.settings)?);
|
output.push_info(&stack.to_formatted_string(config)?);
|
||||||
}
|
}
|
||||||
println!(
|
output.push_command(&format!("cd -- {}", match path.to_str() {
|
||||||
"cd -- {}",
|
Some(value) => value,
|
||||||
match path.to_str() {
|
None => return Err(Error::other("-- failed to print popped path as string")),
|
||||||
Some(value) => value,
|
}));
|
||||||
None => return Err(Error::other("-- failed to print popped path as string")),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_stack(args: &StackArgs, config: &Config, stack: &mut Stack) -> Result<()> {
|
fn handle_stack(args: &StackArgs, config: &Config, stack: &mut Stack, output: &mut Output) -> 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(&config.settings),
|
StackAction::clear(_) => {
|
||||||
|
stack.clear_stack()?;
|
||||||
|
output.push_info(&"stack cleared.".to_owned());
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// retrieve stack
|
// retrieve stack
|
||||||
let output: String = stack.to_formatted_string(&config.settings)?;
|
output.push_info(&stack.to_formatted_string(config)?);
|
||||||
print!("echo '{}'", output);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_bookmark(args: &BookmarkArgs, config: &Config, bookmarks: &mut Bookmarks, stack: &mut Stack) -> Result<()> {
|
fn handle_bookmark(args: &BookmarkArgs, config: &Config, bookmarks: &mut Bookmarks, stack: &mut Stack, output: &mut Output) -> Result<()> {
|
||||||
// if args.bookmark_action.is_some() {
|
// if args.bookmark_action.is_some() {
|
||||||
if let Some(action) = &args.bookmark_action {
|
if let Some(action) = &args.bookmark_action {
|
||||||
match action {
|
match action {
|
||||||
BookmarkAction::list(_) => list_bookmarks(config, bookmarks)?,
|
BookmarkAction::list(_) => list_bookmarks(config, bookmarks, output)?,
|
||||||
BookmarkAction::add(args) => add_bookmarks(args, config, bookmarks)?,
|
BookmarkAction::add(args) => add_bookmarks(args, config, bookmarks, output)?,
|
||||||
BookmarkAction::remove(args) => remove_bookmarks(args, config, bookmarks)?,
|
BookmarkAction::remove(args) => remove_bookmarks(args, config, bookmarks, output)?,
|
||||||
};
|
};
|
||||||
} else if args.name.is_some() { // handle `change to bookmark`
|
} else if args.name.is_some() { // handle `change to bookmark`
|
||||||
let path = bookmarks.get_path_by_name(args.name.as_ref().unwrap())?;
|
let path = bookmarks.get_path_by_name(args.name.as_ref().unwrap())?;
|
||||||
push_path(&path, stack, config)?;
|
push_path(&path, stack, config, output)?;
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::other(
|
list_bookmarks(config, bookmarks, output)?;
|
||||||
"-- provide either a `subcommand` or a `bookmark name`",
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -147,13 +146,12 @@ fn handle_bookmark(args: &BookmarkArgs, config: &Config, bookmarks: &mut Bookmar
|
|||||||
// Ok(())
|
// Ok(())
|
||||||
// }
|
// }
|
||||||
|
|
||||||
fn list_bookmarks(config: &Config, bookmarks: &mut Bookmarks) -> Result<()> {
|
fn list_bookmarks(config: &Config, bookmarks: &mut Bookmarks, output: &mut Output) -> Result<()> {
|
||||||
let output = bookmarks.to_formatted_string(&config.settings)?;
|
output.push_info(&bookmarks.to_formatted_string(config)?);
|
||||||
println!("echo '{}'", output);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookmarks) -> Result<()> {
|
fn add_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookmarks, output: &mut Output) -> Result<()> {
|
||||||
let mut path = match args.path.clone() {
|
let mut path = match args.path.clone() {
|
||||||
Some(value) => value,
|
Some(value) => value,
|
||||||
None => return Err(Error::other("-- missing path argument")),
|
None => return Err(Error::other("-- missing path argument")),
|
||||||
@@ -163,31 +161,40 @@ fn add_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookma
|
|||||||
Err(error) => return Err(Error::other(error.to_string())),
|
Err(error) => return Err(Error::other(error.to_string())),
|
||||||
};
|
};
|
||||||
bookmarks.add_bookmark(&args.name, &path)?;
|
bookmarks.add_bookmark(&args.name, &path)?;
|
||||||
|
|
||||||
|
if config.general.show_books_on_bookmark {
|
||||||
|
output.push_info(&bookmarks.to_formatted_string(config)?);
|
||||||
|
} else {
|
||||||
|
output.push_info(&format!("added bookmark `{}{}{}`.", generate_style_sequence(Some(STYLES.set.bold), None, None), args.name, RESET_SEQ));
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookmarks) -> Result<()> {
|
fn remove_bookmarks(args: &BookmarkSubArgs, config: &Config, bookmarks: &mut Bookmarks, output: &mut Output) -> Result<()> {
|
||||||
bookmarks.remove_bookmark(&args.name)?;
|
bookmarks.remove_bookmark(&args.name)?;
|
||||||
|
|
||||||
|
if config.general.show_books_on_bookmark {
|
||||||
|
output.push_info(&bookmarks.to_formatted_string(config)?);
|
||||||
|
} else {
|
||||||
|
output.push_info(&format!("remove bookmark `{}{}{}`.", generate_style_sequence(Some(STYLES.set.bold), None, None), args.name, RESET_SEQ));
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// push path to stack and print command to navigate to provided path
|
/// push path to stack and print command to navigate to provided path
|
||||||
fn push_path(path: &Path, stack: &mut Stack, config: &Config) -> Result<()> {
|
fn push_path(path: &Path, stack: &mut Stack, config: &Config, output: &mut Output) -> Result<()> {
|
||||||
if !path.is_dir() {
|
if !path.is_dir() {
|
||||||
return Err(Error::other("-- invalid path argument"));
|
return Err(Error::other("-- invalid path argument"));
|
||||||
}
|
}
|
||||||
let current_path = current_dir()?;
|
let current_path = current_dir()?;
|
||||||
let next_path = path.canonicalize()?;
|
|
||||||
stack.push_entry(¤t_path)?;
|
stack.push_entry(¤t_path)?;
|
||||||
if config.settings.general.show_stack_on_push {
|
if config.general.show_stack_on_push {
|
||||||
print!("echo '{}' && ", stack.to_formatted_string(&config.settings)?);
|
output.push_info(&stack.to_formatted_string(&config)?);
|
||||||
}
|
}
|
||||||
println!(
|
output.push_command(&format!("cd -- {}", match path.canonicalize()?.to_str() {
|
||||||
"cd -- {}",
|
Some(value) => value,
|
||||||
match next_path.to_str() {
|
None => return Err(Error::other("-- failed to print provided path as string")),
|
||||||
Some(value) => value,
|
}));
|
||||||
None => return Err(Error::other("-- failed to print provided path as string")),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
|
use super::config::*;
|
||||||
|
|
||||||
|
use std::backtrace::Backtrace;
|
||||||
|
|
||||||
|
/// takes strings to output because the application is
|
||||||
|
/// called by a script which executes the output with `eval`
|
||||||
|
pub struct Output {
|
||||||
|
/// takes direct command output with custom formatting
|
||||||
|
/// **NOTE** - strings in `command` are not wrapped
|
||||||
|
/// with `echo` or similar and thus are interpretted as
|
||||||
|
/// commands
|
||||||
|
command: Vec<String>,
|
||||||
|
/// takes formatted output to be printed with `echo`
|
||||||
|
/// strings in `info` are prepended with `echo` but
|
||||||
|
/// do not get any formatting applied
|
||||||
|
info: Vec<String>,
|
||||||
|
/// takes warnings about command input or the state of
|
||||||
|
/// application
|
||||||
|
warning: Vec<String>,
|
||||||
|
/// takes error messages about command input or the
|
||||||
|
/// state of application
|
||||||
|
error: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Output {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
command: Vec::<String>::new(),
|
||||||
|
info: Vec::<String>::new(),
|
||||||
|
warning: Vec::<String>::new(),
|
||||||
|
error: Vec::<String>::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// push a command to the output pipeline
|
||||||
|
pub fn push_command(&mut self, command: &String) {
|
||||||
|
self.command.push(command.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// push an information to the output pipeline
|
||||||
|
pub fn push_info(&mut self, info: &String) {
|
||||||
|
self.info.push(info.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// push a warning to the output pipeline
|
||||||
|
pub fn push_warning(&mut self, warning: &String) {
|
||||||
|
self.warning.push(warning.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// push an error to the output pipeline
|
||||||
|
pub fn push_error(&mut self, error: &String) {
|
||||||
|
self.error.push(error.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// format and print styled output
|
||||||
|
/// NOTE - this will execute any commands held by `command`
|
||||||
|
pub fn print_output(&self, config: &Config) {
|
||||||
|
let mut info: String = self.info.iter().map(|entry| format!("echo '{}'", entry)).collect::<Vec<String>>().join(" && ");
|
||||||
|
let mut warning: String = self.warning.iter().map(|entry| format!("echo '{}'", entry)).collect::<Vec<String>>().join(" && ");
|
||||||
|
let mut error: String = self.error.iter().map(|entry| format!("echo '{}'", entry)).collect::<Vec<String>>().join(" && ");
|
||||||
|
let mut command: String = self.command.join(" && ");
|
||||||
|
let mut output: Vec<String> = Vec::<String>::new();
|
||||||
|
|
||||||
|
if !info.is_empty() {
|
||||||
|
output.push(info);
|
||||||
|
}
|
||||||
|
if !warning.is_empty() {
|
||||||
|
warning = format!("echo '{}' && {}", config.styles.warning_style, warning);
|
||||||
|
output.push(warning);
|
||||||
|
}
|
||||||
|
if !error.is_empty() {
|
||||||
|
error = format!("echo '{}' && {}", config.styles.error_style, error);
|
||||||
|
output.push(error);
|
||||||
|
}
|
||||||
|
if !command.is_empty() {
|
||||||
|
output.push(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{}", output.join(" && echo && "));
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-4
@@ -9,7 +9,7 @@ use sysinfo::{Pid, System};
|
|||||||
|
|
||||||
use crate::make_padding_string;
|
use crate::make_padding_string;
|
||||||
|
|
||||||
use super::{apply_format, config::*};
|
use super::{apply_format, config::*, output::Output};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Stack {
|
pub struct Stack {
|
||||||
@@ -31,7 +31,7 @@ impl Stack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// formats and prints stack to string
|
/// formats and prints stack to string
|
||||||
pub fn to_formatted_string(&self, config: &Settings) -> Result<String> {
|
pub fn to_formatted_string(&self, config: &Config) -> Result<String> {
|
||||||
let mut buffer: String = "".to_string();
|
let mut buffer: String = "".to_string();
|
||||||
|
|
||||||
if self.stack.is_empty() {
|
if self.stack.is_empty() {
|
||||||
@@ -58,9 +58,8 @@ impl Stack {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// clear stack by deleting the associated stack file
|
/// clear stack by deleting the associated stack file
|
||||||
pub fn clear_stack(&mut self, config: &Settings) -> Result<()> {
|
pub fn clear_stack(&mut self) -> Result<()> {
|
||||||
fs::remove_file(self.path.clone())?;
|
fs::remove_file(self.path.clone())?;
|
||||||
print!("echo 'stack cleared successfully.'");
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,9 +4,14 @@
|
|||||||
- [x] option to pop several entries at a time and option to pop the entire stack
|
- [x] option to pop several entries at a time and option to pop the entire stack
|
||||||
- [x] drop stack
|
- [x] drop stack
|
||||||
- [x] config file
|
- [x] config file
|
||||||
|
- [x] implement procedural macro for config
|
||||||
|
- [x] to parse config
|
||||||
|
- [ ] to write default config
|
||||||
- [ ] dedup stack option
|
- [ ] dedup stack option
|
||||||
- [x] parse config file
|
- [x] parse config file
|
||||||
- [ ] apply config -- partially more done than before :)
|
- [ ] apply config -- partially more done than before :)
|
||||||
- [ ] colored output > make it configurable through config file --> at least partially done
|
- [ ] `show-bookmarks-on-book`
|
||||||
- [x] setting for separator string when displaying stack/bookmarks
|
- [x] setting for separator string when displaying stack/bookmarks
|
||||||
|
- [ ] color option for punctuation (mostly '/')
|
||||||
- [x] bookmarks
|
- [x] bookmarks
|
||||||
|
- [ ] do not resolve links in bookmarks
|
||||||
|
|||||||
Reference in New Issue
Block a user