Skip to content

Commit da3c584

Browse files
committed
UpdateVersion To v0.4.0
修改: Cargo.toml 修改: UpdateLog.md 修改: src/lib.rs
1 parent a0b9704 commit da3c584

File tree

3 files changed

+133
-8
lines changed

3 files changed

+133
-8
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "term_lattice"
3-
version = "0.3.0"
3+
version = "0.4.0"
44
edition = "2021"
55

66
license = "MIT"
@@ -13,7 +13,6 @@ keywords = ["terminal", "tui", "draw", "plot", "ansi_term"]
1313
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1414

1515
[dependencies]
16-
enum_variant_eq = "0.1.0"
1716

1817
[profile.dev]
1918
opt-level = 1

UpdateLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,8 @@ Modified some display sections, added some practical methods, and adjusted the v
1010

1111
# v0.3.0
1212
Revised the visibility of some fields again and added some methods
13+
14+
# v0.4.0
15+
Added some useful methods, such as `get_colors_arrow` and `Into<Vec<Color>>`, to compensate for visibility changes in `self.colors`.
16+
Can be easily created from `Iterator` (such as the colors field of a buffer of the same size))
17+
Manually expand `EnumVariantEq` to reduce dependencies

src/lib.rs

Lines changed: 127 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ use std::{
88
RefMut, Ref,
99
},
1010
};
11-
use enum_variant_eq::{
12-
EnumVariantEq,
13-
enum_variant_eq_derive::*,
14-
};
1511

1612

1713
pub mod consts {
@@ -51,6 +47,9 @@ pub mod traits {
5147
}
5248
}
5349
}
50+
pub trait EnumVariantEq {
51+
fn enum_variant_eq(&self, other: &Self) -> bool;
52+
}
5453
}
5554
use traits::*;
5655

@@ -93,6 +92,13 @@ macro_rules! no_init_var {
9392
}
9493

9594
/// Configurator used to configure its behavior
95+
/// # Examples
96+
/// ```
97+
/// # use term_lattice::Config;
98+
/// let mut cfg = Config::new();
99+
/// cfg.chromatic_aberration = 1;
100+
/// // pass
101+
/// ```
96102
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
97103
pub struct Config {
98104
/// Standard Half Height Characters
@@ -334,7 +340,7 @@ mod ansi_colors_tests {
334340
}
335341

336342

337-
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumVariantEq)]
343+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
338344
pub enum Color {
339345
Rgb(Rgb),
340346
C256(u8),
@@ -436,6 +442,36 @@ impl Default for Color {
436442
Self::None
437443
}
438444
}
445+
impl EnumVariantEq for Color {
446+
/// # Examples
447+
/// ```
448+
/// # use term_lattice::{Color,traits::EnumVariantEq};
449+
/// assert!(Color::None.enum_variant_eq(&Color::None));
450+
/// assert!(Color::C256(39).enum_variant_eq(&Color::C256(78)));
451+
/// assert!(Color::Rgb([89; 3]).enum_variant_eq(&Color::Rgb([23; 3])));
452+
/// assert!(Color::C256(39).enum_variant_eq(&Color::C256(39)));
453+
/// assert!(! Color::C256(8).enum_variant_eq(&Color::None));
454+
/// assert!(! Color::None.enum_variant_eq(&Color::C256(23)));
455+
/// assert!(! Color::Rgb([89; 3]).enum_variant_eq(&Color::C256(87)));
456+
/// assert!(! Color::C256(8).enum_variant_eq(&Color::Rgb([53; 3])));
457+
/// ```
458+
fn enum_variant_eq(&self, other: &Self) -> bool {
459+
macro_rules! matcher {
460+
( $( $pat:pat ),+ $(,)? ) => {
461+
match self {
462+
$(
463+
$pat => is_pat!(other, $pat),
464+
)+
465+
}
466+
};
467+
}
468+
matcher!(
469+
Self::Rgb(..),
470+
Self::C256(..),
471+
Self::None,
472+
)
473+
}
474+
}
439475

440476

