From 559413e4a9c27292962535898780ecf66994d877 Mon Sep 17 00:00:00 2001 From: kageru Date: Mon, 27 Jan 2020 12:06:05 +0100 Subject: [PATCH] Start refactoring ListViews into trait --- src/main.rs | 1 - src/todolist.rs | 75 ++++++++++++++++++++++++++++--------------------- src/tracc.rs | 24 ++++++++++------ 3 files changed, 59 insertions(+), 41 deletions(-) diff --git a/src/main.rs b/src/main.rs index 81c7257..5513c93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,4 +17,3 @@ fn main() -> Result<(), io::Error> { let mut tracc = Tracc::new(terminal); tracc.run() } - diff --git a/src/todolist.rs b/src/todolist.rs index af5d251..8412f5a 100644 --- a/src/todolist.rs +++ b/src/todolist.rs @@ -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, @@ -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 { - 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 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

(&mut self, todo: Todo, position: P) where P: Into> { + 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 { + fn printable(&self) -> Vec { 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); + } + } } diff --git a/src/tracc.rs b/src/tracc.rs index f610412..213de43 100644 --- a/src/tracc.rs +++ b/src/tracc.rs @@ -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 { + fn printable(&self) -> Vec; + fn selection_up(&mut self); + fn selection_down(&mut self); + fn insert

(&mut self, todo: T, position: P) where P: Into>; + 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); +}