Skip to main content

MatrixRainState

Struct MatrixRainState 

Source
pub struct MatrixRainState { /* private fields */ }
Expand description

Per-frame animation state for a MatrixRain widget.

Holds one stream per terminal column, a seeded RNG, timing bookkeeping, and a cached terminal color count. The same state instance must be passed across consecutive renders so the animation continues from frame to frame.

MatrixRainState is Send but not Sync — it’s designed for single-threaded use (render takes &mut self).

§Example

use matrix_rain::{MatrixConfig, MatrixRain, MatrixRainState};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::widgets::StatefulWidget;

let cfg = MatrixConfig::default();
let mut state = MatrixRainState::with_seed(42);
let mut buf = Buffer::empty(Rect::new(0, 0, 40, 12));
MatrixRain::new(&cfg).render(Rect::new(0, 0, 40, 12), &mut buf, &mut state);
assert_eq!(state.streams_len(), 40);

Implementations§

Source§

impl MatrixRainState

Source

pub fn new() -> Self

Create a new state seeded from system entropy.

Use with_seed instead when you need reproducible output (snapshot tests, screenshots, --seed in the binary).

Requires the std feature (entropy comes from getrandom). no_std callers must use with_seed.

Source

pub fn with_seed(seed: u64) -> Self

Create a new state with a deterministic RNG seed.

Two states constructed with the same seed and driven through the same area/config sequence produce identical streams.

§Example
use matrix_rain::MatrixRainState;
let a = MatrixRainState::with_seed(42);
let b = MatrixRainState::with_seed(42);
assert_eq!(a.streams_len(), b.streams_len());
Source

pub fn tick(&mut self)

Advance the animation by exactly one frame, regardless of wall-clock time. Bypasses pause.

Uses the area and configuration cached by the most recent MatrixRain::render call; before the first render, this is a silent no-op. last_tick is not touched, so mixing manual ticks with wall-clock-driven renders will drift over time — pick one driving mode per session.

Source

pub fn reset(&mut self)

Clear streams, timing, cached area/config, frame counter, and the paused flag. RNG state and cached color count are preserved.

After reset, the next render is treated as a first render (applies exactly one tick).

Source

pub fn pause(&mut self)

Pause wall-clock-driven advance. Subsequent render() / advance() calls still handle resize and paint the current state, but do not move streams forward. Manual tick() is unaffected. Idempotent.

Source

pub fn resume(&mut self)

Resume wall-clock-driven advance after a pause(). Discards any previously-recorded last_tick/accum so the next render is treated as a first render (exactly one tick applied) — preventing the catch-up-cap stutter that an accumulated pause-time would otherwise trigger. Idempotent.

Source

pub fn is_paused(&self) -> bool

Returns whether wall-clock advance is currently suppressed.

Source

pub fn streams_len(&self) -> usize

Returns the number of column streams currently allocated.

After a render into a non-empty area, this equals area.width as usize. After a render into an empty area (width == 0 or height == 0), returns 0.

Source

pub fn set_color_count(&mut self, count: u16)

Override the cached terminal color count, suppressing auto-detection on the next render. Useful for forcing a specific gradient tier (16-color collapse for accessibility, 256-color quantization, or u16::MAX for the smooth-interpolation path) and for deterministic testing where TERM/COLORTERM should not influence rendering.

Trait Implementations§

Source§

impl Default for MatrixRainState

Available on crate feature std only.
Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V