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 untrusted user: kageru
GPG Key ID: 8282A2BEA4ADA3D2
3 changed files with 59 additions and 41 deletions

View File

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

View File

@ -3,6 +3,7 @@ use serde_json::from_reader;
use std::fs::File; use std::fs::File;
use std::fmt; use std::fmt;
use std::io::BufReader; use std::io::BufReader;
use crate::tracc::ListView;
pub struct TodoList { pub struct TodoList {
pub todos: Vec<Todo>, pub todos: Vec<Todo>,
@ -28,7 +29,7 @@ impl Todo {
impl fmt::Display for Todo { impl fmt::Display for Todo {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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) { pub fn toggle_current(&mut self) {
self.todos[self.selected].done = !self.todos[self.selected].done; self.todos[self.selected].done = !self.todos[self.selected].done;
} }
@ -82,23 +56,60 @@ impl TodoList {
fn current(&self) -> &Todo { fn current(&self) -> &Todo {
&self.todos[self.selected] &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() { if self.current().text.is_empty() {
self.remove_current(); self.remove_current();
self.selected = self.selected.saturating_sub(1); 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); self.todos[self.selected].text.push(chr);
} }
pub fn current_pop(&mut self) { fn backspace(&mut self) {
self.todos[self.selected].text.pop(); 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() 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);
}
}
} }

View File

@ -54,7 +54,7 @@ impl Tracc {
Key::Char('j') => self.todos.selection_down(), Key::Char('j') => self.todos.selection_down(),
Key::Char('k') => self.todos.selection_up(), Key::Char('k') => self.todos.selection_up(),
Key::Char('o') => { Key::Char('o') => {
self.todos.insert(Default::default()); self.todos.insert(Default::default(), None);
self.set_mode(Mode::Insert)?; self.set_mode(Mode::Insert)?;
} }
Key::Char('a') | Key::Char('A') => self.set_mode(Mode::Insert)?, Key::Char('a') | Key::Char('A') => self.set_mode(Mode::Insert)?,
@ -62,14 +62,10 @@ impl Tracc {
// dd // dd
Key::Char('d') => { Key::Char('d') => {
if let Key::Char('d') = inputs.next().unwrap()? { if let Key::Char('d') = inputs.next().unwrap()? {
self.todos.register = self.todos.remove_current() self.todos.remove_current()
}
}
Key::Char('p') => {
if self.todos.register.is_some() {
self.todos.insert(self.todos.register.clone().unwrap());
} }
} }
Key::Char('p') => self.todos.paste(),
Key::Char('\t') => { Key::Char('\t') => {
self.focus = match self.focus { self.focus = match self.focus {
Focus::Top => Focus::Bottom, Focus::Top => Focus::Bottom,
@ -80,7 +76,7 @@ impl Tracc {
}, },
Mode::Insert => match input { Mode::Insert => match input {
Key::Char('\n') | Key::Esc => self.set_mode(Mode::Normal)?, 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), 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)) .or_else(|| panic!("Can’t save todos to JSON. Dumping raw data:\n{}", string))
.map(|mut f| f.write(string.as_bytes())); .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);
}