remove filtering logic from display code

This commit is contained in:
YK 2024-07-17 11:12:02 +03:00
parent 0631db171f
commit c95e34c6ba
4 changed files with 30 additions and 21 deletions

View File

@ -1,4 +1,3 @@
use std::collections::HashSet; use std::collections::HashSet;
use crossterm::event::{ self, Event, KeyCode, KeyEvent, KeyEventKind }; use crossterm::event::{ self, Event, KeyCode, KeyEvent, KeyEventKind };
@ -182,7 +181,10 @@ impl Widget for &App {
impl Screen { impl Screen {
fn render (&self, app: &App, area: Rect, buf: &mut Buffer) -> () { fn render (&self, app: &App, area: Rect, buf: &mut Buffer) -> () {
match self { match self {
Self::Main { inner, cursor } => main::screen(app, *inner, *cursor, area, buf), Self::Main { inner, cursor } => {
let tasks = inner.get_filtered_tasks(&app.tasks);
main::screen(tasks, *inner, &app.selection, *cursor, area, buf)
},
_ => () _ => ()
} }
} }

View File

@ -5,8 +5,10 @@ use crate::prelude::*;
use super::MainScreen; use super::MainScreen;
type Filter = impl Fn(&&Task) -> bool;
impl MainScreen { impl MainScreen {
pub fn task_filter (self) -> impl Fn(&&Task) -> bool { pub fn task_filter (self) -> Filter {
move |task: &&Task| match self { move |task: &&Task| match self {
Self::All => true, Self::All => true,
Self::Cancelled => task.status == TaskStatus::Cancelled, Self::Cancelled => task.status == TaskStatus::Cancelled,
@ -16,4 +18,8 @@ impl MainScreen {
Self::Recurring => std::mem::discriminant(&task.kind) == std::mem::discriminant(&TaskKind::Recurring { parent: Uuid::default() }), Self::Recurring => std::mem::discriminant(&task.kind) == std::mem::discriminant(&TaskKind::Recurring { parent: Uuid::default() }),
} }
} }
pub fn get_filtered_tasks <'a> (self, tasks: &'a [Task]) -> Vec<&'a Task> {
tasks.iter().filter(self.task_filter()).collect()
}
} }

View File

@ -1,3 +1,5 @@
use std::collections::HashSet;
use crate::{app::{Cursor, Screen}, prelude::*}; use crate::{app::{Cursor, Screen}, prelude::*};
use super::MainScreen; use super::MainScreen;
@ -12,7 +14,10 @@ use utils::truncate_with_ellipsis as tc;
use itertools::Itertools; use itertools::Itertools;
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
pub fn screen (app: &App, ms: MainScreen, cursor: Cursor, area: Rect, buf: &mut Buffer) { pub fn screen (tasks: Vec<&Task>, tab: MainScreen, selection: &HashSet<usize>, cursor: Cursor, area: Rect, buf: &mut Buffer) {
let count = tasks.len();
let w = area.width;
let layout = Layout::default() let layout = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints(vec![ .constraints(vec![
@ -23,9 +28,9 @@ pub fn screen (app: &App, ms: MainScreen, cursor: Cursor, area: Rect, buf: &mut
.split(area); .split(area);
Paragraph::new(Line::from( Paragraph::new(Line::from(
MainScreen::iter().map(|e| { MainScreen::iter().map(|scr| {
let text = format!(" {e} "); let text = format!(" {scr} ");
if e == ms { if scr == tab {
Span::styled(text, Style::default().fg(Color::White).bg(Color::from_u32(0x550055))) Span::styled(text, Style::default().fg(Color::White).bg(Color::from_u32(0x550055)))
} else { } else {
Span::styled(text, Style::default().fg(Color::White)) Span::styled(text, Style::default().fg(Color::White))
@ -36,7 +41,6 @@ pub fn screen (app: &App, ms: MainScreen, cursor: Cursor, area: Rect, buf: &mut
).centered()).block(Block::default().borders(Borders::BOTTOM).padding(Padding::top(1))).render(layout[0], buf); ).centered()).block(Block::default().borders(Borders::BOTTOM).padding(Padding::top(1))).render(layout[0], buf);
let mut list_items = Vec::<ListItem>::new(); let mut list_items = Vec::<ListItem>::new();
let w = area.width;
macro_rules! wx { macro_rules! wx {
($i: literal) => { ($i: literal) => {
@ -54,19 +58,15 @@ pub fn screen (app: &App, ms: MainScreen, cursor: Cursor, area: Rect, buf: &mut
]; ];
let th = |col: usize| if col == cursor.col % cw.len() { Style::default().bold().bg(Color::from_u32(0x253325)) } else { Style::default() }; let th = |col: usize| if col == cursor.col % cw.len() { Style::default().bold().bg(Color::from_u32(0x253325)) } else { Style::default() };
let delimiter = || Span::styled("|", Style::default()); let delimiter = || Span::styled("|", Style::default());
let header: Vec<Span> = cw.iter().enumerate().map(|(idx, (width, h, _))| Span::styled(format!("{: ^width$}", h, width = width), th(idx))).intersperse_with(delimiter).collect_vec(); let header: Vec<Span> = cw.iter().enumerate().map(|(idx, (width, h, _))| Span::styled(format!("{: ^width$}", h, width = width), th(idx))).intersperse_with(delimiter).collect_vec();
let iterator = || app.tasks.iter().filter(ms.task_filter());
let item_count = iterator().count();
let td = |row: usize, col: usize| match (row, col) { let td = |row: usize, col: usize| match (row, col) {
(r, c) if r == cursor.row % item_count && c == cursor.col % cw.len() => Style::default().bg(Color::from_u32(0x007700)), (r, c) if r == cursor.row % count && c == cursor.col % cw.len() => Style::default().bg(Color::from_u32(0x007700)),
(r, _) if r == cursor.row % item_count => Style::default().bg(Color::from_u32(0x005500)), (r, _) if r == cursor.row % count => Style::default().bg(Color::from_u32(0x005500)),
// (_, c) if c == app.cursor.1 % cw.len() => Style::default().bg(Color::from_u32(0x001500)), // (_, c) if c == app.cursor.1 % cw.len() => Style::default().bg(Color::from_u32(0x001500)),
_ => Style::default() _ => Style::default()
}; };
@ -76,12 +76,12 @@ pub fn screen (app: &App, ms: MainScreen, cursor: Cursor, area: Rect, buf: &mut
list_items.push(ListItem::new(Line::from(header))); list_items.push(ListItem::new(Line::from(header)));
list_items.push(ListItem::new(Line::from(Span::from("-".repeat(w as usize))))); list_items.push(ListItem::new(Line::from(Span::from("-".repeat(w as usize)))));
for s @ (idx, _task) in iterator().enumerate() { for s @ (idx, _task) in tasks.into_iter().enumerate() {
let idx = idx % item_count; let idx = idx % count;
let td = tr(idx); let td = tr(idx);
let bg = if app.selection.contains(&idx) { let bg = if selection.contains(&idx) {
Color::from_u32(0x000044) Color::from_u32(0x000044)
} else if idx == cursor.row % item_count { } else if idx == cursor.row % count {
Color::from_u32(0x002200) Color::from_u32(0x002200)
} else { } else {
Color::default() Color::default()
@ -103,9 +103,9 @@ pub fn screen (app: &App, ms: MainScreen, cursor: Cursor, area: Rect, buf: &mut
]) ])
.split(layout[2]); .split(layout[2]);
if !app.selection.is_empty() { if !selection.is_empty() {
let total = item_count; let total = count;
let selected = app.selection.len(); let selected = selection.len();
Paragraph::new(Line::from(Span::styled(format!("[{selected}/{total}]"), Style::default()))).block(Block::default().borders(Borders::ALL)).render(footer[2], buf); Paragraph::new(Line::from(Span::styled(format!("[{selected}/{total}]"), Style::default()))).block(Block::default().borders(Borders::ALL)).render(footer[2], buf);
} }

View File

@ -1,5 +1,6 @@
#![feature(variant_count)] #![feature(variant_count)]
#![feature(let_chains)] #![feature(let_chains)]
#![feature(type_alias_impl_trait)]
use color_eyre::Result; use color_eyre::Result;