work in progress

This commit is contained in:
2024-12-03 00:16:37 +01:00
parent 5b1d79ca7c
commit 0a773c48c4
2 changed files with 101 additions and 39 deletions
+82 -31
View File
@@ -1,63 +1,80 @@
use std::io::{Error, Result}; use std::io::{Error, Result};
#[allow(dead_code)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Format { pub struct Format {
// reset both style and color /// escape character to start a sequence
reset_all: String, pub escape: String,
// text style /// prefix = escape + '['
style: Styles, pub prefix: String,
// text color /// reset both style and color
color: Colors, pub reset_all: String,
/// text style
pub style: Styles,
/// text color
pub color: Colors,
} }
#[allow(dead_code)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Styles { pub struct Styles {
// set style /// set style
set: StyleCodes, pub set: StyleCodes,
// reset style /// reset style
reset: StyleCodes, pub reset: StyleCodes,
} }
#[allow(dead_code)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct StyleCodes { pub struct StyleCodes {
bold: String, pub bold: String,
dim: String, pub dim: String,
italic: String, pub italic: String,
underline: String, pub underline: String,
blink: String, pub blink: String,
reverse: String, pub reverse: String,
invisible: String, pub invisible: String,
strikethrough: String, pub strikethrough: String,
} }
#[allow(dead_code)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Colors { pub struct Colors {
foreground: ColorCodes, pub foreground: ColorCodes,
background: ColorCodes, pub background: ColorCodes,
} }
#[allow(dead_code)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ColorCodes { pub struct ColorCodes {
black: String, pub black: String,
red: String, pub red: String,
green: String, pub green: String,
yellow: String, pub yellow: String,
blue: String, pub blue: String,
magenta: String, pub magenta: String,
cyan: String, pub cyan: String,
white: String, pub white: String,
default: String, pub default: String,
} }
#[allow(dead_code)]
impl Format { impl Format {
const ESC: &str = "\x1B"; pub const ESC: &str = "\x1B";
const PREFIX: &str = "\x1B["; pub const PREFIX: &str = "\x1B[";
pub const TERMINATION: &str = "m";
pub const RESET_STYLE: &str = "\x1B[0m";
pub fn new() -> Format { pub fn new() -> Format {
Format { Format {
escape: String::from(Self::ESC),
prefix: String::from(Self::PREFIX),
reset_all: String::from("0"), reset_all: String::from("0"),
style: Styles { style: Styles {
set: StyleCodes { set: StyleCodes {
@@ -108,4 +125,38 @@ impl Format {
} }
} }
pub fn generate_style_sequence(
&self,
style: Option<&Vec<String>>,
foreground: Option<&String>,
background: Option<&String>,
) -> Result<String> {
if style.is_none() && foreground.is_none() && background.is_none() {
return Err(Error::other("-- generate_style_sequence called without arguments"));
}
// assemble sequence if called with arguments
let mut sequence = Vec::<String>::new();
if let Some(item) = style {
sequence.push_str(item);
sequence.push(';');
}
if let Some(item) = foreground {
sequence.push_str(item);
sequence.push(';');
}
if let Some(item) = background {
sequence.push_str(item);
sequence.push(';');
}
sequence.pop(); // remove final semicolon
sequence.push_str(Self::TERMINATION); // terminate sequence
sequence
}
pub fn generate_rgb_sequence(&self, ) -> String {
String::from("hi")
}
} }
+19 -8
View File
@@ -1,11 +1,12 @@
mod arguments; mod arguments;
mod config; mod config;
mod stack;
mod format; mod format;
mod stack;
use arguments::*; use arguments::*;
use clap::Parser; use clap::Parser;
use config::Config; use config::Config;
use format::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};
@@ -13,10 +14,14 @@ 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 args = match Arguments::try_parse() { let args = match Arguments::try_parse() {
Ok(a) => a, Ok(a) => a,
Err(e) => { Err(e) => {
print!("echo '{}'", e); print!(
"echo '{}{}m{}{}{}m'",
format.prefix, format.color.foreground.red, e, format.prefix, format.reset_all
);
return Ok(()); return Ok(());
} }
}; };
@@ -35,7 +40,14 @@ fn main() -> Result<()> {
}; };
if res.is_err() { if res.is_err() {
print!("echo '{}'", res.unwrap_err()); print!(
"echo '{}{}m{}{}{}m'",
format.prefix,
format.color.foreground.red,
res.unwrap_err(),
format.prefix,
format.reset_all
);
} }
Ok(()) Ok(())
} }
@@ -95,16 +107,15 @@ fn handle_bookmark(args: &BookmarkArgs, stack: &mut Stack) -> Result<()> {
BookmarkAction::remove(args) => remove_bookmarks(&args, &mut config)?, BookmarkAction::remove(args) => remove_bookmarks(&args, &mut config)?,
}; };
} else if args.name.is_some() { } else if args.name.is_some() {
let path = match config let path = match config.get_bookmarks().get(args.name.as_ref().unwrap()) {
.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 {
return Err(Error::other("-- provide either a `subcommand` or a `bookmark name`")); return Err(Error::other(
"-- provide either a `subcommand` or a `bookmark name`",
));
} }
Ok(()) Ok(())
} }