mirror of
https://github.com/fdehau/tui-rs.git
synced 2024-11-17 03:25:41 +00:00
79 lines
1.6 KiB
Rust
79 lines
1.6 KiB
Rust
extern crate rand;
|
|
|
|
pub mod event;
|
|
|
|
use self::rand::distributions::{Distribution, Uniform};
|
|
use self::rand::rngs::ThreadRng;
|
|
|
|
#[derive(Clone)]
|
|
pub struct RandomSignal {
|
|
distribution: Uniform<u64>,
|
|
rng: ThreadRng,
|
|
}
|
|
|
|
impl RandomSignal {
|
|
pub fn new(lower: u64, upper: u64) -> RandomSignal {
|
|
RandomSignal {
|
|
distribution: Uniform::new(lower, upper),
|
|
rng: rand::thread_rng(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Iterator for RandomSignal {
|
|
type Item = u64;
|
|
fn next(&mut self) -> Option<u64> {
|
|
Some(self.distribution.sample(&mut self.rng))
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct SinSignal {
|
|
x: f64,
|
|
interval: f64,
|
|
period: f64,
|
|
scale: f64,
|
|
}
|
|
|
|
impl SinSignal {
|
|
pub fn new(interval: f64, period: f64, scale: f64) -> SinSignal {
|
|
SinSignal {
|
|
x: 0.0,
|
|
interval,
|
|
period,
|
|
scale,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Iterator for SinSignal {
|
|
type Item = (f64, f64);
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
let point = (self.x, (self.x * 1.0 / self.period).sin() * self.scale);
|
|
self.x += self.interval;
|
|
Some(point)
|
|
}
|
|
}
|
|
|
|
pub struct TabsState<'a> {
|
|
pub titles: Vec<&'a str>,
|
|
pub index: usize,
|
|
}
|
|
|
|
impl<'a> TabsState<'a> {
|
|
pub fn new(titles: Vec<&'a str>) -> TabsState {
|
|
TabsState { titles, index: 0 }
|
|
}
|
|
pub fn next(&mut self) {
|
|
self.index = (self.index + 1) % self.titles.len();
|
|
}
|
|
|
|
pub fn previous(&mut self) {
|
|
if self.index > 0 {
|
|
self.index -= 1;
|
|
} else {
|
|
self.index = self.titles.len() - 1;
|
|
}
|
|
}
|
|
}
|