diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-02-08 20:58:03 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-02-08 20:58:55 -0500 |
commit | 656582a6c867c25667661be9b327b4cc73859d7d (patch) | |
tree | fe03a1822b752a747da099aeb33a610fb3590067 /src/util/mod.rs | |
parent | 7a603efb9b3024438bdce8899fd21bcf353e3832 (diff) |
change to using termion's keyboard handling
- user input is now on a seperate thread
- this allows for other threads to be added as well
- keymap configs have changed to be more user friendly
Diffstat (limited to 'src/util/mod.rs')
-rw-r--r-- | src/util/mod.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/src/util/mod.rs b/src/util/mod.rs new file mode 100644 index 0000000..94ead10 --- /dev/null +++ b/src/util/mod.rs @@ -0,0 +1,78 @@ +pub mod event; +pub mod key_mapping; + +use rand::distributions::{Distribution, Uniform}; +use 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; + } + } +} + |