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:
+1
-1
@@ -9,4 +9,4 @@ dirs = "5.0.1"
|
|||||||
serde = { version = "1.0.216", features = [ "std", "derive" ] }
|
serde = { version = "1.0.216", features = [ "std", "derive" ] }
|
||||||
sysinfo = "0.32.0"
|
sysinfo = "0.32.0"
|
||||||
toml = "0.8.19"
|
toml = "0.8.19"
|
||||||
config_parser = { path = "config_parser" }
|
config-parser = { path = "config-parser" }
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
[workspace]
|
||||||
|
members = [
|
||||||
|
"config-parser-common",
|
||||||
|
"config-parser-macro",
|
||||||
|
]
|
||||||
|
|
||||||
|
[package]
|
||||||
|
name = "config-parser"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
config-parser-macro = { path = "config-parser-macro" }
|
||||||
|
config-parser-common = { path = "config-parser-common" }
|
||||||
|
|
||||||
@@ -1,16 +1,11 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "config_parser"
|
name = "config-parser-common"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
autotests = false
|
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
publish = false
|
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
proc-macro = true
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
proc-macro2 = "1.0.92"
|
proc-macro2 = "1.0.92"
|
||||||
quote = "1.0.38"
|
quote = "1.0.38"
|
||||||
syn = { version = "2.0.94", features = ["full", "extra-traits"] }
|
syn = { version = "2.0.94", features = ["full", "extra-traits"] }
|
||||||
|
|
||||||
|
|
||||||
@@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
use std::io::{Result, Error};
|
use std::io::{Result, Error};
|
||||||
|
|
||||||
pub const ESC: &str = "\x1B";
|
pub const ESC: &str = "\x1b";
|
||||||
pub const PREFIX: &str = "\x1B[";
|
pub const PREFIX: &str = "\x1b[";
|
||||||
pub const RESET_ARG: &str = "0";
|
pub const RESET_ARG: &str = "0";
|
||||||
pub const TERMINATION: &str = "m";
|
pub const TERMINATION: &str = "m";
|
||||||
pub const RESET_SEQ: &str = "\x1B[0m";
|
pub const RESET_SEQ: &str = "\x1b[0m";
|
||||||
pub const FG: ColorContext = ColorContext::Foreground;
|
pub const FG: ColorContext = ColorContext::Foreground;
|
||||||
pub const BG: ColorContext = ColorContext::Background;
|
pub const BG: ColorContext = ColorContext::Background;
|
||||||
|
|
||||||
@@ -60,14 +60,12 @@ pub const COLORS: Colors = Colors {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Styles {
|
pub struct Styles {
|
||||||
pub set: StyleCodes,
|
pub set: StyleCodes,
|
||||||
pub reset: StyleCodes,
|
pub reset: StyleCodes,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct StyleCodes {
|
pub struct StyleCodes {
|
||||||
pub bold: &'static str,
|
pub bold: &'static str,
|
||||||
@@ -80,14 +78,12 @@ pub struct StyleCodes {
|
|||||||
pub strikethrough: &'static str,
|
pub strikethrough: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Colors {
|
pub struct Colors {
|
||||||
pub fg: ColorCodes,
|
pub fg: ColorCodes,
|
||||||
pub bg: ColorCodes,
|
pub bg: ColorCodes,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ColorCodes {
|
pub struct ColorCodes {
|
||||||
pub black: &'static str,
|
pub black: &'static str,
|
||||||
@@ -102,7 +98,6 @@ pub struct ColorCodes {
|
|||||||
pub default: &'static str,
|
pub default: &'static str,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ColorContext {
|
pub enum ColorContext {
|
||||||
Foreground,
|
Foreground,
|
||||||
@@ -118,7 +113,7 @@ pub fn apply_format(input: &str, style: &str) -> String {
|
|||||||
/// `\x1B[<styles>;<foreground-color>;<background-color>m`
|
/// `\x1B[<styles>;<foreground-color>;<background-color>m`
|
||||||
/// all elements are optional, if none is supplied the function returns an error
|
/// all elements are optional, if none is supplied the function returns an error
|
||||||
pub fn generate_style_sequence(
|
pub fn generate_style_sequence(
|
||||||
style: Option<Vec<&str>>,
|
style: Option<&str>,
|
||||||
foreground: Option<&str>,
|
foreground: Option<&str>,
|
||||||
background: Option<&str>,
|
background: Option<&str>,
|
||||||
) -> String {
|
) -> String {
|
||||||
@@ -128,9 +123,7 @@ pub fn generate_style_sequence(
|
|||||||
let mut sequence = PREFIX.to_owned();
|
let mut sequence = PREFIX.to_owned();
|
||||||
let mut arguments = Vec::<String>::new();
|
let mut arguments = Vec::<String>::new();
|
||||||
if let Some(item) = style {
|
if let Some(item) = style {
|
||||||
for entry in item {
|
arguments.push(item.to_owned());
|
||||||
arguments.push(entry.to_owned());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if let Some(item) = foreground {
|
if let Some(item) = foreground {
|
||||||
arguments.push(item.to_owned());
|
arguments.push(item.to_owned());
|
||||||
@@ -140,7 +133,6 @@ pub fn generate_style_sequence(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// panic if no arguments provided since this is a programming mistake
|
// panic if no arguments provided since this is a programming mistake
|
||||||
// which should not
|
|
||||||
if arguments.is_empty() {
|
if arguments.is_empty() {
|
||||||
panic!("no arguments provided to 'generate_style_sequence()'");
|
panic!("no arguments provided to 'generate_style_sequence()'");
|
||||||
}
|
}
|
||||||
@@ -152,6 +144,9 @@ pub fn generate_style_sequence(
|
|||||||
/// generates a 256 color sequence
|
/// generates a 256 color sequence
|
||||||
/// see `generate_rgb_sequence(..)` for details
|
/// see `generate_rgb_sequence(..)` for details
|
||||||
pub fn generate_256color_sequence(context: ColorContext, color: u8) -> String {
|
pub fn generate_256color_sequence(context: ColorContext, color: u8) -> String {
|
||||||
|
if color < 16 {
|
||||||
|
return "".to_owned();
|
||||||
|
}
|
||||||
let mut sequence = PREFIX.to_owned();
|
let mut sequence = PREFIX.to_owned();
|
||||||
// choose context
|
// choose context
|
||||||
match context {
|
match context {
|
||||||
@@ -160,7 +155,7 @@ pub fn generate_256color_sequence(context: ColorContext, color: u8) -> String {
|
|||||||
};
|
};
|
||||||
// make it a rgb sequence
|
// make it a rgb sequence
|
||||||
sequence.push_str(";5;");
|
sequence.push_str(";5;");
|
||||||
sequence.push_str(&format!("{color}m"));
|
sequence.push_str(&format!("{color}{TERMINATION}"));
|
||||||
sequence
|
sequence
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,41 +186,73 @@ pub fn make_padding_string(len: usize) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// convert color setting to ansi escape sequence
|
/// convert color setting to ansi escape sequence
|
||||||
pub fn parse_style(string: String) -> Result<String> {
|
/// input format is a quoted string (either double or single)
|
||||||
// check for numbered color
|
/// the style can be a combination of **one** color and
|
||||||
if let Ok(sequence) = parse_numbered_color(&string) {
|
/// one or more style options (bold, italic, underlined, strikethrough)
|
||||||
return Ok(sequence);
|
pub fn parse_style(arg: String) -> Result<String> {
|
||||||
|
let mut colors: Vec<String> = Vec::<String>::new();
|
||||||
|
let mut styles: Vec<String> = Vec::<String>::new();
|
||||||
|
|
||||||
|
// separate style options
|
||||||
|
let mut tokens: Vec<String> = arg.split([' ', ',', '\"', '\'']).map(|entry| entry.trim().to_lowercase()).collect();
|
||||||
|
tokens.retain(|entry| !entry.is_empty());
|
||||||
|
|
||||||
|
// parse options
|
||||||
|
for option in tokens {
|
||||||
|
// parse numbered colors
|
||||||
|
if let Ok(sequence) = parse_numbered_color(&option) {
|
||||||
|
colors.push(sequence);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse rgb colors
|
||||||
|
if let Ok(sequence) = parse_rgb_color(&option) {
|
||||||
|
colors.push(sequence);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse styles and named colors
|
||||||
|
match option.as_str() {
|
||||||
|
// styles
|
||||||
|
"bold" => styles.push(generate_style_sequence(Some(STYLES.set.bold), None, None)),
|
||||||
|
"dim" => styles.push(generate_style_sequence(Some(STYLES.set.dim), None, None)),
|
||||||
|
"italic" => styles.push(generate_style_sequence(Some(STYLES.set.italic), None, None)),
|
||||||
|
"underlined" => styles.push(generate_style_sequence(Some(STYLES.set.underline), None, None)),
|
||||||
|
"blink" => styles.push(generate_style_sequence(Some(STYLES.set.blink), None, None)),
|
||||||
|
"reverse" => styles.push(generate_style_sequence(Some(STYLES.set.reverse), None, None)),
|
||||||
|
"invisible" => styles.push(generate_style_sequence(Some(STYLES.set.invisible), None, None)),
|
||||||
|
"strikethrough" => styles.push(generate_style_sequence(Some(STYLES.set.strikethrough), None, None)),
|
||||||
|
// named colors
|
||||||
|
"black" => colors.push(generate_style_sequence(None, Some(COLORS.fg.black), None)),
|
||||||
|
"red" => colors.push(generate_style_sequence(None, Some(COLORS.fg.red), None)),
|
||||||
|
"green" => colors.push(generate_style_sequence(None, Some(COLORS.fg.green), None)),
|
||||||
|
"yellow" => colors.push(generate_style_sequence(None, Some(COLORS.fg.yellow), None)),
|
||||||
|
"blue" => colors.push(generate_style_sequence(None, Some(COLORS.fg.blue), None)),
|
||||||
|
"magenta" => colors.push(generate_style_sequence(None, Some(COLORS.fg.magenta), None)),
|
||||||
|
"cyan" => colors.push(generate_style_sequence(None, Some(COLORS.fg.cyan), None)),
|
||||||
|
"white" => colors.push(generate_style_sequence(None, Some(COLORS.fg.white), None)),
|
||||||
|
"default" => colors.push(generate_style_sequence(None, Some(COLORS.fg.default), None)),
|
||||||
|
_ => return Err(Error::other(format!("-- could not parse style token `{}` in config file", option))),
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
if colors.len() > 1 {
|
||||||
|
return Err(Error::other(format!("-- too many colors found in setting <{}>", arg)));
|
||||||
}
|
}
|
||||||
// check for rgb color
|
if !colors.is_empty() {
|
||||||
if let Ok(sequence) = parse_rgb_color(&string) {
|
styles.push(colors.pop().unwrap());
|
||||||
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
|
|
||||||
))),
|
|
||||||
}
|
}
|
||||||
|
Ok(styles.join(""))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_numbered_color(string: &String) -> Result<String> {
|
fn parse_numbered_color(string: &String) -> Result<String> {
|
||||||
// check for numbered color
|
// check for numbered color
|
||||||
if let Ok(number) = string.parse::<u8>() {
|
if let Ok(number) = string.parse::<u8>() {
|
||||||
if number >= 16 {
|
|
||||||
return Ok(generate_256color_sequence(
|
return Ok(generate_256color_sequence(
|
||||||
ColorContext::Foreground,
|
ColorContext::Foreground,
|
||||||
number,
|
number,
|
||||||
));}
|
));
|
||||||
}
|
}
|
||||||
Err(Error::other(format!("no numbered color found in '{}'", string)))
|
Err(Error::other(format!("no numbered color found in '{}'", string)))
|
||||||
}
|
}
|
||||||
@@ -233,7 +260,6 @@ fn parse_numbered_color(string: &String) -> Result<String> {
|
|||||||
fn parse_rgb_color(string: &String) -> Result<String> {
|
fn parse_rgb_color(string: &String) -> Result<String> {
|
||||||
// check for rgb color
|
// check for rgb color
|
||||||
if string.as_bytes()[0] == b'#' && string.len() == 7 {
|
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) {
|
let red = match u8::from_str_radix(&string[1..=2], 16) {
|
||||||
Ok(value) => value,
|
Ok(value) => value,
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
pub mod common;
|
||||||
|
pub mod format;
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
[package]
|
||||||
|
name = "config-parser-macro"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
proc-macro = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
proc-macro2 = "1.0.92"
|
||||||
|
quote = "1.0.38"
|
||||||
|
syn = { version = "2.0.94", features = ["full", "extra-traits"] }
|
||||||
|
config-parser-common = { path = "../config-parser-common/" }
|
||||||
@@ -1,11 +1,8 @@
|
|||||||
use common::gen_config_load_function;
|
use config_parser_common::common::gen_config_load_function;
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use syn::{parse_macro_input, DeriveInput};
|
use syn::{parse_macro_input, DeriveInput};
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
|
|
||||||
#[macro_use]
|
|
||||||
mod common;
|
|
||||||
|
|
||||||
/// **for structs only**
|
/// **for structs only**
|
||||||
/// - implements `parse_config(&mut self, input: &String) -> Result<()>`
|
/// - implements `parse_config(&mut self, input: &String) -> Result<()>`
|
||||||
/// which parses a string and fills the fills recognised values into the struct
|
/// which parses a string and fills the fills recognised values into the struct
|
||||||
@@ -26,8 +23,10 @@ pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|||||||
Err(_) => panic!("loading config failed"),
|
Err(_) => panic!("loading config failed"),
|
||||||
};
|
};
|
||||||
|
|
||||||
//let string = format!("{:#?}", ast);
|
if name.to_string() == "Settings" {
|
||||||
//_ = std::fs::write("test.txt", string);
|
let string = format!("{:#?}", ast);
|
||||||
|
_ = std::fs::write("test.txt", string);
|
||||||
|
}
|
||||||
|
|
||||||
let expanded_stream: TokenStream = quote! {
|
let expanded_stream: TokenStream = quote! {
|
||||||
impl #name {
|
impl #name {
|
||||||
@@ -97,6 +96,5 @@ pub fn derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|||||||
}
|
}
|
||||||
}.into();
|
}.into();
|
||||||
expanded_stream.into()
|
expanded_stream.into()
|
||||||
//TokenStream::new()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
pub use config_parser_common::format;
|
||||||
|
pub use config_parser_macro::ConfigParser;
|
||||||
+1
-1
@@ -9,7 +9,7 @@ use std::io::{Error, Result};
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use crate::make_padding_string;
|
use config_parser::format::make_padding_string;
|
||||||
|
|
||||||
use super::{apply_format, config::*};
|
use super::{apply_format, config::*};
|
||||||
|
|
||||||
|
|||||||
+7
-1
@@ -3,12 +3,12 @@
|
|||||||
//! handle the config file and bookmarks stored
|
//! handle the config file and bookmarks stored
|
||||||
//! in said config file
|
//! in said config file
|
||||||
|
|
||||||
use crate::format::*;
|
|
||||||
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 std::path::PathBuf;
|
||||||
use config_parser::ConfigParser;
|
use config_parser::ConfigParser;
|
||||||
|
use config_parser::format::*;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
@@ -42,11 +42,17 @@ pub struct FormatSettings {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Default, ConfigParser)]
|
#[derive(Debug, Clone, Default, ConfigParser)]
|
||||||
pub struct StyleSettings {
|
pub struct StyleSettings {
|
||||||
|
#[color_config]
|
||||||
pub stack_number: String,
|
pub stack_number: String,
|
||||||
|
#[color_config]
|
||||||
pub stack_separator: String,
|
pub stack_separator: String,
|
||||||
|
#[color_config]
|
||||||
pub stack_path: String,
|
pub stack_path: String,
|
||||||
|
#[color_config]
|
||||||
pub bookmarks_name: String,
|
pub bookmarks_name: String,
|
||||||
|
#[color_config]
|
||||||
pub bookmarks_seperator: String,
|
pub bookmarks_seperator: String,
|
||||||
|
#[color_config]
|
||||||
pub bookmarks_path: String,
|
pub bookmarks_path: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-3
@@ -1,5 +1,4 @@
|
|||||||
mod arguments;
|
mod arguments;
|
||||||
mod format;
|
|
||||||
mod config;
|
mod config;
|
||||||
mod bookmarks;
|
mod bookmarks;
|
||||||
mod stack;
|
mod stack;
|
||||||
@@ -9,7 +8,7 @@ use arguments::*;
|
|||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use config::*;
|
use config::*;
|
||||||
use bookmarks::*;
|
use bookmarks::*;
|
||||||
use format::*;
|
use config_parser::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};
|
||||||
@@ -18,7 +17,7 @@ use std::str::FromStr;
|
|||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let style_error =
|
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() {
|
let args = match Arguments::try_parse() {
|
||||||
Ok(a) => a,
|
Ok(a) => a,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user