mirror of
https://github.com/ricoriedel/wipe.git
synced 2024-11-22 16:06:38 +00:00
Make converter wrap
This commit is contained in:
parent
60ccd12e4b
commit
a42d141e6a
@ -4,20 +4,21 @@ pub trait CharConverter {
|
|||||||
|
|
||||||
pub struct CharConverterImpl {
|
pub struct CharConverterImpl {
|
||||||
chars: String,
|
chars: String,
|
||||||
|
count: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CharConverterImpl {
|
impl CharConverterImpl {
|
||||||
pub fn new(chars: String) -> Self {
|
pub fn new(chars: String) -> Self {
|
||||||
Self { chars }
|
let count = chars.chars().count();
|
||||||
|
|
||||||
|
Self { chars, count }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CharConverter for CharConverterImpl {
|
impl CharConverter for CharConverterImpl {
|
||||||
fn convert(&self, level: f32) -> char {
|
fn convert(&self, level: f32) -> char {
|
||||||
assert!(level >= 0.0);
|
let len = self.count as f32;
|
||||||
assert!(level < 1.0);
|
let index = (level * len).rem_euclid(len) as usize;
|
||||||
|
|
||||||
let index = (level * self.chars.len() as f32) as usize;
|
|
||||||
|
|
||||||
self.chars.chars().nth(index).unwrap()
|
self.chars.chars().nth(index).unwrap()
|
||||||
}
|
}
|
||||||
@ -28,9 +29,10 @@ mod test {
|
|||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
fn convert_negative_index() {
|
||||||
fn convert_index_below_zero() {
|
let converter = CharConverterImpl::new("abc".to_string());
|
||||||
CharConverterImpl::new("abc".to_string()).convert(-0.1);
|
|
||||||
|
assert_eq!('c', converter.convert(-0.2));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -48,14 +50,16 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn convert_index_one() {
|
fn convert_index_one() {
|
||||||
CharConverterImpl::new("abc".to_string()).convert(1.0);
|
let converter = CharConverterImpl::new("abc".to_string());
|
||||||
|
|
||||||
|
assert_eq!('a', converter.convert(1.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn convert_index_above_one() {
|
fn convert_index_above_one() {
|
||||||
CharConverterImpl::new("abc".to_string()).convert(1.1);
|
let converter = CharConverterImpl::new("abc".to_string());
|
||||||
|
|
||||||
|
assert_eq!('b', converter.convert(1.5));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,10 +16,8 @@ impl ColorConverterImpl {
|
|||||||
|
|
||||||
impl ColorConverter for ColorConverterImpl {
|
impl ColorConverter for ColorConverterImpl {
|
||||||
fn convert(&self, level: f32) -> Color {
|
fn convert(&self, level: f32) -> Color {
|
||||||
assert!(level >= 0.0);
|
let len = self.colors.len() as f32;
|
||||||
assert!(level < 1.0);
|
let index = (level * len).rem_euclid(len) as usize;
|
||||||
|
|
||||||
let index = (level * self.colors.len() as f32) as usize;
|
|
||||||
|
|
||||||
self.colors[index]
|
self.colors[index]
|
||||||
}
|
}
|
||||||
@ -31,9 +29,10 @@ mod test {
|
|||||||
use crossterm::style::Color::*;
|
use crossterm::style::Color::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
fn convert_negative_index() {
|
||||||
fn convert_index_below_zero() {
|
let converter = ColorConverterImpl::new(vec![Red, Green, Blue]);
|
||||||
ColorConverterImpl::new(vec![Red, Green, Blue]).convert(-0.1);
|
|
||||||
|
assert!(matches!(converter.convert(-0.2), Blue));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -51,14 +50,16 @@ mod test {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn convert_index_one() {
|
fn convert_index_one() {
|
||||||
ColorConverterImpl::new(vec![Red, Green, Blue]).convert(1.0);
|
let converter = ColorConverterImpl::new(vec![Red, Green, Blue]);
|
||||||
|
|
||||||
|
assert!(matches!(converter.convert(1.0), Red));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[should_panic]
|
|
||||||
fn convert_index_above_one() {
|
fn convert_index_above_one() {
|
||||||
ColorConverterImpl::new(vec![Red, Green, Blue]).convert(1.1);
|
let converter = ColorConverterImpl::new(vec![Red, Green, Blue]);
|
||||||
|
|
||||||
|
assert!(matches!(converter.convert(1.5), Green));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user