forked from kageru/tracc
split into more modules
This commit is contained in:
parent
1204be40fb
commit
129e890670
|
@ -0,0 +1,33 @@
|
||||||
|
use tui::layout::*;
|
||||||
|
use tui::style::{Color, Style};
|
||||||
|
use tui::widgets::*;
|
||||||
|
pub fn selectable_list<'a, C: AsRef<str>>(
|
||||||
|
title: &'a str,
|
||||||
|
content: &'a [C],
|
||||||
|
selected: Option<usize>,
|
||||||
|
) -> SelectableList<'a> {
|
||||||
|
SelectableList::default()
|
||||||
|
.block(
|
||||||
|
Block::default()
|
||||||
|
.title(title)
|
||||||
|
.borders(Borders::TOP | Borders::RIGHT | Borders::LEFT),
|
||||||
|
)
|
||||||
|
.items(content)
|
||||||
|
.select(selected)
|
||||||
|
.highlight_style(Style::default().fg(Color::LightGreen))
|
||||||
|
.highlight_symbol(">")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn layout(r: Rect) -> Vec<Rect> {
|
||||||
|
Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints(
|
||||||
|
[
|
||||||
|
Constraint::Percentage(40),
|
||||||
|
Constraint::Percentage(40),
|
||||||
|
Constraint::Percentage(20),
|
||||||
|
]
|
||||||
|
.as_ref(),
|
||||||
|
)
|
||||||
|
.split(r)
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
pub trait ListView<T: fmt::Display + Clone> {
|
||||||
|
// get properties of implementations
|
||||||
|
fn selection_pointer(&mut self) -> &mut usize;
|
||||||
|
fn list(&mut self) -> &mut Vec<T>;
|
||||||
|
fn register(&mut self) -> &mut Option<T>;
|
||||||
|
|
||||||
|
// specific input handling
|
||||||
|
fn backspace(&mut self);
|
||||||
|
fn append_to_current(&mut self, chr: char);
|
||||||
|
fn normal_mode(&mut self);
|
||||||
|
fn toggle_current(&mut self);
|
||||||
|
|
||||||
|
// selection manipulation
|
||||||
|
fn selection_up(&mut self) {
|
||||||
|
*self.selection_pointer() = self.selection_pointer().saturating_sub(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn selection_down(&mut self) {
|
||||||
|
*self.selection_pointer() =
|
||||||
|
(*self.selection_pointer() + 1).min(self.list().len().saturating_sub(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
// adding/removing elements
|
||||||
|
fn insert(&mut self, item: T, position: Option<usize>) {
|
||||||
|
let pos = position.unwrap_or(*self.selection_pointer());
|
||||||
|
if pos == self.list().len().saturating_sub(1) {
|
||||||
|
self.list().push(item);
|
||||||
|
*self.selection_pointer() = self.list().len() - 1;
|
||||||
|
} else {
|
||||||
|
self.list().insert(pos + 1, item);
|
||||||
|
*self.selection_pointer() = pos + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_current(&mut self) {
|
||||||
|
if self.list().is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let index = *self.selection_pointer();
|
||||||
|
*self.selection_pointer() = index.min(self.list().len().saturating_sub(2));
|
||||||
|
*self.register() = self.list().remove(index).into();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn paste(&mut self) {
|
||||||
|
if let Some(item) = self.register().clone() {
|
||||||
|
self.insert(item, None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// printing
|
||||||
|
fn printable(&mut self) -> Vec<String> {
|
||||||
|
self.list().iter().map(T::to_string).collect()
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,8 @@ use std::io;
|
||||||
use termion::raw::IntoRawMode;
|
use termion::raw::IntoRawMode;
|
||||||
use tui::backend::TermionBackend;
|
use tui::backend::TermionBackend;
|
||||||
use tui::Terminal;
|
use tui::Terminal;
|
||||||
|
mod layout;
|
||||||
|
mod listview;
|
||||||
mod timesheet;
|
mod timesheet;
|
||||||
mod todolist;
|
mod todolist;
|
||||||
mod tracc;
|
mod tracc;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use super::tracc::ListView;
|
use super::listview::ListView;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::from_reader;
|
use serde_json::from_reader;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::tracc::ListView;
|
use crate::listview::ListView;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::from_reader;
|
use serde_json::from_reader;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
132
src/tracc.rs
132
src/tracc.rs
|
@ -1,13 +1,12 @@
|
||||||
|
use super::layout;
|
||||||
|
use super::listview::ListView;
|
||||||
use super::timesheet::TimeSheet;
|
use super::timesheet::TimeSheet;
|
||||||
use super::todolist::TodoList;
|
use super::todolist::TodoList;
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::fmt;
|
|
||||||
use std::io::{self, Write};
|
use std::io::{self, Write};
|
||||||
use termion::event::Key;
|
use termion::event::Key;
|
||||||
use termion::input::TermRead;
|
use termion::input::TermRead;
|
||||||
use tui::backend::TermionBackend;
|
use tui::backend::TermionBackend;
|
||||||
use tui::layout::*;
|
|
||||||
use tui::style::{Color, Style};
|
|
||||||
use tui::widgets::*;
|
use tui::widgets::*;
|
||||||
|
|
||||||
type Terminal = tui::Terminal<TermionBackend<termion::raw::RawTerminal<io::Stdout>>>;
|
type Terminal = tui::Terminal<TermionBackend<termion::raw::RawTerminal<io::Stdout>>>;
|
||||||
|
@ -107,56 +106,32 @@ impl Tracc {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn refresh(&mut self) -> Result<(), io::Error> {
|
fn refresh(&mut self) -> Result<(), io::Error> {
|
||||||
fn selectable_list<'a, C: AsRef<str>>(
|
let summary_content = [Text::raw(format!(
|
||||||
title: &'a str,
|
"Sum for today: {}\n{}",
|
||||||
content: &'a [C],
|
self.times.sum_as_str(),
|
||||||
selected: Option<usize>,
|
self.times.time_by_tasks()
|
||||||
) -> SelectableList<'a> {
|
))];
|
||||||
SelectableList::default()
|
let mut summary = Paragraph::new(summary_content.iter())
|
||||||
.block(
|
.wrap(true)
|
||||||
Block::default()
|
.block(Block::default().borders(Borders::ALL));
|
||||||
.title(title)
|
let todos = self.todos.printable();
|
||||||
.borders(Borders::TOP | Borders::RIGHT | Borders::LEFT),
|
let mut todolist = layout::selectable_list(
|
||||||
)
|
" t r a c c ",
|
||||||
.items(content)
|
&todos,
|
||||||
.select(selected)
|
Some(self.todos.selected).filter(|_| self.focus == Focus::Top),
|
||||||
.highlight_style(Style::default().fg(Color::LightGreen))
|
);
|
||||||
.highlight_symbol(">")
|
let times = self.times.printable();
|
||||||
}
|
let mut timelist = layout::selectable_list(
|
||||||
|
" 🕑 ",
|
||||||
let printable_todos = self.todos.printable();
|
×,
|
||||||
let top_focus = Some(self.todos.selected).filter(|_| self.focus == Focus::Top);
|
Some(self.times.selected).filter(|_| self.focus == Focus::Bottom),
|
||||||
let printable_times = self.times.printable();
|
);
|
||||||
let bottom_focus = Some(self.times.selected).filter(|_| self.focus == Focus::Bottom);
|
|
||||||
let total_time = self.times.sum_as_str();
|
|
||||||
let times = self.times.time_by_tasks();
|
|
||||||
|
|
||||||
self.terminal.draw(|mut frame| {
|
self.terminal.draw(|mut frame| {
|
||||||
let size = frame.size();
|
let chunks = layout::layout(frame.size());
|
||||||
let chunks = Layout::default()
|
todolist.render(&mut frame, chunks[0]);
|
||||||
.direction(Direction::Vertical)
|
timelist.render(&mut frame, chunks[1]);
|
||||||
.constraints(
|
summary.render(&mut frame, chunks[2]);
|
||||||
[
|
|
||||||
Constraint::Percentage(40),
|
|
||||||
Constraint::Percentage(40),
|
|
||||||
Constraint::Percentage(20),
|
|
||||||
]
|
|
||||||
.as_ref(),
|
|
||||||
)
|
|
||||||
.split(size);
|
|
||||||
selectable_list(" t r a c c ", &printable_todos, top_focus)
|
|
||||||
.render(&mut frame, chunks[0]);
|
|
||||||
selectable_list(" 🕑 ", &printable_times, bottom_focus).render(&mut frame, chunks[1]);
|
|
||||||
Paragraph::new(
|
|
||||||
[
|
|
||||||
Text::raw(format!("Sum for today: {}\n", total_time)),
|
|
||||||
Text::raw(times),
|
|
||||||
]
|
|
||||||
.iter(),
|
|
||||||
)
|
|
||||||
.wrap(true)
|
|
||||||
.block(Block::default().borders(Borders::ALL))
|
|
||||||
.render(&mut frame, chunks[2]);
|
|
||||||
})?;
|
})?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -178,58 +153,3 @@ fn persist_state(todos: &TodoList, times: &TimeSheet) {
|
||||||
let times = serde_json::to_string(×.times).unwrap();
|
let times = serde_json::to_string(×.times).unwrap();
|
||||||
write(JSON_PATH_TIME, times);
|
write(JSON_PATH_TIME, times);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ListView<T: fmt::Display + Clone> {
|
|
||||||
// get properties of implementations
|
|
||||||
fn selection_pointer(&mut self) -> &mut usize;
|
|
||||||
fn list(&mut self) -> &mut Vec<T>;
|
|
||||||
fn register(&mut self) -> &mut Option<T>;
|
|
||||||
|
|
||||||
// specific input handling
|
|
||||||
fn backspace(&mut self);
|
|
||||||
fn append_to_current(&mut self, chr: char);
|
|
||||||
fn normal_mode(&mut self);
|
|
||||||
fn toggle_current(&mut self);
|
|
||||||
|
|
||||||
// selection manipulation
|
|
||||||
fn selection_up(&mut self) {
|
|
||||||
*self.selection_pointer() = self.selection_pointer().saturating_sub(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn selection_down(&mut self) {
|
|
||||||
*self.selection_pointer() =
|
|
||||||
(*self.selection_pointer() + 1).min(self.list().len().saturating_sub(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
// adding/removing elements
|
|
||||||
fn insert(&mut self, item: T, position: Option<usize>) {
|
|
||||||
let pos = position.unwrap_or(*self.selection_pointer());
|
|
||||||
if pos == self.list().len().saturating_sub(1) {
|
|
||||||
self.list().push(item);
|
|
||||||
*self.selection_pointer() = self.list().len() - 1;
|
|
||||||
} else {
|
|
||||||
self.list().insert(pos + 1, item);
|
|
||||||
*self.selection_pointer() = pos + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn remove_current(&mut self) {
|
|
||||||
if self.list().is_empty() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let index = *self.selection_pointer();
|
|
||||||
*self.selection_pointer() = index.min(self.list().len().saturating_sub(2));
|
|
||||||
*self.register() = self.list().remove(index).into();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn paste(&mut self) {
|
|
||||||
if let Some(item) = self.register().clone() {
|
|
||||||
self.insert(item, None);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// printing
|
|
||||||
fn printable(&mut self) -> Vec<String> {
|
|
||||||
self.list().iter().map(T::to_string).collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user