clinging to past mistakes
This commit is contained in:
1 parent
87e4b6176c
commit
aa3035e31e
5 files changed
+75
-17
No files matched your search
+67
-12
@@ -35,6 +35,15 @@ impl From<&mut Color> for Vec<u8> {
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<Vec<u8>> for Color {
|
||||
type Error = ColorError;
|
||||
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
|
||||
let s = Color { inner: value };
|
||||
s.validate_mode()?;
|
||||
Ok(s)
|
||||
}
|
||||
}
|
||||
|
||||
// impl TryFrom<&Color> for Vec<u8> {
|
||||
// type Error = ColorError;
|
||||
// fn try_from(value: &Color) -> Result<Self, Self::Error> {
|
||||
@@ -50,12 +59,14 @@ impl From<&mut Color> for Vec<u8> {
|
||||
// }
|
||||
|
||||
macro_rules! color_defintion {
|
||||
($p:vis $name:ident, $code:expr) => {
|
||||
($p:vis $name:ident, $code:expr $(, $doc:literal)?) => {
|
||||
$(#[doc = $doc])? // holy fuck thank you copilot
|
||||
$p fn $name() -> Color {
|
||||
Color {inner: vec![$code]}
|
||||
}
|
||||
};
|
||||
($p:vis $name:ident, $code:expr, $mode:expr, ($($param:ident),+)) => {
|
||||
($p:vis $name:ident, $code:expr, $mode:expr, ($($param:ident),+)$(, $doc:literal)?) => {
|
||||
$(#[doc = $doc])?
|
||||
$p fn $name($($param: u8),+) -> Color {
|
||||
Color {inner: vec![$code, $mode, $($param), +]}
|
||||
}
|
||||
@@ -63,13 +74,13 @@ macro_rules! color_defintion {
|
||||
}
|
||||
|
||||
impl Default for Color {
|
||||
color_defintion!(default, 39);
|
||||
color_defintion!(default, 39, "reset value for color");
|
||||
}
|
||||
type ColorResult<T> = Result<T, ColorError>;
|
||||
|
||||
/// the entirety of this is a mess
|
||||
impl Color {
|
||||
color_defintion!(pub black, 30); // also can be vec![38, 5, 0] for
|
||||
// some ass fucking reason
|
||||
color_defintion!(pub black, 30); // also can be vec![38, 5, 0] for some ass fucking reason
|
||||
color_defintion!(pub red, 31);
|
||||
color_defintion!(pub green, 32);
|
||||
color_defintion!(pub yellow, 33);
|
||||
@@ -78,9 +89,10 @@ impl Color {
|
||||
color_defintion!(pub cyan, 36);
|
||||
color_defintion!(pub white, 37);
|
||||
// custom color: 38;5;7 == 37 so uh impl partialeq :cryge:
|
||||
color_defintion!(pub custom_256, 38, 5, (value));
|
||||
color_defintion!(pub custom_truecolor, 38, 2, (red, green, blue));
|
||||
color_defintion!(pub custom_256, 38, 5, (value), "8-bit color custom according to [wiki](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit)");
|
||||
color_defintion!(pub custom_truecolor, 38, 2, (red, green, blue), "24-bit or True Color color custom according to [wiki](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit)");
|
||||
|
||||
#[doc = "bg"]
|
||||
pub fn bg(mut self) -> Self {
|
||||
// ColorResult<Self> {
|
||||
// this is why inner is private
|
||||
@@ -100,7 +112,6 @@ impl Color {
|
||||
|
||||
pub fn fg(mut self) -> Self {
|
||||
// -> ColorResult<Self> {
|
||||
// this is why inner is private
|
||||
let code = self.inner.first_mut().unwrap();
|
||||
// .ok_or(ColorError::NoCode)?;
|
||||
|
||||
@@ -113,15 +124,15 @@ impl Color {
|
||||
self
|
||||
}
|
||||
// NOT IN STANDARD LMAOP use 256
|
||||
/// if call on complex number then will not chaeng
|
||||
pub fn bright(mut self) -> Self {
|
||||
// this is why inner is private
|
||||
let code = self.inner.first_mut().unwrap();
|
||||
// .ok_or(ColorError::NoCode)?;
|
||||
|
||||
*code = match code {
|
||||
30..=37 | 40..=47 => *code + (90 - 30),
|
||||
90..=97 | 100..=109 => *code,
|
||||
38 | 39 | 48 | 49 => unimplemented!(),
|
||||
38 | 39 | 48 | 49 => return self,
|
||||
_ => unreachable!(),
|
||||
// return Err(ColorError::InvalidCode(*code)),
|
||||
};
|
||||
@@ -137,6 +148,41 @@ impl Color {
|
||||
};
|
||||
self
|
||||
}
|
||||
/// also !is_basic cuz i lazy
|
||||
fn is_custom(&self) -> bool {
|
||||
// debug_assert!(self.validate_mode().is_ok(), "Invalid color mode"); // IT IS IN self.codes() DO NOT
|
||||
let v = self.codes();
|
||||
let Some(a) = v.first() else { unreachable!() };
|
||||
match a {
|
||||
38 | 48 => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
/// if already custom then none
|
||||
/// also can not convert default
|
||||
fn to_custom(&self) -> Option<Self> {
|
||||
if self.is_custom() {
|
||||
return None;
|
||||
}; // ìksjhfwkjfhkgsr
|
||||
let v = self.codes();
|
||||
let code = v.first().and_then(|c| {
|
||||
let context = *c / 10;
|
||||
let code = *c % 10;
|
||||
if code == 9 {
|
||||
return None;
|
||||
}
|
||||
let bright = { context >= 9 };
|
||||
let bg = { context - if bright { 6 } else { 0 } == 4 };
|
||||
|
||||
Some(vec![
|
||||
8 + if bg { 40 } else { 30 },
|
||||
5,
|
||||
code + if bright { 8 } else { 0 },
|
||||
])
|
||||
})?;
|
||||
Some(Color::try_from(code).ok()?)
|
||||
}
|
||||
|
||||
pub fn codes(&self) -> Vec<u8> {
|
||||
// Result<Vec<u8>, ColorError> {
|
||||
debug_assert!(self.validate_mode().is_ok(), "Invalid color mode");
|
||||
@@ -154,7 +200,7 @@ impl Color {
|
||||
30..=37 | 39 | 40..=47 | 49 | 90..=97 | 100..=107 => Ok(()),
|
||||
38 | 48 => Err(ColorError::InvalidMode),
|
||||
_ => Err(ColorError::InvalidCode),
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
if *code == 38 || *code == 48 {
|
||||
@@ -179,7 +225,7 @@ impl PartialEq for Color {
|
||||
if this == other {
|
||||
return true;
|
||||
};
|
||||
|
||||
todo!();
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -197,4 +243,13 @@ mod test {
|
||||
assert_eq!(Vec::<u8>::from(blue_text), vec![34]);
|
||||
assert_eq!(Vec::<u8>::from(custom_green), vec![48, 2, 0x74, 0xb7, 0x2e]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_vec_to() {
|
||||
let yellow_bg_vec = vec![48, 5, 3]; // yellow bg
|
||||
assert_eq!(
|
||||
yellow_bg_vec,
|
||||
Color::yellow().bg().to_custom().unwrap().codes()
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -135,8 +135,8 @@ impl AnsiColor {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::super::*;
|
||||
use super::*;
|
||||
use super::{*, super::*};
|
||||
|
||||
#[test]
|
||||
fn test_basic() {
|
||||
let color_ = Color::Red.bg().bright();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
mod ansi_color_interface;
|
||||
pub mod ansi_color_interface;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub struct ColorConstruct {
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ pub mod ansi_colors;
|
||||
use ansi_styles::Style as AnsiStyle;
|
||||
|
||||
mod ansi_styles; // here so ide dont dim code
|
||||
mod ansi_colors_2;
|
||||
pub mod ansi_colors_2;
|
||||
|
||||
struct AnsiCodes {
|
||||
prefix: String,
|
||||
|
||||
+4
-1
@@ -1,7 +1,10 @@
|
||||
use lda_ansi::ansi_colors::Color;
|
||||
use lda_ansi::ansi_colors_2::{Color as Color2, CustomColor as CustomColor2, ansi_color_interface::ColorContext};
|
||||
|
||||
|
||||
#[allow(unused_variables)]
|
||||
fn main(){
|
||||
let white_bg = Color::white().bg();
|
||||
let bright_green_text = Color::green().bright();
|
||||
let white_bg2 = Color2::Magenta.bg();
|
||||
let bright_green_text2 = CustomColor2::Color256(215).fg();
|
||||
}
|
||||
Reference in new issue
Block a user