1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
extern crate std;
use std::time::Instant;
pub trait Setter {
fn set_value(&mut self, value: char);
}
pub struct Cursor {
pub pos: usize,
pub value: Vec<char>,
pub min_value: u16,
pub max_value: u16,
pub instant: Instant,
}
impl Cursor {
pub fn new(default_value: Vec<char>, min: u16, max: u16) -> Cursor {
Cursor {
pos: 0,
value: default_value,
min_value: min,
max_value: max,
instant: Instant::now(),
}
}
pub fn get_pos(&self) -> usize {
self.pos
}
pub fn cursor_is_blinking(&self) -> bool {
(self.instant.elapsed().as_millis() % 1000) < 500
}
pub fn reset_cursor(&mut self) {
self.pos = 0;
self.instant = Instant::now();
}
pub fn cursor_left(&mut self) {
if self.pos > 0 {
self.pos -= 1;
}
self.instant = Instant::now();
}
pub fn cursor_right(&mut self) {
if self.pos < self.value.len() - 1 {
self.pos += 1;
}
self.instant = Instant::now();
}
}
pub struct StringCursor {
pub pos: usize,
pub value: String,
pub max_length: usize,
pub instant: Instant,
}
impl StringCursor {
pub fn new(max_length: usize) -> StringCursor {
StringCursor {
pos: 0,
value: String::new(),
max_length: max_length,
instant: Instant::now(),
}
}
pub fn get_pos(&self) -> usize {
self.pos
}
pub fn cursor_is_blinking(&self) -> bool {
(self.instant.elapsed().as_millis() % 1000) < 500
}
pub fn cursor_left(&mut self) {
if self.pos > 0 {
self.pos -= 1;
}
self.instant = Instant::now();
}
pub fn cursor_right(&mut self) {
if self.pos < self.value.len() {
self.pos += 1;
}
self.instant = Instant::now();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_sets_cursor_to_the_right() {
let mut cursor = Cursor::new(vec!['0', '1', '2', '3', '4'], 0, 10000);
let init_pos = cursor.get_pos();
cursor.cursor_right();
let second_pos = cursor.get_pos();
let value_len = cursor.value.len() - 1;
for _i in 2..=value_len {
cursor.cursor_right();
}
let third_pos = cursor.get_pos();
cursor.cursor_right();
assert!((init_pos == 0) && (second_pos == 1) &&
(third_pos == value_len) && (cursor.get_pos() == value_len));
}
#[test]
fn it_sets_cursor_to_the_left() {
let mut cursor = Cursor::new(vec!['0', '1', '2', '3', '4'], 0, 10000);
cursor.cursor_right();
let init_pos = cursor.get_pos();
cursor.cursor_left();
let second_pos = cursor.get_pos();
cursor.cursor_left();
assert!((init_pos == 1) && (second_pos == 0) &&
(cursor.get_pos() == 0));
}
#[test]
fn it_resets_cursor() {
let mut cursor = Cursor::new(vec!['0', '1', '2', '3', '4'], 0, 10000);
let value_len = cursor.value.len() - 1;
for _i in 1..=value_len {
cursor.cursor_right();
}
let init_pos = cursor.get_pos();
cursor.reset_cursor();
assert!((init_pos == value_len) && (cursor.get_pos() == 0));
}
}