| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 17 |
pub struct Cursor { |
| 18 |
index: usize, |
| 19 |
len: usize, |
| 20 |
} |
| 21 |
|
| 22 |
impl Cursor { |
| 23 |
|
| 24 |
|
| 25 |
pub const fn new() -> Self { |
| 26 |
Self { index: 0, len: 0 } |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
pub const fn selected(&self) -> Option<usize> { |
| 35 |
if self.len == 0 { |
| 36 |
None |
| 37 |
} else { |
| 38 |
Some(self.index) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
pub const fn len(&self) -> usize { |
| 43 |
self.len |
| 44 |
} |
| 45 |
|
| 46 |
pub const fn is_empty(&self) -> bool { |
| 47 |
self.len == 0 |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
pub const fn resize(&mut self, len: usize) { |
| 53 |
self.len = len; |
| 54 |
if len == 0 { |
| 55 |
self.index = 0; |
| 56 |
} else if self.index >= len { |
| 57 |
self.index = len - 1; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
pub const fn move_by(&mut self, delta: isize) { |
| 63 |
if self.len == 0 { |
| 64 |
return; |
| 65 |
} |
| 66 |
let last = self.len - 1; |
| 67 |
if delta < 0 { |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
self.index = self.index.saturating_sub(delta.unsigned_abs()); |
| 72 |
} else { |
| 73 |
|
| 74 |
let target = self.index.saturating_add(delta as usize); |
| 75 |
self.index = if target > last { last } else { target }; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
pub const fn next(&mut self) { |
| 80 |
self.move_by(1); |
| 81 |
} |
| 82 |
|
| 83 |
pub const fn prev(&mut self) { |
| 84 |
self.move_by(-1); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
#[cfg(test)] |
| 89 |
mod tests { |
| 90 |
use super::*; |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn empty_cursor_selects_nothing() { |
| 94 |
let mut cursor = Cursor::new(); |
| 95 |
assert_eq!(cursor.selected(), None); |
| 96 |
cursor.next(); |
| 97 |
cursor.prev(); |
| 98 |
assert_eq!( |
| 99 |
cursor.selected(), |
| 100 |
None, |
| 101 |
"movement on an empty list is inert" |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
#[test] |
| 106 |
fn clamps_at_both_ends_instead_of_wrapping() { |
| 107 |
let mut cursor = Cursor::new(); |
| 108 |
cursor.resize(3); |
| 109 |
cursor.prev(); |
| 110 |
assert_eq!( |
| 111 |
cursor.selected(), |
| 112 |
Some(0), |
| 113 |
"no wrap to the end from the top" |
| 114 |
); |
| 115 |
cursor.move_by(99); |
| 116 |
assert_eq!( |
| 117 |
cursor.selected(), |
| 118 |
Some(2), |
| 119 |
"no wrap to the top from the end" |
| 120 |
); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
#[test] |
| 126 |
fn resize_pulls_the_selection_into_range() { |
| 127 |
let mut cursor = Cursor::new(); |
| 128 |
cursor.resize(5); |
| 129 |
cursor.move_by(4); |
| 130 |
cursor.resize(2); |
| 131 |
assert_eq!(cursor.selected(), Some(1), "clamped to the new last row"); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
#[test] |
| 136 |
fn resize_to_empty_clears_the_selection() { |
| 137 |
let mut cursor = Cursor::new(); |
| 138 |
cursor.resize(4); |
| 139 |
cursor.move_by(3); |
| 140 |
cursor.resize(0); |
| 141 |
assert_eq!(cursor.selected(), None); |
| 142 |
assert!(cursor.is_empty()); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
#[test] |
| 148 |
fn growing_the_list_leaves_the_selection_alone() { |
| 149 |
let mut cursor = Cursor::new(); |
| 150 |
cursor.resize(3); |
| 151 |
cursor.move_by(1); |
| 152 |
cursor.resize(10); |
| 153 |
assert_eq!(cursor.selected(), Some(1)); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
#[test] |
| 158 |
fn extreme_negative_delta_saturates_rather_than_overflowing() { |
| 159 |
let mut cursor = Cursor::new(); |
| 160 |
cursor.resize(3); |
| 161 |
cursor.move_by(2); |
| 162 |
cursor.move_by(isize::MIN); |
| 163 |
assert_eq!(cursor.selected(), Some(0)); |
| 164 |
} |
| 165 |
} |
| 166 |
|