Use get_bool trait for String and str

This commit is contained in:
Dheepak Krishnamurthy 2020-10-22 21:49:20 -06:00
parent a6a437ec77
commit 8071558dbe
3 changed files with 111 additions and 62 deletions

View file

@ -1,5 +1,5 @@
use crate::config::TConfig;
use crate::calendar::Calendar;
use crate::config::TConfig;
use crate::table::{Row, Table, TableState};
use std::cmp::Ordering;
@ -14,7 +14,7 @@ use task_hookrs::task::Task;
use task_hookrs::uda::UDAValue;
use uuid::Uuid;
use chrono::{Datelike, Local, NaiveDateTime, NaiveDate, TimeZone};
use chrono::{Datelike, Local, NaiveDate, NaiveDateTime, TimeZone};
use std::sync::{Arc, Mutex};
use std::{sync::mpsc, thread, time::Duration};
@ -434,7 +434,6 @@ impl TTApp {
}
pub fn draw_calendar(&mut self, f: &mut Frame<impl Backend>) {
let dates_with_styles = self.get_dates_with_styles();
let rects = Layout::default()
.direction(Direction::Vertical)
@ -450,7 +449,6 @@ impl TTApp {
}
pub fn get_dates_with_styles(&self) -> Vec<(NaiveDate, Style)> {
let mut tasks_with_styles = vec![];
let tasks_is_empty = self.tasks.lock().unwrap().is_empty();
@ -460,12 +458,8 @@ impl TTApp {
let tasks = &self.tasks.lock().unwrap();
let tasks_with_due_dates = tasks.iter().filter(|t| t.due().is_some());
tasks_with_styles.extend(
tasks_with_due_dates.map(
|t| (t.due().unwrap().clone().date(), self.style_for_task(t))
)
)
tasks_with_styles
.extend(tasks_with_due_dates.map(|t| (t.due().unwrap().clone().date(), self.style_for_task(t))))
}
return tasks_with_styles;
}
@ -1048,7 +1042,7 @@ impl TTApp {
task_id
)),
}
},
}
None => Err(format!(
"Unable to run `task modify` with `{}` on task {}",
self.modify.as_str(),
@ -1297,7 +1291,12 @@ impl TTApp {
add_tag(&mut task, "ANNOTATED".to_string());
}
if task.tags().is_some() {
let tags = task.tags().unwrap().iter().filter(|s| !self.task_report_table.virtual_tags.contains(s)).collect::<Vec<_>>();
let tags = task
.tags()
.unwrap()
.iter()
.filter(|s| !self.task_report_table.virtual_tags.contains(s))
.collect::<Vec<_>>();
if !tags.is_empty() {
add_tag(&mut task, "TAGGED".to_string());
}
@ -1401,7 +1400,7 @@ impl TTApp {
Some(t) => {
let s = format!("{} ", t.description());
self.modify.update(&s, s.len())
},
}
None => self.modify.update("", 0),
}
}

View file

@ -5,7 +5,7 @@ use std::fmt;
const COL_WIDTH: usize = 21;
use chrono::{Datelike, Local, Duration, NaiveDate, NaiveDateTime, TimeZone, Month};
use chrono::{Datelike, Duration, Local, Month, NaiveDate, NaiveDateTime, TimeZone};
use tui::{
buffer::Buffer,
@ -25,7 +25,7 @@ pub struct Calendar<'a> {
pub month: u32,
pub style: Style,
pub months_per_row: usize,
pub date_style: Vec<(NaiveDate, Style)>
pub date_style: Vec<(NaiveDate, Style)>,
}
impl<'a> Default for Calendar<'a> {
@ -78,7 +78,7 @@ impl<'a> Calendar<'a> {
}
}
impl <'a> Widget for Calendar<'a> {
impl<'a> Widget for Calendar<'a> {
fn render(mut self, area: Rect, buf: &mut Buffer) {
let month_names = [
Month::January.name(),
@ -106,7 +106,7 @@ impl <'a> Widget for Calendar<'a> {
};
if area.height < 7 {
return
return;
}
let style = self.style;
@ -117,21 +117,41 @@ impl <'a> Widget for Calendar<'a> {
let months: Vec<_> = (0..12).collect();
let mut days: Vec<_> = months.iter().map(|i| {
let first = NaiveDate::from_ymd(year, i+1, 1);
(first, first - Duration::days(first.weekday().num_days_from_sunday() as i64))
}).collect();
let mut days: Vec<_> = months
.iter()
.map(|i| {
let first = NaiveDate::from_ymd(year, i + 1, 1);
(
first,
first - Duration::days(first.weekday().num_days_from_sunday() as i64),
)
})
.collect();
days.append(&mut months.iter().map(|i| {
let first = NaiveDate::from_ymd(year + 1, i+1, 1);
(first, first - Duration::days(first.weekday().num_days_from_sunday() as i64))
}).collect::<Vec<_>>()
days.append(
&mut months
.iter()
.map(|i| {
let first = NaiveDate::from_ymd(year + 1, i + 1, 1);
(
first,
first - Duration::days(first.weekday().num_days_from_sunday() as i64),
)
})
.collect::<Vec<_>>(),
);
days.append(&mut months.iter().map(|i| {
let first = NaiveDate::from_ymd(year + 2, i+1, 1);
(first, first - Duration::days(first.weekday().num_days_from_sunday() as i64))
}).collect::<Vec<_>>()
days.append(
&mut months
.iter()
.map(|i| {
let first = NaiveDate::from_ymd(year + 2, i + 1, 1);
(
first,
first - Duration::days(first.weekday().num_days_from_sunday() as i64),
)
})
.collect::<Vec<_>>(),
);
let mut startm = 0 as usize;
@ -179,9 +199,19 @@ impl <'a> Widget for Calendar<'a> {
let m = d.0.month() as usize;
let style = Style::default().bg(Color::Rgb(220, 220, 220));
if m == today.month() as usize && self.year + year as i32 == today.year() {
buf.set_string(x as u16, y, "Su Mo Tu We Th Fr Sa", style.add_modifier(Modifier::UNDERLINED));
buf.set_string(
x as u16,
y,
"Su Mo Tu We Th Fr Sa",
style.add_modifier(Modifier::UNDERLINED),
);
} else {
buf.set_string(x as u16, y, "Su Mo Tu We Th Fr Sa", style.add_modifier(Modifier::UNDERLINED));
buf.set_string(
x as u16,
y,
"Su Mo Tu We Th Fr Sa",
style.add_modifier(Modifier::UNDERLINED),
);
}
x += 21 + 1;
}
@ -224,12 +254,16 @@ impl <'a> Widget for Calendar<'a> {
startm += self.months_per_row;
y += 2;
if y + 8 > area.height {
break
break;
} else if startm >= 12 {
startm = 0;
year += 1;
let x = area.x;
let s = format!("{year:^width$}", year = self.year as usize + year, width = area.width as usize);
let s = format!(
"{year:^width$}",
year = self.year as usize + year,
width = area.width as usize
);
let mut style = Style::default().add_modifier(Modifier::UNDERLINED);
if self.year + year as i32 == today.year() {
style = style.add_modifier(Modifier::BOLD)

View file

@ -41,6 +41,34 @@ pub struct TConfig {
pub uda_calendar_months_per_row: usize,
}
trait TaskWarriorBool {
fn get_bool(&self) -> Option<bool>;
}
impl TaskWarriorBool for String {
fn get_bool(&self) -> Option<bool> {
if self == "true" || self == "1" || self == "y" || self == "yes" || self == "on" {
Some(true)
} else if self == "false" || self == "0" || self == "n" || self == "no" || self == "off" {
Some(false)
} else {
None
}
}
}
impl TaskWarriorBool for str {
fn get_bool(&self) -> Option<bool> {
if self == "true" || self == "1" || self == "y" || self == "yes" || self == "on" {
Some(true)
} else if self == "false" || self == "0" || self == "n" || self == "no" || self == "off" {
Some(false)
} else {
None
}
}
}
impl TConfig {
pub fn default() -> Self {
let bool_collection = Self::get_bool_collection();
@ -265,34 +293,33 @@ impl TConfig {
fn get_uda_selection_bold() -> bool {
let s = Self::get_config("uda.taskwarrior-tui.selection.bold");
if s == "yes" {
true
} else if s == "no" {
false
} else {
true
match s.get_bool() {
Some(b) => b,
None => true,
}
}
fn get_uda_selection_italic() -> bool {
let s = Self::get_config("uda.taskwarrior-tui.selection.italic");
if s == "yes" {
true
} else if s == "no" {
false
} else {
false
match s.get_bool() {
Some(b) => b,
None => false,
}
}
fn get_uda_selection_dim() -> bool {
let s = Self::get_config("uda.taskwarrior-tui.selection.dim");
if s == "yes" {
true
} else if s == "no" {
false
} else {
false
match s.get_bool() {
Some(b) => b,
None => false,
}
}
fn get_uda_selection_blink() -> bool {
let s = Self::get_config("uda.taskwarrior-tui.selection.blink");
match s.get_bool() {
Some(b) => b,
None => false,
}
}
@ -303,17 +330,6 @@ impl TConfig {
Err(e) => 4,
}
}
fn get_uda_selection_blink() -> bool {
let s = Self::get_config("uda.taskwarrior-tui.selection.blink");
if s == "yes" {
true
} else if s == "no" {
false
} else {
false
}
}
}
#[cfg(test)]