renamed config-parser and implemented the first set of functions for the

macro
extended and cleaned up format.rs and moved it into `config-parser`
some other bits and bobs
This commit is contained in:
2025-01-11 22:38:12 +01:00
parent 57dbafac3d
commit d20fd0d023
12 changed files with 119 additions and 59 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ use std::io::{Error, Result};
use std::path::PathBuf;
use std::str::FromStr;
use crate::make_padding_string;
use config_parser::format::make_padding_string;
use super::{apply_format, config::*};
+7 -1
View File
@@ -3,12 +3,12 @@
//! handle the config file and bookmarks stored
//! in said config file
use crate::format::*;
use dirs::config_dir;
use std::fs;
use std::io::{Error, Result};
use std::path::PathBuf;
use config_parser::ConfigParser;
use config_parser::format::*;
#[derive(Debug, Clone)]
pub struct Config {
@@ -42,11 +42,17 @@ pub struct FormatSettings {
#[derive(Debug, Clone, Default, ConfigParser)]
pub struct StyleSettings {
#[color_config]
pub stack_number: String,
#[color_config]
pub stack_separator: String,
#[color_config]
pub stack_path: String,
#[color_config]
pub bookmarks_name: String,
#[color_config]
pub bookmarks_seperator: String,
#[color_config]
pub bookmarks_path: String,
}
-272
View File
@@ -1,272 +0,0 @@
#![allow(dead_code)]
use std::io::{Result, Error};
pub const ESC: &str = "\x1B";
pub const PREFIX: &str = "\x1B[";
pub const RESET_ARG: &str = "0";
pub const TERMINATION: &str = "m";
pub const RESET_SEQ: &str = "\x1B[0m";
pub const FG: ColorContext = ColorContext::Foreground;
pub const BG: ColorContext = ColorContext::Background;
pub const STYLES: Styles = Styles {
set: StyleCodes {
bold: "1",
dim: "2",
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",
},
};
pub const COLORS: Colors = Colors {
fg: ColorCodes {
black: "30",
red: "31",
green: "32",
yellow: "33",
blue: "34",
magenta: "35",
cyan: "36",
white: "37",
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)]
#[derive(Debug, Clone)]
pub struct Styles {
pub set: StyleCodes,
pub reset: StyleCodes,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct StyleCodes {
pub bold: &'static str,
pub dim: &'static str,
pub italic: &'static str,
pub underline: &'static str,
pub blink: &'static str,
pub reverse: &'static str,
pub invisible: &'static str,
pub strikethrough: &'static str,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Colors {
pub fg: ColorCodes,
pub bg: ColorCodes,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct ColorCodes {
pub black: &'static str,
pub red: &'static str,
pub green: &'static str,
pub yellow: &'static str,
pub blue: &'static str,
pub magenta: &'static str,
pub cyan: &'static str,
pub white: &'static str,
pub extended: &'static str,
pub default: &'static str,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub enum ColorContext {
Foreground,
Background,
}
/// prepends input with style string and appends the reset sequence at the end
pub fn apply_format(input: &str, style: &str) -> String {
format!("{}{}{}", style, input, RESET_SEQ)
}
/// 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(
style: Option<Vec<&str>>,
foreground: Option<&str>,
background: Option<&str>,
) -> String {
// assemble sequence as vector of string
// this way the semicolons to separate arguments
// can be inserted with 'join()'
let mut sequence = PREFIX.to_owned();
let mut arguments = Vec::<String>::new();
if let Some(item) = style {
for entry in item {
arguments.push(entry.to_owned());
}
}
if let Some(item) = foreground {
arguments.push(item.to_owned());
}
if let Some(item) = background {
arguments.push(item.to_owned());
}
// 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
}
/// 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
}
/// 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
}
/// generates a padding string for numbers in a list
pub fn make_padding_string(len: usize) -> String {
// determine padding needed to align the paths
String::from_utf8(vec![b' '; len]).unwrap()
}
/// convert color setting to ansi escape sequence
pub fn parse_style(string: String) -> Result<String> {
// check for numbered color
if let Ok(sequence) = parse_numbered_color(&string) {
return Ok(sequence);
}
// check for rgb color
if let Ok(sequence) = parse_rgb_color(&string) {
return Ok(sequence);
}
// check for named color
match string.to_ascii_lowercase().as_str() {
"black" => Ok(generate_style_sequence(None, Some(COLORS.fg.black), None)),
"red" => Ok(generate_style_sequence(None, Some(COLORS.fg.red), None)),
"green" => Ok(generate_style_sequence(None, Some(COLORS.fg.green), None)),
"yellow" => Ok(generate_style_sequence(None, Some(COLORS.fg.yellow), None)),
"blue" => Ok(generate_style_sequence(None, Some(COLORS.fg.blue), None)),
"magenta" => Ok(generate_style_sequence(None, Some(COLORS.fg.magenta), None)),
"cyan" => Ok(generate_style_sequence(None, Some(COLORS.fg.cyan), None)),
"white" => Ok(generate_style_sequence(None, Some(COLORS.fg.white), None)),
"default" => Ok(generate_style_sequence(None, Some(COLORS.fg.default), None)),
_ => Err(Error::other(format!(
"-- could not parse color `{}` in config file",
string
))),
}
}
fn parse_numbered_color(string: &String) -> Result<String> {
// check for numbered color
if let Ok(number) = string.parse::<u8>() {
if number >= 16 {
return Ok(generate_256color_sequence(
ColorContext::Foreground,
number,
));}
}
Err(Error::other(format!("no numbered color found in '{}'", string)))
}
fn parse_rgb_color(string: &String) -> Result<String> {
// check for rgb color
if string.as_bytes()[0] == b'#' && string.len() == 7 {
// match u8::from_str_radix(&color, 16) {
let red = match u8::from_str_radix(&string[1..=2], 16) {
Ok(value) => value,
Err(_) => {
return Err(Error::other(format!(
"-- failed to parse rgb color `{}` in config file",
string
)))
}
};
let green = match u8::from_str_radix(&string[3..=4], 16) {
Ok(value) => value,
Err(_) => {
return Err(Error::other(format!(
"-- failed to parse rgb color `{}` in config file",
string
)))
}
};
let blue = match u8::from_str_radix(&string[5..=6], 16) {
Ok(value) => value,
Err(_) => {
return Err(Error::other(format!(
"-- failed to parse rgb color `{}` in config file",
string
)))
}
};
return Ok(generate_rgb_sequence(
ColorContext::Foreground,
red,
green,
blue,
));
}
Err(Error::other(format!("no rgb color found in '{}'", string)))
}
+2 -3
View File
@@ -1,5 +1,4 @@
mod arguments;
mod format;
mod config;
mod bookmarks;
mod stack;
@@ -9,7 +8,7 @@ use arguments::*;
use clap::Parser;
use config::*;
use bookmarks::*;
use format::*;
use config_parser::format::*;
use stack::Stack;
use std::env::{current_dir, var};
use std::io::{Error, Result};
@@ -18,7 +17,7 @@ use std::str::FromStr;
fn main() -> Result<()> {
let style_error =
generate_style_sequence(Some(vec![STYLES.set.bold]), Some(COLORS.fg.red), None);
generate_style_sequence(Some(STYLES.set.bold), Some(COLORS.fg.red), None);
let args = match Arguments::try_parse() {
Ok(a) => a,
Err(e) => {