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};
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Format {
// reset both style and color
reset_all: String,
/// escape character to start a sequence
pub escape: String,
// text style
style: Styles,
/// prefix = escape + '['
pub prefix: String,
// text color
color: Colors,
/// reset both style and color
pub reset_all: String,
/// text style
pub style: Styles,
/// text color
pub color: Colors,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Styles {
// set style
set: StyleCodes,
/// set style
pub set: StyleCodes,
// reset style
reset: StyleCodes,
/// reset style
pub reset: StyleCodes,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct StyleCodes {
bold: String,
dim: String,
italic: String,
underline: String,
blink: String,
reverse: String,
invisible: String,
strikethrough: String,
pub bold: String,
pub dim: String,
pub italic: String,
pub underline: String,
pub blink: String,
pub reverse: String,
pub invisible: String,
pub strikethrough: String,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct Colors {
foreground: ColorCodes,
background: ColorCodes,
pub foreground: ColorCodes,
pub background: ColorCodes,
}
#[allow(dead_code)]
#[derive(Debug, Clone)]
pub struct ColorCodes {
black: String,
red: String,
green: String,
yellow: String,
blue: String,
magenta: String,
cyan: String,
white: String,
default: String,
pub black: String,
pub red: String,
pub green: String,
pub yellow: String,
pub blue: String,
pub magenta: String,
pub cyan: String,
pub white: String,
pub default: String,
}
#[allow(dead_code)]
impl Format {
const ESC: &str = "\x1B";
const PREFIX: &str = "\x1B[";
pub const ESC: &str = "\x1B";
pub const PREFIX: &str = "\x1B[";
pub const TERMINATION: &str = "m";
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 {
@@ -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")
}
}