ratatui/examples/util/mod.rs
2017-10-31 00:08:29 +01:00

74 lines
1.5 KiB
Rust

#![allow(dead_code)]
extern crate rand;
use self::rand::distributions::{IndependentSample, Range};
#[derive(Clone)]
pub struct RandomSignal {
range: Range<u64>,
rng: rand::ThreadRng,
}
impl RandomSignal {
pub fn new(lower: u64, upper: u64) -> RandomSignal {
RandomSignal {
range: Range::new(lower, upper),
rng: rand::thread_rng(),
}
}
}
impl Iterator for RandomSignal {
type Item = u64;
fn next(&mut self) -> Option<u64> {
Some(self.range.ind_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: interval,
period: period,
scale: 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 MyTabs<'a> {
pub titles: Vec<&'a str>,
pub selection: usize,
}
impl<'a> MyTabs<'a> {
pub fn next(&mut self) {
self.selection = (self.selection + 1) % self.titles.len();
}
pub fn previous(&mut self) {
if self.selection > 0 {
self.selection -= 1;
} else {
self.selection = self.titles.len() - 1;
}
}
}