From 32a73184e888b83c36100cd517a97bf585c11aee Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Sun, 12 May 2024 11:48:01 -0400 Subject: [PATCH] chore: Decrease tech debt --- src/app.rs | 39 +++++++++++++++++++-------------------- src/pane/context.rs | 5 ++--- src/task_report.rs | 2 +- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/app.rs b/src/app.rs index 854487a..c9dabf4 100644 --- a/src/app.rs +++ b/src/app.rs @@ -43,8 +43,7 @@ use crate::{ action::Action, calendar::Calendar, completion::{get_start_word_under_cursor, CompletionList}, - config, - config::Config, + config::{self, Config}, event::{Event, KeyCode}, help::Help, history::HistoryContext, @@ -928,8 +927,7 @@ impl TaskwarriorTui { let maximum_column_width = area.width; let widths = self.calculate_widths(&contexts, &headers, maximum_column_width); - let selected = self.contexts.table_state.current_selection().unwrap_or_default(); - let header = headers.iter(); + let selected = self.contexts.table_state.selected().unwrap_or_default(); let mut rows = vec![]; let mut highlight_style = Style::default(); for (i, context) in contexts.iter().enumerate() { @@ -937,8 +935,8 @@ impl TaskwarriorTui { if &self.contexts.rows[i].active == "yes" { style = self.config.uda_style_context_active; } - rows.push(Row::StyledData(context.iter(), style)); - if i == self.contexts.table_state.current_selection().unwrap_or_default() { + rows.push(ratatui::widgets::Row::new(context.clone()).style(style)); + if i == self.contexts.table_state.selected().unwrap_or_default() { highlight_style = style; } } @@ -949,25 +947,26 @@ impl TaskwarriorTui { .collect(); let highlight_style = highlight_style.add_modifier(Modifier::BOLD); - let t = Table::new(header, rows.into_iter()) + let t = ratatui::widgets::Table::new(rows.into_iter(), constraints.clone()) .block( Block::default() .borders(Borders::ALL) .border_type(BorderType::Rounded) .title(Line::from(vec![Span::styled("Context", Style::default().add_modifier(Modifier::BOLD))])), ) - .header_style( - self - .config - .color - .get("color.label") - .copied() - .unwrap_or_default() - .add_modifier(Modifier::UNDERLINED), + .header( + ratatui::widgets::Row::new(headers).style( + self + .config + .color + .get("color.label") + .copied() + .unwrap_or_default() + .add_modifier(Modifier::UNDERLINED), + ), ) .highlight_style(highlight_style) - .highlight_symbol(&self.config.uda_selection_indicator) - .widths(&constraints); + .highlight_symbol(self.config.uda_selection_indicator.clone()); f.render_stateful_widget(t, area, &mut self.contexts.table_state); } @@ -1442,7 +1441,7 @@ impl TaskwarriorTui { } pub fn context_next(&mut self) { - let i = match self.contexts.table_state.current_selection() { + let i = match self.contexts.table_state.selected() { Some(i) => { if i >= self.contexts.len() - 1 { 0 @@ -1456,7 +1455,7 @@ impl TaskwarriorTui { } pub fn context_previous(&mut self) { - let i = match self.contexts.table_state.current_selection() { + let i = match self.contexts.table_state.selected() { Some(i) => { if i == 0 { self.contexts.len() - 1 @@ -1470,7 +1469,7 @@ impl TaskwarriorTui { } pub fn context_select(&mut self) -> Result<()> { - let i = self.contexts.table_state.current_selection().unwrap_or_default(); + let i = self.contexts.table_state.selected().unwrap_or_default(); let mut command = std::process::Command::new("task"); command.arg("context").arg(&self.contexts.rows[i].name); command.output()?; diff --git a/src/pane/context.rs b/src/pane/context.rs index 07a5daf..2f44a9b 100644 --- a/src/pane/context.rs +++ b/src/pane/context.rs @@ -32,7 +32,6 @@ use crate::{ app::{Mode, TaskwarriorTui}, event::KeyCode, pane::Pane, - table::TableState, }; #[derive(Debug, Clone, Default)] @@ -55,7 +54,7 @@ impl ContextDetails { } pub struct ContextsState { - pub table_state: TableState, + pub table_state: ratatui::widgets::TableState, pub report_height: u16, pub columns: Vec, pub rows: Vec, @@ -64,7 +63,7 @@ pub struct ContextsState { impl ContextsState { pub(crate) fn new() -> Self { Self { - table_state: TableState::default(), + table_state: ratatui::widgets::TableState::default(), report_height: 0, columns: vec![NAME.to_string(), TYPE.to_string(), DEFINITION.to_string(), ACTIVE.to_string()], rows: vec![], diff --git a/src/task_report.rs b/src/task_report.rs index 8f20e73..74fb0ee 100644 --- a/src/task_report.rs +++ b/src/task_report.rs @@ -14,7 +14,7 @@ pub fn format_date_time(dt: NaiveDateTime) -> String { pub fn format_date(dt: NaiveDateTime) -> String { let offset = Local.offset_from_utc_datetime(&dt); - let dt = DateTime::::from_utc(dt, offset); + let dt = DateTime::::from_naive_utc_and_offset(dt, offset); dt.format("%Y-%m-%d").to_string() }