pub struct Raindrop<'a, T>where
T: ColorAlgorithm,{ /* private fields */ }Expand description
A Raindrop describes a single ‘falling stream’ of randomized characters
Raindrops consist of a ‘leader’ and a ‘follower’. The leader is a continuously (per frame) randomized single character at the bottom of the raindrop. The follower is a string of characters that follow the leader. They have randomized length and content, but unlike leaders, are randomized only once (at instantiation) rather than continuously (per frame)
Implementations§
Source§impl<'a, T> Raindrop<'a, T>where
T: ColorAlgorithm,
impl<'a, T> Raindrop<'a, T>where
T: ColorAlgorithm,
Sourcepub fn gen_char(&mut self) -> char
pub fn gen_char(&mut self) -> char
Returns a (pseudo)randomly generated character from the internal charset
Sourcepub fn new(
charset: &'a Vec<char>,
color_algorithm: T,
advance_chance: f64,
terminal_height: u16,
) -> Self
pub fn new( charset: &'a Vec<char>, color_algorithm: T, advance_chance: f64, terminal_height: u16, ) -> Self
Returns a new Raindrop instance
charset should be a reference to Vector of chars.
color_algorithm should implement
ColorAlgorithm. It defines
how follower characters will be colored.
advance_chance is the chance that, on any given frame, this Raindrop will
advance its animation. This can be any real number within the range [0.0, 1.0).
If the advance_chance is 1.0, this Raindrop will always advance its animation.
terminal_height should be the current height of the terminal, in rows.
§Panics
This function panics if advance_chance is outside the range [0.0, 1.0)
§Examples
use mrs_matrix::raindrop::{Raindrop, color_algorithms};
use crossterm::terminal;
let charset = vec!['a','b', 'c'];
let color_algorithm = color_algorithms::LightnessDescending{
hue: 118.0,
saturation: 0.82
};
let advance_chance = 0.75;
let term_height = terminal::size().unwrap().1;
let new_raindrop_instance = Raindrop::new(&charset, color_algorithm, advance_chance, term_height);
// do something with instanceSourcepub fn reinit_state(&mut self, terminal_height: u16)
pub fn reinit_state(&mut self, terminal_height: u16)
Re-initializes the state of the Raindrop instance
Uses an internally cached random number generator to generate pseudorandom follower chars and sets the row index to a pseudorandom value less than (visually ‘above’) row 0.
terminal_height should be the current height of the terminal, in rows
§Notes
The Raindrop::new function uses this function internally
to set the initial state. Calling this function manually is similar to creating
a new Raindrop instance outright, but avoids the need to create a new Rng.
Sourcepub fn get_char_at_row(&mut self, row_index: u16) -> Option<char>
pub fn get_char_at_row(&mut self, row_index: u16) -> Option<char>
Returns the character that should be printed for a given row
§Notes
This function returns an Option. When requesting a char for a
row that this instance has no char for (for example, because this raindrop
is above the provided row), None will be returned.
If this instance does have a char for the provided row, Some(char) is returned.
Sourcepub fn get_styled_char_at_row(
&mut self,
row_index: u16,
) -> Option<StyledContent<char>>
pub fn get_styled_char_at_row( &mut self, row_index: u16, ) -> Option<StyledContent<char>>
Returns the character that should be printed for a given row with appropriate styling
Internally, uses get_styled_char to retrieve the actual character. Then applies a color
according to this Raindrop’s color_algorithm
The leader of the raindrop will always be styled white (and bolded).
Sourcepub fn move_drop(&mut self)
pub fn move_drop(&mut self)
Moves the Raindrop down one row.
To reset to the top, use reinit_state.
Sourcepub fn is_visible(&self, terminal_height: u16) -> bool
pub fn is_visible(&self, terminal_height: u16) -> bool
Returns true if Raindrop displays any chars on a terminal of height terminal_height; false otherwise
Sourcepub fn advance_animation(&mut self, terminal_height: u16)
pub fn advance_animation(&mut self, terminal_height: u16)
Advance the Raindrop by one ‘frame’
terminal_height should be the current height of the terminal, in rows.
This is similar to move_drop, with two key differences:
-
If the
Raindropis not visible because it has fallen down below the bottom of the terminal, reinit_state is called to re-randomize theRaindropand move it slightly above the top of the terminal. -
If the
Raindrophas had itsadvance_chanceset to some value that is not 1.0, this function will only have a chance of advancing this raindrop’s position. If you want to move theRaindropfor certain, use the move_drop method