Skip to content

Commit

Permalink
Refactor EditorComponent
Browse files Browse the repository at this point in the history
Make lolcat instance available to each editor buffer. This ensures
that colorization can happen using the same "color wheel" to get
gradients that are related to each other (each step to the next
color is strongly related to the previous).

Add serde support, and related to it, f64 comparison support for
Lolcat.

Related to #23
  • Loading branch information
nazmulidris committed Aug 31, 2022
1 parent 6dea59b commit 8d515d2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion core/src/tui_core/lolcat/cat.rs
Expand Up @@ -21,6 +21,7 @@ use std::{fmt::Display,
time::Duration};

use rand::{thread_rng, Rng};
use serde::*;

use crate::*;

Expand Down Expand Up @@ -85,7 +86,7 @@ fn _print(output_vec: &mut OutputCollectorType, args: std::fmt::Arguments) {
output_vec.push(content);
}

#[derive(Debug, Clone, Copy, Default)]
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, PartialEq)]
pub struct Lolcat {
color_wheel_control: ColorWheelControl,
}
Expand Down
21 changes: 19 additions & 2 deletions core/src/tui_core/lolcat/control.rs
Expand Up @@ -17,9 +17,10 @@

use atty::Stream;
use rand::random;
use serde::*;

/// A struct to contain info we need to print with every character.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ColorWheelControl {
pub seed: f64,
pub spread: f64,
Expand All @@ -30,7 +31,23 @@ pub struct ColorWheelControl {
pub color_change_speed: ColorChangeSpeed,
}

#[derive(Debug, Clone, Copy)]
impl PartialEq for ColorWheelControl {
/// More info:
/// 1. <https://stackoverflow.com/questions/67951688/comparing-structs-with-floating-point-numbers-in-rust>
/// 2. <https://doc.rust-lang.org/std/primitive.f64.html#associatedconstant.EPSILON>
/// 3. <https://rust-lang.github.io/rust-clippy/master/index.html#float_equality_without_abs>
fn eq(&self, other: &Self) -> bool {
(self.seed - other.seed).abs() < f64::EPSILON // self.seed == other.seed
&& self.spread == other.spread
&& self.frequency == other.frequency
&& self.background_mode == other.background_mode
&& self.dialup_mode == other.dialup_mode
&& self.print_color == other.print_color
&& self.color_change_speed == other.color_change_speed
}
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum ColorChangeSpeed {
Rapid,
Slow,
Expand Down
4 changes: 3 additions & 1 deletion src/tui/ed/editor_buffer.rs
Expand Up @@ -18,12 +18,14 @@
use r3bl_rs_utils_core::*;
use serde::*;

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct EditorBuffer {
/// A list of lines representing the document being edited.
pub buffer: Vec<String>,
/// The current caret position.
pub cursor: Position,
/// The col and row offset for scrolling if active.
pub scroll_offset: Position,
/// Lolcat struct for generating rainbow colors.
pub lolcat: Lolcat,
}

0 comments on commit 8d515d2

Please sign in to comment.