clinging to past mistakes

This commit is contained in:
lda committed 2025-01-11 19:02:15 +07:00
1 parent 87e4b6176c
commit aa3035e31e
5 files changed
+75 -17

No files matched your search

+67 -12
View File
@@ -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> { // impl TryFrom<&Color> for Vec<u8> {
// type Error = ColorError; // type Error = ColorError;
// fn try_from(value: &Color) -> Result<Self, Self::Error> { // fn try_from(value: &Color) -> Result<Self, Self::Error> {
@@ -50,12 +59,14 @@ impl From<&mut Color> for Vec<u8> {
// } // }
macro_rules! color_defintion { 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 { $p fn $name() -> Color {
Color {inner: vec![$code]} 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 { $p fn $name($($param: u8),+) -> Color {
Color {inner: vec![$code, $mode, $($param), +]} Color {inner: vec![$code, $mode, $($param), +]}
} }
@@ -63,13 +74,13 @@ macro_rules! color_defintion {
} }
impl Default for Color { impl Default for Color {
color_defintion!(default, 39); color_defintion!(default, 39, "reset value for color");
} }
type ColorResult<T> = Result<T, ColorError>; type ColorResult<T> = Result<T, ColorError>;
/// the entirety of this is a mess
impl Color { impl Color {
color_defintion!(pub black, 30); // also can be vec![38, 5, 0] for color_defintion!(pub black, 30); // also can be vec![38, 5, 0] for some ass fucking reason
// some ass fucking reason
color_defintion!(pub red, 31); color_defintion!(pub red, 31);
color_defintion!(pub green, 32); color_defintion!(pub green, 32);
color_defintion!(pub yellow, 33); color_defintion!(pub yellow, 33);
@@ -78,9 +89,10 @@ impl Color {
color_defintion!(pub cyan, 36); color_defintion!(pub cyan, 36);
color_defintion!(pub white, 37); color_defintion!(pub white, 37);
// custom color: 38;5;7 == 37 so uh impl partialeq :cryge: // custom color: 38;5;7 == 37 so uh impl partialeq :cryge:
color_defintion!(pub custom_256, 38, 5, (value)); 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)); 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 { pub fn bg(mut self) -> Self {
// ColorResult<Self> { // ColorResult<Self> {
// this is why inner is private // this is why inner is private
@@ -100,7 +112,6 @@ impl Color {
pub fn fg(mut self) -> Self { pub fn fg(mut self) -> Self {
// -> ColorResult<Self> { // -> ColorResult<Self> {
// this is why inner is private
let code = self.inner.first_mut().unwrap(); let code = self.inner.first_mut().unwrap();
// .ok_or(ColorError::NoCode)?; // .ok_or(ColorError::NoCode)?;
@@ -113,15 +124,15 @@ impl Color {
self self
} }
// NOT IN STANDARD LMAOP use 256 // NOT IN STANDARD LMAOP use 256
/// if call on complex number then will not chaeng
pub fn bright(mut self) -> Self { pub fn bright(mut self) -> Self {
// this is why inner is private
let code = self.inner.first_mut().unwrap(); let code = self.inner.first_mut().unwrap();
// .ok_or(ColorError::NoCode)?; // .ok_or(ColorError::NoCode)?;
*code = match code { *code = match code {
30..=37 | 40..=47 => *code + (90 - 30), 30..=37 | 40..=47 => *code + (90 - 30),
90..=97 | 100..=109 => *code, 90..=97 | 100..=109 => *code,
38 | 39 | 48 | 49 => unimplemented!(), 38 | 39 | 48 | 49 => return self,
_ => unreachable!(), _ => unreachable!(),
// return Err(ColorError::InvalidCode(*code)), // return Err(ColorError::InvalidCode(*code)),
}; };
@@ -137,6 +148,41 @@ impl Color {
}; };
self 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> { pub fn codes(&self) -> Vec<u8> {
// Result<Vec<u8>, ColorError> { // Result<Vec<u8>, ColorError> {
debug_assert!(self.validate_mode().is_ok(), "Invalid color mode"); 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(()), 30..=37 | 39 | 40..=47 | 49 | 90..=97 | 100..=107 => Ok(()),
38 | 48 => Err(ColorError::InvalidMode), 38 | 48 => Err(ColorError::InvalidMode),
_ => Err(ColorError::InvalidCode), _ => Err(ColorError::InvalidCode),
} };
} }
} }
if *code == 38 || *code == 48 { if *code == 38 || *code == 48 {
@@ -179,7 +225,7 @@ impl PartialEq for Color {
if this == other { if this == other {
return true; return true;
}; };
todo!();
true true
} }
} }
@@ -197,4 +243,13 @@ mod test {
assert_eq!(Vec::<u8>::from(blue_text), vec![34]); assert_eq!(Vec::<u8>::from(blue_text), vec![34]);
assert_eq!(Vec::<u8>::from(custom_green), vec![48, 2, 0x74, 0xb7, 0x2e]); 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()
)
}
} }
+2 -2
View File
@@ -135,8 +135,8 @@ impl AnsiColor {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::super::*; use super::{*, super::*};
use super::*;
#[test] #[test]
fn test_basic() { fn test_basic() {
let color_ = Color::Red.bg().bright(); let color_ = Color::Red.bg().bright();
+1 -1
View File
@@ -1,4 +1,4 @@
mod ansi_color_interface; pub mod ansi_color_interface;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct ColorConstruct { pub struct ColorConstruct {
+1 -1
View File
@@ -5,7 +5,7 @@ pub mod ansi_colors;
use ansi_styles::Style as AnsiStyle; use ansi_styles::Style as AnsiStyle;
mod ansi_styles; // here so ide dont dim code mod ansi_styles; // here so ide dont dim code
mod ansi_colors_2; pub mod ansi_colors_2;
struct AnsiCodes { struct AnsiCodes {
prefix: String, prefix: String,
+4 -1
View File
@@ -1,7 +1,10 @@
use lda_ansi::ansi_colors::Color; 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(){ fn main(){
let white_bg = Color::white().bg(); let white_bg = Color::white().bg();
let bright_green_text = Color::green().bright(); let bright_green_text = Color::green().bright();
let white_bg2 = Color2::Magenta.bg();
let bright_green_text2 = CustomColor2::Color256(215).fg();
} }