441477
#[cfg(test)]
@@ -729,6 +765,32 @@ impl ScreenBuffer {
729765
debug_assert_eq!(res.capacity(), res_cap);
730766
res
731767
}
768+
/// Get a borrow of the color buffer
769+
/// # Examples
770+
/// ```
771+
/// # use term_lattice::{ScreenBuffer,Color};
772+
/// let a = ScreenBuffer::new([2, 2]);
773+
/// a.set([0, 1], Color::C256(39));
774+
/// let b = a.get_colors_borrow().clone();
775+
/// assert_eq!(
776+
/// b, vec![Color::None, Color::None, Color::C256(39), Color::None]);
777+
/// ```
778+
pub fn get_colors_borrow(&self) -> Ref<Vec<Color>> {
779+
self.colors.borrow()
780+
}
781+
/// Get a borrow of the background_colors buffer
782+
/// # Examples
783+
/// ```
784+
/// # use term_lattice::{ScreenBuffer,Color};
785+
/// let a = ScreenBuffer::new([2, 2]);
786+
/// a.set([0, 1], Color::C256(39));
787+
/// assert_eq!(a.get_bg_colors_borrow()[2], Color::None);
788+
/// a.flush(false);
789+
/// assert_eq!(a.get_bg_colors_borrow()[2], Color::C256(39));
790+
/// ```
791+
pub fn get_bg_colors_borrow(&self) -> Ref<Vec<Color>> {
792+
self.background_colors.borrow()
793+
}
732794
}
733795
impl Default for ScreenBuffer {
734796
/// # Examples
@@ -762,7 +824,66 @@ impl PartialEq for ScreenBuffer {
762824
&& self.colors == other.colors
763825
}
764826
}
765-
827+
impl Into<Vec<Color>> for ScreenBuffer {
828+
/// Into colors.
829+
/// # Examples
830+
/// ```
831+
/// # use term_lattice::{ScreenBuffer,Color};
832+
/// let a = ScreenBuffer::new([2, 2]);
833+
/// a.set([0, 1], Color::C256(39));
834+
/// let b: Vec<Color> = a.into();
835+
/// assert_eq!(
836+
/// b, vec![Color::None, Color::None, Color::C256(39), Color::None]);
837+
/// ```
838+
fn into(self) -> Vec<Color> {
839+
self.colors.into_inner()
840+
}
841+
}
842+
impl<T> From<(Position, Config, T)> for ScreenBuffer
843+
where T: Iterator<Item = Color>
844+
{
845+
/// From (size, cfg, iter)
846+
/// # Examples
847+
/// ```
848+
/// # use term_lattice::{ScreenBuffer,Color,Config};
849+
/// let a = ScreenBuffer::from((
850+
/// [2, 2],
851+
/// Config::new(),
852+
/// vec![Color::None, Color::C256(39)].into_iter(),
853+
/// ));
854+
/// let b: Vec<Color> = a.into();
855+
/// assert_eq!(
856+
/// b, vec![Color::None, Color::C256(39), Color::None, Color::None]);
857+
/// ```
858+
fn from(value: (Position, Config, T)) -> Self {
859+
let res = Self::new_from_cfg(value.0, value.1);
860+
let mut i = 0;
861+
for color in value.2 {
862+
res.set_idx(i, color);
863+
i += 1;
864+
}
865+
res
866+
}
867+
}
868+
impl<T> From<(Position, T)> for ScreenBuffer
869+
where T: Iterator<Item = Color>
870+
{
871+
/// From (size, iter)
872+
/// # Examples
873+
/// ```
874+
/// # use term_lattice::{ScreenBuffer,Color,Config};
875+
/// let a = ScreenBuffer::from((
876+
/// [2, 2],
877+
/// vec![Color::None, Color::C256(39)].into_iter(),
878+
/// ));
879+
/// let b: Vec<Color> = a.into();
880+
/// assert_eq!(
881+
/// b, vec![Color::None, Color::C256(39), Color::None, Color::None]);
882+
/// ```
883+
fn from(value: (Position, T)) -> Self {
884+
Self::from((value.0, Config::default(), value.1))
885+
}
886+
}
766887

767888

768889
#[cfg(test)]

0 commit comments

Comments
 (0)