Start refactoring ListViews into trait

This commit is contained in:
kageru 2020-01-27 12:06:05 +01:00
parent 61c385404b
commit 559413e4a9
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 59 additions and 41 deletions

@ -17,4 +17,3 @@ fn main() -> Result<(), io::Error> {
let mut tracc = Tracc::new(terminal);
tracc.run()
}

@ -3,6 +3,7 @@ use serde_json::from_reader;
use std::fs::File;
use std::fmt;
use std::io::BufReader;
use crate::tracc::ListView;
pub struct TodoList {
pub todos: Vec<Todo>,
@ -28,7 +29,7 @@ impl Todo {
impl fmt::Display for Todo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "[{}] {}", if self.done { 'x' } else { ' ' }, self.text)
write!(f, "[{}] {}", if self.done { 'x' } else { ' ' }, self.text)
}
}
@ -48,33 +49,6 @@ impl TodoList {
}
}
pub fn selection_down(&mut self) {
self.selected = (self.selected + 1).min(self.todos.len().saturating_sub(1));
}
pub fn selection_up(&mut self) {
self.selected = self.selected.saturating_sub(1);
}
pub fn insert(&mut self, todo: Todo) {
if self.selected == self.todos.len().saturating_sub(1) {
self.todos.push(todo);
self.selected = self.todos.len() - 1;
} else {
self.todos.insert(self.selected + 1, todo);
self.selected += 1;
}
}
pub fn remove_current(&mut self) -> Option<Todo> {
if self.todos.is_empty() {
return None;
}
let index = self.selected;
self.selected = index.min(self.todos.len().saturating_sub(2));
return Some(self.todos.remove(index));
}
pub fn toggle_current(&mut self) {
self.todos[self.selected].done = !self.todos[self.selected].done;
}
@ -82,23 +56,60 @@ impl TodoList {
fn current(&self) -> &Todo {
&self.todos[self.selected]
}
}
pub fn normal_mode(&mut self) {
impl ListView<Todo> for TodoList {
fn selection_down(&mut self) {
self.selected = (self.selected + 1).min(self.todos.len().saturating_sub(1));
}
fn selection_up(&mut self) {
self.selected = self.selected.saturating_sub(1);
}
fn insert<P>(&mut self, todo: Todo, position: P) where P: Into<Option<usize>> {
let pos = position.into().unwrap_or(self.selected);
if pos == self.todos.len().saturating_sub(1) {
self.todos.push(todo);
self.selected = self.todos.len() - 1;
} else {
self.todos.insert(pos + 1, todo);
self.selected = pos + 1;
}
}
fn remove_current(&mut self) {
if self.todos.is_empty() {
return;
}
let index = self.selected;
self.selected = index.min(self.todos.len().saturating_sub(2));
self.register = self.todos.remove(index).into();
}
fn normal_mode(&mut self) {
if self.current().text.is_empty() {
self.remove_current();
self.selected = self.selected.saturating_sub(1);
}
}
pub fn append_to_current(&mut self, chr: char) {
fn append_to_current(&mut self, chr: char) {
self.todos[self.selected].text.push(chr);
}
pub fn current_pop(&mut self) {
fn backspace(&mut self) {
self.todos[self.selected].text.pop();
}
pub fn printable(&self) -> Vec<String> {
fn printable(&self) -> Vec<String> {
self.todos.iter().map(Todo::to_string).collect()
}
fn paste(&mut self) {
if self.register.is_some() {
// Is there a better way?
self.insert(self.register.as_ref().unwrap().clone(), None);
}
}
}

@ -54,7 +54,7 @@ impl Tracc {
Key::Char('j') => self.todos.selection_down(),
Key::Char('k') => self.todos.selection_up(),
Key::Char('o') => {
self.todos.insert(Default::default());
self.todos.insert(Default::default(), None);
self.set_mode(Mode::Insert)?;
}
Key::Char('a') | Key::Char('A') => self.set_mode(Mode::Insert)?,
@ -62,14 +62,10 @@ impl Tracc {
// dd
Key::Char('d') => {
if let Key::Char('d') = inputs.next().unwrap()? {
self.todos.register = self.todos.remove_current()
}
}
Key::Char('p') => {
if self.todos.register.is_some() {
self.todos.insert(self.todos.register.clone().unwrap());
self.todos.remove_current()
}
}
Key::Char('p') => self.todos.paste(),
Key::Char('\t') => {
self.focus = match self.focus {
Focus::Top => Focus::Bottom,
@ -80,7 +76,7 @@ impl Tracc {
},
Mode::Insert => match input {
Key::Char('\n') | Key::Esc => self.set_mode(Mode::Normal)?,
Key::Backspace => self.todos.current_pop(),
Key::Backspace => self.todos.backspace(),
Key::Char(x) => self.todos.append_to_current(x),
_ => (),
},
@ -162,3 +158,15 @@ fn persist_todos(todos: &TodoList, path: &str) {
.or_else(|| panic!("Can’t save todos to JSON. Dumping raw data:\n{}", string))
.map(|mut f| f.write(string.as_bytes()));
}
pub trait ListView<T: std::fmt::Display> {
fn printable(&self) -> Vec<String>;
fn selection_up(&mut self);
fn selection_down(&mut self);
fn insert<P>(&mut self, todo: T, position: P) where P: Into<Option<usize>>;
fn paste(&mut self);
fn remove_current(&mut self);
fn backspace(&mut self);
fn append_to_current(&mut self, chr: char);
fn normal_mode(&mut self);
}