diff --git a/src/app.rs b/src/app.rs index 886ec4a..b1d94e9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -4,8 +4,8 @@ use crate::context::Context; use crate::help::Help; use crate::table::{Row, Table, TableState}; use crate::task_report::TaskReportTable; -use crate::util::{Events, Event}; use crate::util::Key; +use crate::util::{Event, Events}; use std::cmp::Ordering; use std::convert::TryInto; @@ -29,7 +29,7 @@ use std::sync::{Arc, Mutex}; use std::{sync::mpsc, thread, time::Duration}; use tui::{ backend::Backend, - layout::{Alignment, Constraint, Direction, Layout, Rect, Margin}, + layout::{Alignment, Constraint, Direction, Layout, Margin, Rect}, style::{Color, Modifier, Style}, terminal::Frame, text::{Span, Spans, Text}, @@ -290,7 +290,7 @@ impl TTApp { for (i, (_i, g)) in self.filter.as_str().grapheme_indices(true).enumerate() { if _i == self.filter.pos() { position = i; - break + break; } } f.set_cursor(rects[1].x + position as u16 + 1, rects[1].y + 1); @@ -307,7 +307,7 @@ impl TTApp { for (i, (_i, g)) in self.modify.as_str().grapheme_indices(true).enumerate() { if _i == self.modify.pos() { position = i; - break + break; } } f.set_cursor(rects[1].x + position as u16 + 1, rects[1].y + 1); @@ -327,7 +327,7 @@ impl TTApp { for (i, (_i, g)) in self.command.as_str().grapheme_indices(true).enumerate() { if _i == self.command.pos() { position = i; - break + break; } } f.set_cursor(rects[1].x + position as u16 + 1, rects[1].y + 1); @@ -344,7 +344,7 @@ impl TTApp { for (i, (_i, g)) in self.command.as_str().grapheme_indices(true).enumerate() { if _i == self.command.pos() { position = i; - break + break; } } f.set_cursor(rects[1].x + position as u16 + 1, rects[1].y + 1); @@ -361,7 +361,7 @@ impl TTApp { for (i, (_i, g)) in self.command.as_str().grapheme_indices(true).enumerate() { if _i == self.command.pos() { position = i; - break + break; } } f.set_cursor(rects[1].x + position as u16 + 1, rects[1].y + 1); @@ -381,7 +381,7 @@ impl TTApp { for (i, (_i, g)) in self.command.as_str().grapheme_indices(true).enumerate() { if _i == self.command.pos() { position = i; - break + break; } } f.set_cursor(rects[1].x + position as u16 + 1, rects[1].y + 1); @@ -430,9 +430,13 @@ impl TTApp { let area = centered_rect(80, 50, f.size()); - f.render_widget(Clear, area.inner(&Margin { - vertical: 0, horizontal: 0, - })); + f.render_widget( + Clear, + area.inner(&Margin { + vertical: 0, + horizontal: 0, + }), + ); let (contexts, headers) = self.get_all_contexts(); @@ -467,9 +471,10 @@ impl TTApp { Block::default() .borders(Borders::ALL) .border_type(BorderType::Rounded) - .title(Spans::from(vec![ - Span::styled("Context", Style::default().add_modifier(Modifier::BOLD)), - ])), + .title(Spans::from(vec![Span::styled( + "Context", + Style::default().add_modifier(Modifier::BOLD), + )])), ) .header_style(Style::default().add_modifier(Modifier::UNDERLINED)) .highlight_style(highlight_style) @@ -506,7 +511,10 @@ impl TTApp { let selected = self.task_table_state.selected().unwrap_or_default(); let task_id = self.tasks.lock().unwrap()[selected].id().unwrap_or_default(); let task_uuid = self.tasks.lock().unwrap()[selected].uuid().clone(); - let output = Command::new("task").arg("rc.color=off").arg(format!("{}", task_uuid)).output(); + let output = Command::new("task") + .arg("rc.color=off") + .arg(format!("{}", task_uuid)) + .output(); if let Ok(output) = output { let data = String::from_utf8_lossy(&output.stdout); let p = Paragraph::new(Text::from(&data[..])).block( @@ -561,8 +569,12 @@ impl TTApp { style } - pub fn calculate_widths(&self, tasks: &Vec>, headers: &Vec, maximum_column_width: u16) -> Vec { - + pub fn calculate_widths( + &self, + tasks: &Vec>, + headers: &Vec, + maximum_column_width: u16, + ) -> Vec { // naive implementation of calculate widths let mut widths = headers.iter().map(|s| s.len()).collect::>(); @@ -576,7 +588,7 @@ impl TTApp { if header == "Description" || header == "Definition" { // always give description or definition the most room to breath widths[i] = maximum_column_width as usize; - break + break; } } for (i, header) in headers.iter().enumerate() { @@ -590,12 +602,12 @@ impl TTApp { while (widths.iter().sum::() as u16) >= maximum_column_width - (headers.len()) as u16 { let index = widths.iter().position(|i| i == widths.iter().max().unwrap()).unwrap(); if widths[index] == 1 { - break + break; } widths[index] -= 1; } - return widths + return widths; } fn draw_task_report(&mut self, f: &mut Frame, rect: Rect) { @@ -626,7 +638,7 @@ impl TTApp { for (i, header) in headers.iter().enumerate() { if header == "Description" || header == "Definition" { self.task_report_table.description_width = widths[i] - 1; - break + break; } } @@ -684,7 +696,11 @@ impl TTApp { } pub fn get_all_contexts(&self) -> (Vec>, Vec) { - let contexts = self.contexts.iter().map(|c| vec![c.name.clone(), c.description.clone(), c.active.clone()]).collect(); + let contexts = self + .contexts + .iter() + .map(|c| vec![c.name.clone(), c.description.clone(), c.active.clone()]) + .collect(); let headers = vec!["Name".to_string(), "Description".to_string(), "Active".to_string()]; (contexts, headers) } @@ -715,7 +731,7 @@ impl TTApp { } else { i + 1 } - }, + } None => 0, }; self.context_table_state.select(Some(i)); @@ -737,10 +753,7 @@ impl TTApp { pub fn context_select(&mut self) { let i = self.context_table_state.selected().unwrap(); - let output = Command::new("task") - .arg("context") - .arg(&self.contexts[i].name) - .output(); + let output = Command::new("task").arg("context").arg(&self.contexts[i].name).output(); } pub fn task_report_top(&mut self) { @@ -856,16 +869,18 @@ impl TTApp { let definition = line.replacen(name, "", 1); let definition = definition.strip_suffix(active).unwrap(); if i == 0 || i == 1 { - continue + continue; } else { let context = Context::new(name.to_string(), definition.trim().to_string(), active.to_string()); self.contexts.push(context); } } if self.contexts.iter().any(|r| r.active != "no") { - self.contexts.insert(0, Context::new("none".to_string(), "".to_string(), "no".to_string())) + self.contexts + .insert(0, Context::new("none".to_string(), "".to_string(), "no".to_string())) } else { - self.contexts.insert(0, Context::new("none".to_string(), "".to_string(), "yes".to_string())) + self.contexts + .insert(0, Context::new("none".to_string(), "".to_string(), "yes".to_string())) } Ok(()) @@ -998,7 +1013,11 @@ impl TTApp { self.modify.update("", 0); Ok(()) } else { - Err(format!("Unable to modify task with uuid {}. Failed with status code {}", task_uuid, o.status.code().unwrap())) + Err(format!( + "Unable to modify task with uuid {}. Failed with status code {}", + task_uuid, + o.status.code().unwrap() + )) } } Err(_) => Err(format!( @@ -1038,7 +1057,11 @@ impl TTApp { self.command.update("", 0); Ok(()) } else { - Err(format!("Unable to annotate task with uuid {}. Failed with status code {}", task_uuid, o.status.code().unwrap())) + Err(format!( + "Unable to annotate task with uuid {}. Failed with status code {}", + task_uuid, + o.status.code().unwrap() + )) } } Err(_) => Err(format!( @@ -1348,7 +1371,7 @@ impl TTApp { Key::Char('g') => match events.next()? { Event::Input(Key::Char('g')) => self.task_report_top(), _ => (), - } + }, Key::Down | Key::Char('j') => self.task_report_next(), Key::Up | Key::Char('k') => self.task_report_previous(), Key::PageDown | Key::Char('J') => self.task_report_next_page(), @@ -1435,7 +1458,7 @@ impl TTApp { AppMode::TaskContextMenu => match input { Key::Esc | Key::Char('q') => { self.mode = AppMode::TaskReport; - }, + } Key::Down | Key::Char('j') => self.context_next(), Key::Up | Key::Char('k') => self.context_previous(), Key::Char('\n') => { @@ -1641,6 +1664,5 @@ mod tests { fn test_app() { let app = TTApp::new().unwrap(); assert!(app.task_by_index(0).is_some()) - } } diff --git a/src/config.rs b/src/config.rs index 8b25692..6ddc869 100644 --- a/src/config.rs +++ b/src/config.rs @@ -27,9 +27,7 @@ impl TColor { } pub fn new(fg: Color, bg: Color, modifiers: Vec) -> Self { - Self { - fg, bg, modifiers, - } + Self { fg, bg, modifiers } } } @@ -61,7 +59,6 @@ impl TaskWarriorBool for str { } } - #[derive(Debug)] pub struct Config { pub enabled: bool, @@ -98,10 +95,16 @@ impl Config { uda_selection_dim: Self::get_uda_selection_dim(), uda_selection_blink: Self::get_uda_selection_blink(), uda_calendar_months_per_row: Self::get_uda_months_per_row(), - uda_style_calendar_title: Self::get_uda_style("calendar.title") - .unwrap_or(TColor::new(Color::Reset, Color::Reset, vec![])), - uda_style_context_active: Self::get_uda_style("context.active") - .unwrap_or(TColor::new(Color::Reset, Color::Reset, vec![])), + uda_style_calendar_title: Self::get_uda_style("calendar.title").unwrap_or(TColor::new( + Color::Reset, + Color::Reset, + vec![], + )), + uda_style_context_active: Self::get_uda_style("context.active").unwrap_or(TColor::new( + Color::Reset, + Color::Reset, + vec![], + )), }) } diff --git a/src/main.rs b/src/main.rs index 5217d7b..e17e7fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,8 +16,8 @@ use clap::{App, Arg}; use std::env; use std::error::Error; use std::io::Write; -use std::time::Duration; use std::panic; +use std::time::Duration; use crate::util::Key; use app::{AppMode, TTApp}; @@ -120,13 +120,12 @@ fn tui_main(_config: &str) -> Result<(), Box> { #[cfg(test)] mod tests { - use crate::util::{destruct_terminal, setup_terminal, Event, EventConfig, Events}; use crate::util::Key; + use crate::util::{destruct_terminal, setup_terminal, Event, EventConfig, Events}; use std::time::Duration; #[test] fn test_main() { - let mut terminal = setup_terminal(); terminal.clear().unwrap(); // Setup event handlers @@ -141,15 +140,15 @@ mod tests { Event::Input(Key::Char('g')) => events.resume_ticker(), _ => (), } - }, + } Event::Input(input) => { print!("\r\n"); dbg!(input); - }, + } Event::Tick => { print!("\r\n"); dbg!("tick"); - }, + } }; destruct_terminal(); diff --git a/src/table.rs b/src/table.rs index 8bd3ad2..bca6581 100644 --- a/src/table.rs +++ b/src/table.rs @@ -14,9 +14,9 @@ use tui::{ style::Style, widgets::{Block, StatefulWidget, Widget}, }; -use unicode_width::UnicodeWidthStr; use unicode_segmentation::Graphemes; use unicode_segmentation::UnicodeSegmentation; +use unicode_width::UnicodeWidthStr; #[derive(Debug, Clone)] pub struct TableState { @@ -278,9 +278,21 @@ where let mut index = 0; if y < table_area.bottom() { for (w, t) in solved_widths.iter().zip(self.header.by_ref()) { - buf.set_stringn(x, y, format!("{symbol:>width$}", symbol=" ", width=*w as usize), *w as usize, self.header_style); + buf.set_stringn( + x, + y, + format!("{symbol:>width$}", symbol = " ", width = *w as usize), + *w as usize, + self.header_style, + ); if t.to_string() == "ID" { - buf.set_stringn(x, y, format!("{symbol:>width$}", symbol=t, width=*w as usize), *w as usize, self.header_style); + buf.set_stringn( + x, + y, + format!("{symbol:>width$}", symbol = t, width = *w as usize), + *w as usize, + self.header_style, + ); header_index = index; } else { buf.set_stringn(x, y, format!("{}", t), *w as usize, self.header_style); @@ -335,9 +347,19 @@ where style, ); if c == header_index { - format!("{symbol}{elt:>width$}", symbol = symbol, elt = elt, width = *w as usize - symbol.to_string().graphemes(true).count()) + format!( + "{symbol}{elt:>width$}", + symbol = symbol, + elt = elt, + width = *w as usize - symbol.to_string().graphemes(true).count() + ) } else { - format!("{symbol}{elt: format!(""), }; format!("{} {}", task.description().to_string(), c) - }, + } "description.truncated_count" => { let c = match task.annotations() { Some(a) => format!(" [{}]", a.iter().count()), @@ -242,7 +242,7 @@ impl TaskReportTable { if self.description_width >= c.len() { end = self.description_width - c.len(); } - while end < d.len() && !d.is_char_boundary(end) { + while end < d.len() && !d.is_char_boundary(end) { end += 1; } d.truncate(end); @@ -250,7 +250,7 @@ impl TaskReportTable { d = format!("{}…", d); } format!("{}{}", d, c) - }, + } "description.truncated" => { let mut d = task.description().to_string(); let mut end = self.description_width; @@ -262,7 +262,7 @@ impl TaskReportTable { d = format!("{}…", d); } format!("{}", d) - }, + } "description" => task.description().to_string(), "urgency" => match &task.urgency() { Some(f) => format!("{:.2}", *f), @@ -279,7 +279,7 @@ impl TaskReportTable { UDAValue::F64(f) => f.to_string(), UDAValue::U64(u) => u.to_string(), } - }, + } } } } diff --git a/src/util.rs b/src/util.rs index a658069..6fbf1b9 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,8 +1,8 @@ use crossterm::{ + cursor, event::{self, DisableMouseCapture, EnableMouseCapture}, execute, - terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen, Clear, ClearType}, - cursor, + terminal::{disable_raw_mode, enable_raw_mode, Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen}, }; use tui::{backend::CrosstermBackend, Terminal}; @@ -129,7 +129,12 @@ impl Events { }) }; - Events { rx, tx, pause_stdin, pause_ticker } + Events { + rx, + tx, + pause_stdin, + pause_ticker, + } } /// Attempts to read an event.