usable!! cool!!!
This commit is contained in:
1 parent
aa3035e31e
commit
03e55e7ae7
5 files changed
+314
-28
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 {}
|
||||
+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,
|
||||
}
|
||||
|
||||
+44
-16
@@ -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
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
+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