alternate

This commit is contained in:
lda committed 2025-01-11 01:29:33 +07:00
1 parent eaf7361cb8
commit 87e4b6176c
9 files changed
+458 -66

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
# thing with style
## color
color has a lot of colors: we have
1. 1234567 that takes 30 40 (90 100)
2. 38 / 48 with 2 mode (256^3) or 5 mode (256)
which can cover 30..=37 like this: 38;5;(0..=7)
(same goes with 40..=47, 90 = 38;5;8 (8 + 0))
3. 39 49 reset / default
## style
a lot of styles, will cover 0..=9 | 21..=29
## constructor
i do not know how to do this at all
how do i call
(this is only an example, if i thought this out ill chaneg)
```rust
// how do i call
let style = Style::new("text");
style.green(); // ???
style.underline();
let string = style.render();
let style2 = Style::new();
style2.bold().yellow().italic();
let string = style2.apply("string".into()); //????
let style3 = Style::new();
style3.style(Style_::ITALIC).style(Style_::SLOW_BLINK);
style3.color(Color_::RED).color(Color_::WHITE.bg()); // ??? what the hell is this
let red1 = Color::RED.bg().into(); // ?????
let red2 = Color.red().bg(); // also why into
```
internally. how do i deal with storage + formatting? use vec? keep struct til infinity? struct & enum -ception? tf do
+8 -4
View File
@@ -4,15 +4,19 @@ use super::Color;
#[derive(Debug)]
pub enum ColorError {
NoCode,
InvalidCode(u8), // 50 for example which is for other things
InvalidMode, // not 2 or 5, or 5 but 3 element
InvalidCode, // 50 for example which is for other things
InvalidMode, // not 2 or 5, or 5 but 3 element
// MissingMode, // 48; invalid mode
}
impl Display for ColorError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ColorError::NoCode => write!(f, "No color code found"),
ColorError::InvalidCode(code) => write!(f, "Invalid color code: {}", code),
ColorError::InvalidCode => write!(f, "Invalid color code"),
ColorError::InvalidMode => write!(f, "Invalid color mode"),
// ColorError::MissingMode => {
// write!(f, "Specified custom color, but contains no color modes")
// }
}
}
}
}
+1 -1
View File
@@ -10,4 +10,4 @@ impl From<CustomColor> for Vec<u8> {
CustomColor::TrueColor(r, g, b) => vec![2, r, g, b],
}
}
}
}
+99 -54
View File
@@ -3,6 +3,11 @@ pub mod custom_color; // why
use color_error::ColorError;
#[derive(Debug, Clone)]
/// Color. use Color:: color of your choice () immediately usable as fg.
/// ### usage:
/// convert to [`Vec<u8>`] and then append to existing styles idfk thats what u do how else
#[derive(Eq)]
pub struct Color {
inner: Vec<u8>,
}
@@ -14,36 +19,44 @@ pub struct Color {
// }
// }
impl From<Color> for Vec<u8> {
fn from(value: Color) -> Self {
value.codes()
}
}
impl From<&Color> for Vec<u8> {
fn from(value: &Color) -> Self {
value.codes()
}
}
impl From<&mut Color> for Vec<u8> {
fn from(value: &mut Color) -> Self {
value.codes()
}
}
// impl TryFrom<&Color> for Vec<u8> {
// type Error = ColorError;
// fn try_from(value: &Color) -> Result<Self, Self::Error> {
// value.codes()
// value.codes()
// }
// }
// impl TryFrom<&mut Color> for Vec<u8> {
// type Error = ColorError;
// fn try_from(value: &mut Color) -> Result<Self, Self::Error> {
// value.codes()
// value.codes()
// }
// }
macro_rules! color_defintion {
($name:ident, $code:expr) => {
fn $name() -> Color {
($p:vis $name:ident, $code:expr) => {
$p fn $name() -> Color {
Color {inner: vec![$code]}
}
};
($name:ident, $code:expr, $mode:expr, ($($param:ident),+)) => {
fn $name($($param: u8),+) -> Color {
($p:vis $name:ident, $code:expr, $mode:expr, ($($param:ident),+)) => {
$p fn $name($($param: u8),+) -> Color {
Color {inner: vec![$code, $mode, $($param), +]}
}
}
@@ -55,34 +68,28 @@ impl Default for Color {
type ColorResult<T> = Result<T, ColorError>;
impl Color {
color_defintion!(black, 30); // also can be vec![38, 5, 0] for
// some ass fucking reason
color_defintion!(red, 31);
color_defintion!(green, 32);
color_defintion!(yellow, 33);
color_defintion!(blue, 34);
color_defintion!(magenta, 35);
color_defintion!(cyan, 36);
color_defintion!(white, 37);
color_defintion!(custom_256, 38, 5, (value));
color_defintion!(custom_truecolor, 38, 2, (r, g, b));
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);
color_defintion!(pub blue, 34);
color_defintion!(pub magenta, 35);
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));
fn codes(&self) -> Vec<u8> {
// Result<Vec<u8>, ColorError> {
debug_assert!(self.validate_mode().is_ok(), "Invalid color mode");
self.inner.to_vec()
}
fn bg(mut self) -> Self {
// ColorResult<Self> {
pub fn bg(mut self) -> 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)?;
*code = match code {
30..=39 | 90..=99 => *code + 10,
40..=49 | 100..=109 => *code,
30..=39 | 90..=97 => *code + 10,
40..=49 | 100..=107 => *code,
// _ => return Err(ColorError::InvalidCode(*code)),
_ => unreachable!(),
};
@@ -91,50 +98,92 @@ impl Color {
self
}
fn fg(mut self) -> Self {
// -> ColorResult<Self> {
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)?;
*code = match code {
30..=39 | 90..=99 => *code,
40..=49 | 100..=109 => *code + 10,
_ => unreachable!()
// return Err(ColorError::InvalidCode(*code)),
30..=39 | 90..=97 => *code,
40..=49 | 100..=107 => *code + 10,
_ => unreachable!(), // return Err(ColorError::InvalidCode(*code)),
};
self
}
fn bright(mut self) -> Self {
// NOT IN STANDARD LMAOP use 256
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..=49 => *code + (90 - 30),
90..=109 => *code,
30..=37 | 40..=47 => *code + (90 - 30),
90..=97 | 100..=109 => *code,
38 | 39 | 48 | 49 => unimplemented!(),
_ => unreachable!(),
// return Err(ColorError::InvalidCode(*code)),
};
self
}
pub fn not_bright(mut self) -> Self {
let code = self.inner.first_mut().unwrap();
*code = match code {
30..=49 => *code,
90..=97 | 100..=109 => *code - (90 - 30),
_ => unreachable!(),
};
self
}
pub fn codes(&self) -> Vec<u8> {
// Result<Vec<u8>, ColorError> {
debug_assert!(self.validate_mode().is_ok(), "Invalid color mode");
self.inner.to_vec()
}
#[cfg(debug_assertions)]
fn validate_mode(&self) -> ColorResult<()> {
if self.inner.len() < 2 {
return Ok(());
let selfinnerlen = self.inner.len();
let Some(code) = self.inner.first() else {
return Err(ColorError::NoCode);
};
if selfinnerlen < 2 {
{
return match code {
30..=37 | 39 | 40..=47 | 49 | 90..=97 | 100..=107 => Ok(()),
38 | 48 => Err(ColorError::InvalidMode),
_ => Err(ColorError::InvalidCode),
}
}
}
match self.inner[1] {
2 if self.inner.len() != 5 => Err(ColorError::InvalidMode),
5 if self.inner.len() != 3 => Err(ColorError::InvalidMode),
2 | 5 => Ok(()),
_ => Err(ColorError::InvalidMode),
if *code == 38 || *code == 48 {
match self.inner[1] {
2 if selfinnerlen != 5 => Err(ColorError::InvalidMode),
5 if selfinnerlen != 3 => Err(ColorError::InvalidMode),
2 | 5 => Ok(()),
_ => Err(ColorError::InvalidMode),
}
} else {
Err(ColorError::InvalidMode)
}
}
}
impl PartialEq for Color {
/// color2 cant be equal anything exept color2: shell impl details
/// color5: c8;5;a == ca
fn eq(&self, other: &Self) -> bool {
let this: Vec<u8> = self.into();
let other: Vec<u8> = other.into();
if this == other {
return true;
};
true
}
}
#[cfg(test)]
mod test {
@@ -143,13 +192,9 @@ mod test {
fn test_to_vec() {
let red_bg = Color::red().bg();
let blue_text = Color::blue();
let custom_green = Color::custom_truecolor(0x74, 0xb7, 0x2e)
.bg();
let custom_green = Color::custom_truecolor(0x74, 0xb7, 0x2e).bg();
assert_eq!(Vec::<u8>::from(red_bg), vec![41]);
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]);
}
}
+150
View File
@@ -0,0 +1,150 @@
// use std::{cell::Ref, ops::Deref};
use super::*;
impl From<Color> for ColorConstruct {
fn from(value: Color) -> Self {
ColorConstruct {
color: value,
is_bright: false,
}
}
}
impl From<ColorConstruct> for CustomColor {
fn from(value: ColorConstruct) -> Self {
value.to_custom()
}
}
impl From<ColorConstruct> for ColorType {
fn from(value: ColorConstruct) -> Self {
Self::BasicColor(value)
}
}
impl From<Color> for ColorType {
fn from(value: Color) -> Self {
Self::BasicColor(value.into())
}
}
impl From<CustomColor> for ColorType {
fn from(value: CustomColor) -> Self {
Self::CustomColor(value)
}
}
impl ColorConstruct {
fn bright(mut self) -> Self {
self.is_bright = true;
self
}
fn to_custom(&self) -> CustomColor {
let mut val = self.color.value();
if self.is_bright {
val = val + 8
}
CustomColor::Color256(val)
}
}
pub trait ColorContext {
fn fg(self) -> AnsiColor;
fn bg(self) -> AnsiColor;
}
impl ColorContext for ColorConstruct {
fn bg(self) -> AnsiColor {
AnsiColor {
color: self.into(),
is_bg: true,
}
}
fn fg(self) -> AnsiColor {
AnsiColor {
color: self.into(),
is_bg: true,
}
}
}
impl ColorContext for CustomColor {
fn bg(self) -> AnsiColor {
AnsiColor {
color: self.into(),
is_bg: true,
}
}
fn fg(self) -> AnsiColor {
AnsiColor {
color: self.into(),
is_bg: false,
}
}
}
impl ColorContext for Color {
fn bg(self) -> AnsiColor {
AnsiColor {
color: self.into(),
is_bg: true,
}
}
fn fg(self) -> AnsiColor {
AnsiColor {
color: self.into(),
is_bg: false,
}
}
}
impl ColorContext for AnsiColor {
fn bg(self) -> AnsiColor {
Self {
is_bg: true,
..self
}
}
fn fg(self) -> AnsiColor {
Self {
is_bg: false,
..self
}
}
}
impl AnsiColor {
/// only affects basic color
pub fn bright(self) -> Self {
match self.color {
ColorType::BasicColor(c) => {
let d = c.bright();
Self {
color: d.into(),
..self
}
}
_ => self,
}
}
pub fn to_custom(self) -> Self {
match self.color {
ColorType::CustomColor(_) => self,
ColorType::BasicColor(c) => {
let d = c.to_custom();
Self {
color: d.into(),
..self
}
}
}
}
}
#[cfg(test)]
mod test {
use super::super::*;
use super::*;
#[test]
fn test_basic() {
let color_ = Color::Red.bg().bright();
assert_eq!(Vec::from(color_), [101])
}
#[test]
fn test_problems() {
let uuh_ = CustomColor::TrueColor(13, 21, 26);
assert_eq!(uuh_.fg().bright().value(), [38, 2, 13, 21, 26])
}
}
+141
View File
@@ -0,0 +1,141 @@
mod ansi_color_interface;
#[derive(Clone, Copy)]
pub struct ColorConstruct {
color: Color,
is_bright: bool,
}
#[derive(Clone, Copy)]
pub enum Color {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
// bright?
Default,
}
/// isbg = 1
/// is bright = 10 = 2
#[derive(Clone, Copy)]
pub struct AnsiColor {
color: ColorType,
is_bg: bool,
}
#[derive(Clone, Copy)]
pub enum CustomColor {
Color256(u8),
TrueColor(u8, u8, u8),
}
#[derive(Clone, Copy)]
pub enum ColorType {
BasicColor(ColorConstruct),
CustomColor(CustomColor),
}
#[allow(non_camel_case_types)]
trait ldaValue<T> {
fn value(&self) -> T;
}
// impl<T, V > From<T> for V
// where T: ldaValue<V> {
// fn from(value: T) -> Self {
// value.value()
// }
// }
impl ldaValue<u8> for Color {
fn value(&self) -> u8 {
use Color::*;
match self {
Black => 0,
Red => 1,
Green => 2,
Yellow => 3,
Blue => 4,
Magenta => 5,
Cyan => 6,
White => 7,
Default => 9,
}
}
}
impl ldaValue<u8> for ColorConstruct {
fn value(&self) -> u8 {
let val = self.color.value();
val + if val != 9 && self.is_bright { 60 } else { 0 }
}
}
impl ldaValue<Vec<u8>> for CustomColor {
fn value(&self) -> Vec<u8> {
match *self {
// ??
CustomColor::Color256(value) => vec![8, 5, value],
CustomColor::TrueColor(r, g, b) => vec![8, 2, r, g, b],
}
}
}
impl ldaValue<Vec<u8>> for AnsiColor {
fn value(&self) -> Vec<u8> {
let mut vect = match self.color {
ColorType::BasicColor(v) => vec![v.value()],
ColorType::CustomColor(v) => v.value(),
};
let Some(code) = vect.first_mut() else {
panic!("no code found");
};
*code += if self.is_bg { 40 } else { 30 };
vect
}
}
impl From<Color> for u8 {
fn from(color: Color) -> Self {
color.value()
}
}
impl From<ColorConstruct> for u8 {
fn from(color: ColorConstruct) -> Self {
color.value()
}
}
impl From<CustomColor> for Vec<u8> {
fn from(value: CustomColor) -> Self {
value.value()
}
}
impl From<AnsiColor> for Vec<u8> {
fn from(value: AnsiColor) -> Self {
value.value()
}
}
// copilot
// First, define a wrapper type for our values
// struct ValueWrapper<T: ldaValue<V>, V>(T, V);
// impl<T, V> ldaValue<V> for ValueWrapper<T, V>
// where
// T: ldaValue<V>,
// {
// fn value(&self) -> V {
// self.0.value()
// }
// }
// impl<L, F> From<ValueWrapper<L, F>> for F
// where L: ldaValue<F> {
// fn from(value: ValueWrapper<L, F>) -> Self {
// value.value()
// }
// }
+1 -1
View File
@@ -92,4 +92,4 @@ mod tests {
assert!(matches!(-Style::Faint, Style::Bold));
assert!(matches!(-(-Style::Bold), Style::Bold));
}
}
}
+9 -6
View File
@@ -2,8 +2,11 @@
use ansi_colors::Color;
pub mod ansi_colors;
mod ansi_styles; // here so ide dont dim code
use ansi_styles::Style as AnsiStyle;
mod ansi_styles; // here so ide dont dim code
mod ansi_colors_2;
struct AnsiCodes {
prefix: String,
suffix: String,
@@ -12,9 +15,7 @@ struct AnsiCodes {
struct Style {
inner: Vec<u8>,
}
trait StyleTrait {
}
trait StyleTrait {}
struct AnsiStyles {
text: String,
styles: Vec<Style>, // no
@@ -22,9 +23,11 @@ struct AnsiStyles {
impl Style {
fn color(&mut self, color: Color) -> &mut Self {
self.inner.append(&mut color.try_into().expect("oof")); self // does this clear color.inner?
self.inner.append(&mut color.try_into().expect("oof"));
self // does this clear color.inner?
}
fn style(&mut self, style: AnsiStyle) -> &mut Self {
self.inner.push(style.code()); self
self.inner.push(style.code());
self
}
}
+2
View File
@@ -2,4 +2,6 @@ use lda_ansi::ansi_colors::Color;
fn main(){
let white_bg = Color::white().bg();
let bright_green_text = Color::green().bright();
}