move cursor into separated mod

This commit is contained in:
yggverse 2024-12-18 06:15:09 +02:00
parent 247c7babed
commit c521ae4f5d
3 changed files with 42 additions and 36 deletions

View File

@ -47,7 +47,7 @@ impl Navigation {
back, back,
forward, forward,
g_box, g_box,
model: RefCell::new(Model::new(Vec::new())), // @TODO model: RefCell::new(Model::new(Vec::new())), // @TODO option?
} }
} }
@ -56,7 +56,7 @@ impl Navigation {
pub fn update(&self, matches: Vec<(TextIter, TextIter)>) { pub fn update(&self, matches: Vec<(TextIter, TextIter)>) {
self.back.update(!matches.is_empty()); self.back.update(!matches.is_empty());
self.forward.update(!matches.is_empty()); self.forward.update(!matches.is_empty());
let _ = self.model.replace(Model::new(matches)); self.model.replace(Model::new(matches));
} }
pub fn back(&self, subject: &Subject) -> Option<(TextIter, TextIter)> { pub fn back(&self, subject: &Subject) -> Option<(TextIter, TextIter)> {

View File

@ -1,33 +1,5 @@
struct Cursor { mod cursor;
current: usize, use cursor::Cursor;
last: usize,
}
impl Cursor {
pub fn back(&mut self) {
self.current = if self.current > 0 {
self.current - 1
} else {
self.last
}
}
pub fn next(&mut self) {
self.current = if self.current < self.last {
self.current + 1
} else {
0
}
}
pub fn as_index(&self) -> usize {
if self.current > 0 {
self.current - 1
} else {
0
}
}
}
pub struct Model<T> { pub struct Model<T> {
cursor: Cursor, cursor: Cursor,
@ -37,10 +9,7 @@ pub struct Model<T> {
impl<T> Model<T> { impl<T> Model<T> {
pub fn new(vector: Vec<T>) -> Self { pub fn new(vector: Vec<T>) -> Self {
Self { Self {
cursor: Cursor { cursor: Cursor::new(vector.len()),
current: 0,
last: vector.len(),
},
vector, vector,
} }
} }

View File

@ -0,0 +1,37 @@
pub struct Cursor {
current: usize,
last: usize,
}
impl Cursor {
pub fn new(len: usize) -> Self {
Self {
current: 0,
last: len,
}
}
pub fn back(&mut self) {
self.current = if self.current > 0 {
self.current - 1
} else {
self.last
}
}
pub fn next(&mut self) {
self.current = if self.current < self.last {
self.current + 1
} else {
0
}
}
pub fn as_index(&self) -> usize {
if self.current > 0 {
self.current - 1
} else {
0
}
}
}