Compare commits
2
Commits
87e4b6176c
...
03e55e7ae7
No files matched your search
@@ -0,0 +1,85 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::{ansi_colors, ansi_colors_2};
|
||||
use crate::ansi_styles::Style as AnsiStyle;
|
||||
#[derive(Clone)]
|
||||
pub struct AnsiCodes {
|
||||
pub prefix: String,
|
||||
pub suffix: String,
|
||||
pub codes: Vec<u8>,
|
||||
}
|
||||
impl AnsiCodes {
|
||||
pub const ESCAPE: &str = "\x1b[";
|
||||
pub const STYLE_SUFFIX: &str = "m";
|
||||
pub const DELIMITER: &str = ";";
|
||||
}
|
||||
impl Display for AnsiCodes {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{}{}{}",
|
||||
self.prefix,
|
||||
self.codes
|
||||
.iter()
|
||||
.map(|c| c.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(AnsiCodes::DELIMITER),
|
||||
self.suffix
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ColorTrait {}
|
||||
impl ColorTrait for ansi_colors_2::Color {}
|
||||
impl ColorTrait for ansi_colors_2::CustomColor {}
|
||||
impl ColorTrait for ansi_colors_2::AnsiColor {}
|
||||
impl ColorTrait for ansi_colors::Color {}
|
||||
// impl<T: ColorTrait> From<T> for Vec<u8> {
|
||||
// fn from(value: T) -> Self {
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Style {
|
||||
pub(super) inner: Vec<u8>,
|
||||
}
|
||||
impl From<Style> for Vec<u8> {
|
||||
fn from(value: Style) -> Self {
|
||||
value.inner
|
||||
}
|
||||
}
|
||||
pub trait StyleTrait {}
|
||||
|
||||
// honestly it could work
|
||||
impl Style {
|
||||
pub fn new() -> Self{
|
||||
Self{inner: Vec::new()}
|
||||
}
|
||||
pub fn color<T: ColorTrait>(&mut self, color: T) -> &mut Self
|
||||
where
|
||||
Vec<u8>: From<T>,
|
||||
{
|
||||
self.inner.append(&mut color.into());
|
||||
self // does this clear color.inner?
|
||||
}
|
||||
pub fn style(&mut self, style: AnsiStyle) -> &mut Self {
|
||||
self.inner.push(style.into());
|
||||
self
|
||||
}
|
||||
}
|
||||
impl Display for Style {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let a = AnsiCodes::from(vec![self.clone()]);
|
||||
write!(f, "{a}")
|
||||
}
|
||||
}
|
||||
impl From<Vec<Style>> for AnsiCodes {
|
||||
fn from(value: Vec<Style>) -> Self {
|
||||
Self {
|
||||
prefix: AnsiCodes::ESCAPE.into(),
|
||||
suffix: Self::STYLE_SUFFIX.into(),
|
||||
codes: value.into_iter().flat_map(Vec::<u8>::from).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
use std::fmt::{Debug, Display};
|
||||
use std::{error::Error, fmt::{Debug, Display}};
|
||||
|
||||
use crate::ansi_colors_2::Color;
|
||||
|
||||
use super::Color;
|
||||
#[derive(Debug)]
|
||||
pub enum ColorError {
|
||||
NoCode,
|
||||
@@ -20,3 +21,4 @@ impl Display for ColorError {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Error for ColorError {}
|
||||
+70
-15
@@ -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");
|
||||
@@ -151,10 +197,10 @@ impl Color {
|
||||
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),
|
||||
}
|
||||
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 {
|
||||
|
||||
+36
-10
@@ -1,5 +1,3 @@
|
||||
use std::ops::{Deref, Mul, MulAssign, Neg, Not, Sub, SubAssign};
|
||||
|
||||
pub enum Style {
|
||||
Reset,
|
||||
Bold,
|
||||
@@ -44,25 +42,43 @@ impl Style {
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Not for Style {
|
||||
|
||||
impl From<Style> for u8 {
|
||||
fn from(value: Style) -> Self {
|
||||
value.code()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl std::ops::Not for Style {
|
||||
type Output = Self;
|
||||
fn not(self) -> Self::Output {
|
||||
use Style::*;
|
||||
match self {
|
||||
Bold => NormalWeight,
|
||||
Faint => NormalWeight,
|
||||
Bold | Faint => NormalWeight,
|
||||
|
||||
Italic => Straight,
|
||||
Straight => Italic,
|
||||
|
||||
Underline => NoUnderline,
|
||||
BlinkSlow => Static,
|
||||
BlinkFast => Static,
|
||||
SwapColor => NormalColor,
|
||||
NoUnderline => Underline,
|
||||
|
||||
BlinkSlow | BlinkFast => Static,
|
||||
|
||||
Strike => NoStrike,
|
||||
NoStrike => Strike,
|
||||
|
||||
NormalColor => SwapColor,
|
||||
SwapColor => NormalColor,
|
||||
|
||||
Hide => Reveal,
|
||||
_ => Reset, // am i cooked
|
||||
Reveal => Hide,
|
||||
|
||||
_ => self, // am i cooked // no
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Neg for Style {
|
||||
impl std::ops::Neg for Style {
|
||||
type Output = Self;
|
||||
fn neg(self) -> Self::Output {
|
||||
use Style::*;
|
||||
@@ -72,11 +88,21 @@ impl Neg for Style {
|
||||
|
||||
// Style opposites
|
||||
Italic => Straight,
|
||||
Straight => Italic,
|
||||
|
||||
Underline => NoUnderline,
|
||||
NoUnderline => Underline,
|
||||
|
||||
BlinkSlow | BlinkFast => Static,
|
||||
|
||||
SwapColor => NormalColor,
|
||||
NormalColor => SwapColor,
|
||||
|
||||
Strike => NoStrike,
|
||||
NoStrike => Strike,
|
||||
|
||||
Hide => Reveal,
|
||||
Reveal => Hide,
|
||||
|
||||
_ => self,
|
||||
}
|
||||
|
||||
+45
-17
@@ -1,33 +1,61 @@
|
||||
#![allow(unused)]
|
||||
|
||||
use std::fmt::Display;
|
||||
|
||||
use ansi_colors::Color;
|
||||
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;
|
||||
pub mod ansi_styles; // here so ide dont dim code
|
||||
use ansi_colors_2::{
|
||||
// ansi_color_interface::ColorContext as Color2Context,
|
||||
AnsiColor as AnsiColor2,
|
||||
Color as Color2,
|
||||
CustomColor as CustomColor2,
|
||||
};
|
||||
|
||||
struct AnsiCodes {
|
||||
prefix: String,
|
||||
suffix: String,
|
||||
codes: Vec<u8>,
|
||||
}
|
||||
struct Style {
|
||||
inner: Vec<u8>,
|
||||
}
|
||||
trait StyleTrait {}
|
||||
pub mod ansi_backbone;
|
||||
use ansi_backbone::{AnsiCodes, ColorTrait, Style};
|
||||
|
||||
mod test;
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AnsiStyles {
|
||||
text: String,
|
||||
styles: Vec<Style>, // no
|
||||
}
|
||||
|
||||
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?
|
||||
impl AnsiStyles {
|
||||
pub fn new() -> AnsiStyles {
|
||||
AnsiStyles {
|
||||
text: String::new(),
|
||||
styles: Vec::new(),
|
||||
}
|
||||
}
|
||||
fn style(&mut self, style: AnsiStyle) -> &mut Self {
|
||||
self.inner.push(style.code());
|
||||
pub fn init(str: &str) -> AnsiStyles {
|
||||
AnsiStyles {
|
||||
text: str.into(),
|
||||
styles: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn add_style(&mut self, style: &Style) -> &mut Self {
|
||||
self.styles.push(style.clone());
|
||||
self
|
||||
}
|
||||
// chaining support
|
||||
pub fn style(mut self, style: Style) -> Self {
|
||||
self.add_style(&style);
|
||||
self
|
||||
}
|
||||
pub fn apply(&mut self, str: &str) {
|
||||
self.text = str.into();
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for AnsiStyles {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}{}", AnsiCodes::from(self.styles.to_vec()), self.text)
|
||||
}
|
||||
}
|
||||
|
||||
+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();
|
||||
}
|
||||
+145
@@ -0,0 +1,145 @@
|
||||
//! test for lib.rs
|
||||
//! very good
|
||||
//!
|
||||
//! copilot chat teach me how to do stuff (all code made by copilot chat)
|
||||
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use ansi_colors_2::ansi_color_interface::ColorContext as _;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct CodeError;
|
||||
|
||||
impl std::fmt::Display for CodeError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Invalid ANSI code sequence")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for CodeError {} // ooh
|
||||
|
||||
fn process_codes(codes: &[u8]) -> Result<(HashSet<u8>, HashSet<Vec<u8>>), CodeError> {
|
||||
let mut set = HashSet::new();
|
||||
let mut groups = HashSet::new();
|
||||
let mut i = 0;
|
||||
|
||||
while i < codes.len() {
|
||||
match codes[i] {
|
||||
38 | 48 => {
|
||||
if i + 1 < codes.len() {
|
||||
match codes[i + 1] {
|
||||
2 => {
|
||||
if i + 4 < codes.len() {
|
||||
groups.insert(vec![
|
||||
codes[i],
|
||||
codes[i + 1],
|
||||
codes[i + 2],
|
||||
codes[i + 3],
|
||||
codes[i + 4],
|
||||
]);
|
||||
i += 5;
|
||||
} else {
|
||||
return Err(CodeError);
|
||||
}
|
||||
}
|
||||
5 => {
|
||||
if i + 2 < codes.len() {
|
||||
groups.insert(vec![codes[i], codes[i + 1], codes[i + 2]]);
|
||||
i += 3;
|
||||
} else {
|
||||
return Err(CodeError);
|
||||
}
|
||||
}
|
||||
_ => return Err(CodeError),
|
||||
}
|
||||
} else {
|
||||
return Err(CodeError);
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
set.insert(codes[i]);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok((set, groups))
|
||||
}
|
||||
|
||||
|
||||
fn process_codes2(codes: &[u8]) -> Result<(HashSet<u8>, HashSet<Vec<u8>>), CodeError> {
|
||||
let mut set = HashSet::new();
|
||||
let mut groups = HashSet::new();
|
||||
|
||||
let mut enumerate_codes = codes.iter().enumerate();
|
||||
while let Some((i, c)) = enumerate_codes.next() {
|
||||
match c {
|
||||
38 | 48 => {
|
||||
let mut this = vec![*c];
|
||||
if let Some((ind, colop)) = enumerate_codes.next() {
|
||||
debug_assert!(ind == i + 1);
|
||||
this.push(*colop);
|
||||
match colop {
|
||||
2 => {
|
||||
let group: Vec<_> = enumerate_codes.by_ref().take(3).map(|(_, &v)| v).collect(); // by ref is big
|
||||
if group.len() == 3 {
|
||||
this.extend(group);
|
||||
} else {
|
||||
return Err(CodeError);
|
||||
}
|
||||
}
|
||||
5 => {
|
||||
if let Some((_, &v)) = enumerate_codes.next() {
|
||||
this.push(v);
|
||||
} else {
|
||||
return Err(CodeError);
|
||||
}
|
||||
}
|
||||
_ => return Err(CodeError),
|
||||
}
|
||||
} else {
|
||||
return Err(CodeError);
|
||||
}
|
||||
groups.insert(this);
|
||||
}
|
||||
_ => {
|
||||
set.insert(*c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok((set, groups))
|
||||
}
|
||||
|
||||
|
||||
fn compare_ansi_codes(actual: &[u8], expected: &[u8]) -> Result<bool, CodeError> {
|
||||
let (actual_set, actual_groups) = process_codes2(actual)?;
|
||||
let (expected_set, expected_groups) = process_codes2(expected)?;
|
||||
|
||||
Ok(actual_set == expected_set && actual_groups == expected_groups)
|
||||
}
|
||||
#[test]
|
||||
fn test_normal_use() {
|
||||
// what kinda shit
|
||||
|
||||
let mut builder = AnsiStyles::init("what is going on here");
|
||||
let mut error_style = Style::new();
|
||||
error_style
|
||||
.color(Color2::Red.fg().bright()) // 91
|
||||
.color(Color::custom_256(232).bg()) // 48;5;232
|
||||
.style(AnsiStyle::Bold) // 1
|
||||
.style(AnsiStyle::BlinkFast); // 6
|
||||
builder.add_style(&error_style);
|
||||
let mut reset_style = Style::new().style(AnsiStyle::Reset).to_owned();
|
||||
println!("{builder}{reset_style}"); // good so far
|
||||
let error_estimate: Vec<u8> = vec![6, 1, 91, 48, 5, 232]; // 48, 5, 232 sticks together in a clump
|
||||
let reset_estimate: Vec<u8> = vec![0];
|
||||
|
||||
assert!(compare_ansi_codes(&error_style.clone().inner, &error_estimate).unwrap());
|
||||
assert!(compare_ansi_codes(&reset_style.clone().inner, &reset_estimate).unwrap());
|
||||
}
|
||||
Reference in new issue
Block a user