Compare commits

...
1 Commits
8 changed files with 48 additions and 34 deletions

No files matched your search

+14 -9
View File
@@ -47,23 +47,25 @@ pub trait StyleTrait {}
impl StyleTrait for AnsiStyle {} impl StyleTrait for AnsiStyle {}
#[derive(Clone)] #[derive(Clone)]
pub struct Style { pub struct StyleConstruct {
pub(super) inner: Vec<u8>, pub(super) inner: Vec<u8>,
} }
impl From<Style> for Vec<u8> {
fn from(value: Style) -> Self { // why do i need this impl
value.inner impl From<StyleConstruct> for Vec<u8> {
fn from(value: StyleConstruct) -> Self {
value.into_inner()
} }
} }
impl Default for Style { impl Default for StyleConstruct {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()
} }
} }
// honestly it could work // honestly it could work
impl Style { impl StyleConstruct {
pub fn new() -> Self { pub fn new() -> Self {
Self { inner: Vec::new() } Self { inner: Vec::new() }
} }
@@ -81,15 +83,18 @@ impl Style {
self.inner.push(style.into()); self.inner.push(style.into());
self self
} }
pub fn into_inner(self) -> Vec<u8> {
self.inner
}
} }
impl Display for Style { impl Display for StyleConstruct {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let a = AnsiCodes::from(vec![self.clone()]); let a = AnsiCodes::from(vec![self.clone()]);
write!(f, "{a}") write!(f, "{a}")
} }
} }
impl From<Vec<Style>> for AnsiCodes { impl From<Vec<StyleConstruct>> for AnsiCodes {
fn from(value: Vec<Style>) -> Self { fn from(value: Vec<StyleConstruct>) -> Self {
Self { Self {
prefix: AnsiCodes::ESCAPE.into(), prefix: AnsiCodes::ESCAPE.into(),
suffix: Self::STYLE_SUFFIX.into(), suffix: Self::STYLE_SUFFIX.into(),
+4 -1
View File
@@ -1,4 +1,7 @@
use std::{error::Error, fmt::{Debug, Display}}; use std::{
error::Error,
fmt::{Debug, Display},
};
// use crate::ansi_colors_2::Color; // use crate::ansi_colors_2::Color;
+1 -3
View File
@@ -90,9 +90,7 @@ impl ldaValue<Vec<u8>> for AnsiColor {
ColorType::CustomColor(v) => v.value(), ColorType::CustomColor(v) => v.value(),
}; };
let code = vect let code = vect.first_mut().expect("no code found, bad init");
.first_mut()
.expect("no code found, bad init");
*code += if self.is_bg { 40 } else { 30 }; *code += if self.is_bg { 40 } else { 30 };
vect vect
-1
View File
@@ -49,7 +49,6 @@ impl From<Style> for u8 {
} }
} }
impl std::ops::Not for Style { impl std::ops::Not for Style {
type Output = Self; type Output = Self;
fn not(self) -> Self::Output { fn not(self) -> Self::Output {
+6
View File
@@ -0,0 +1,6 @@
pub use crate::{
ansi_backbone::StyleConstruct,
ansi_colors_2::{ansi_color_interface::ColorContext, Color, CustomColor},
ansi_styles::Style,
AnsiStyles,
};
+8 -8
View File
@@ -7,16 +7,14 @@ pub mod ansi_colors;
pub mod ansi_colors_2; pub mod ansi_colors_2;
pub mod ansi_styles; // here so ide dont dim code pub mod ansi_styles; // here so ide dont dim code
pub mod ansi_backbone; pub mod ansi_backbone;
use ansi_backbone::{AnsiCodes, Style}; // ColorTrait, use ansi_backbone::{AnsiCodes, StyleConstruct}; // ColorTrait,
mod test;
/// text as text and styles will be turned to [`AnsiCodes`] /// text as text and styles will be turned to [`AnsiCodes`]
#[derive(Clone)] #[derive(Clone)]
pub struct AnsiStyles { pub struct AnsiStyles {
pub text: String, pub text: String,
styles: Vec<Style>, // no styles: Vec<StyleConstruct>, // no
} }
impl Default for AnsiStyles { impl Default for AnsiStyles {
fn default() -> Self { fn default() -> Self {
@@ -36,17 +34,17 @@ impl AnsiStyles {
styles: Vec::new(), styles: Vec::new(),
} }
} }
pub fn add_style(&mut self, style: &Style) -> &mut Self { pub fn add_style(&mut self, style: &StyleConstruct) -> &mut Self {
self.styles.push(style.clone()); self.styles.push(style.clone());
self self
} }
// chaining support // chaining support
pub fn style(mut self, style: Style) -> Self { pub fn style(mut self, style: StyleConstruct) -> Self {
self.add_style(&style); self.add_style(&style);
self self
} }
pub fn clone_with_style(&mut self,style: Style) -> Self { pub fn clone_with_style(&mut self, style: StyleConstruct) -> Self {
let c= self.clone(); let c = self.clone();
c.style(style) c.style(style)
} }
pub fn apply(&mut self, str: &str) { pub fn apply(&mut self, str: &str) {
@@ -65,3 +63,5 @@ impl From<AnsiStyles> for String {
format!("{value}") // wow format!("{value}") // wow
} }
} }
pub mod import;
+4 -2
View File
@@ -1,8 +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}; use lda_ansi::ansi_colors_2::{
ansi_color_interface::ColorContext, Color as Color2, CustomColor as CustomColor2,
};
#[allow(unused_variables)] #[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 white_bg2 = Color2::Magenta.bg();
+11 -10
View File
@@ -3,8 +3,6 @@
//! //!
//! copilot chat teach me how to do stuff (all code made by copilot chat) //! copilot chat teach me how to do stuff (all code made by copilot chat)
#![cfg(test)]
use ansi_colors::Color; use ansi_colors::Color;
use ansi_colors_2::{ use ansi_colors_2::{
ansi_color_interface::ColorContext as Color2Context, ansi_color_interface::ColorContext as Color2Context,
@@ -13,12 +11,13 @@ use ansi_colors_2::{
// CustomColor as CustomColor2, // CustomColor as CustomColor2,
}; };
use ansi_styles::Style as AnsiStyle; use ansi_styles::Style as AnsiStyle;
use lda_ansi::{ansi_colors, ansi_colors_2, ansi_styles};
use std::collections::HashSet; use std::collections::HashSet;
// use ansi_colors_2::ansi_color_interface::ColorContext as _; // use ansi_colors_2::ansi_color_interface::ColorContext as _;
use ansi_colors::color_error::ColorError; use ansi_colors::color_error::ColorError;
use super::*; // use super::*;
#[derive(Debug)] #[derive(Debug)]
struct CodeError; struct CodeError;
@@ -80,7 +79,6 @@ fn process_codes(codes: &[u8]) -> Result<(HashSet<u8>, HashSet<Vec<u8>>), CodeEr
Ok((set, groups)) Ok((set, groups))
} }
fn process_codes2(codes: &[u8]) -> Result<(HashSet<u8>, HashSet<Vec<u8>>), ColorError> { fn process_codes2(codes: &[u8]) -> Result<(HashSet<u8>, HashSet<Vec<u8>>), ColorError> {
let mut set = HashSet::new(); let mut set = HashSet::new();
let mut groups = HashSet::new(); let mut groups = HashSet::new();
@@ -95,7 +93,8 @@ fn process_codes2(codes: &[u8]) -> Result<(HashSet<u8>, HashSet<Vec<u8>>), Color
this.push(*colop); this.push(*colop);
match colop { match colop {
2 => { 2 => {
let group: Vec<_> = enumerate_codes.by_ref().take(3).map(|(_, &v)| v).collect(); // by ref is big let group: Vec<_> =
enumerate_codes.by_ref().take(3).map(|(_, &v)| v).collect(); // by ref is big
if group.len() == 3 { if group.len() == 3 {
this.extend(group); this.extend(group);
} else { } else {
@@ -136,8 +135,8 @@ pub fn compare_ansi_codes(actual: &[u8], expected: &[u8]) -> Result<bool, ColorE
fn test_normal_use() { fn test_normal_use() {
// what kinda shit // what kinda shit
let mut builder = AnsiStyles::init("what is going on here"); let mut builder = lda_ansi::AnsiStyles::init("what is going on here");
let mut error_style = Style::new(); let mut error_style = lda_ansi::ansi_backbone::StyleConstruct::new();
error_style error_style
.color(Color2::Red.fg().bright()) // 91 .color(Color2::Red.fg().bright()) // 91
.color(Color::custom_256(232).bg()) // 48;5;232 .color(Color::custom_256(232).bg()) // 48;5;232
@@ -145,11 +144,13 @@ fn test_normal_use() {
.style(AnsiStyle::BlinkFast); // 6 .style(AnsiStyle::BlinkFast); // 6
builder.add_style(&error_style); builder.add_style(&error_style);
//mut //mut
let reset_style = Style::new().style(AnsiStyle::Reset).to_owned(); let reset_style = lda_ansi::ansi_backbone::StyleConstruct::new()
.style(AnsiStyle::Reset)
.to_owned();
println!("{builder}{reset_style}"); // good so far 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 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]; let reset_estimate: Vec<u8> = vec![0];
assert!(compare_ansi_codes(&error_style.clone().inner, &error_estimate).unwrap()); assert!(compare_ansi_codes(&error_style.clone().into_inner(), &error_estimate).unwrap());
assert!(compare_ansi_codes(&reset_style.clone().inner, &reset_estimate).unwrap()); assert!(compare_ansi_codes(&reset_style.clone().into_inner(), &reset_estimate).unwrap());
} }