tui-rs/examples/util/mod.rs

75 lines
1.5 KiB
Rust
Raw Normal View History

2016-11-07 23:35:46 +00:00
#![allow(dead_code)]
2017-10-30 21:28:37 +00:00
extern crate rand;
2016-11-07 23:35:46 +00:00
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;
}
}
}