Change color.rs to config.rs

This commit is contained in:
Dheepak Krishnamurthy 2020-10-10 21:54:34 -06:00
parent a8a053e989
commit 028509004f
4 changed files with 338 additions and 404 deletions

View file

@ -1,4 +1,4 @@
use crate::color::TColorConfig; use crate::config::TConfig;
use std::cmp::Ordering; use std::cmp::Ordering;
use std::convert::TryInto; use std::convert::TryInto;
@ -364,7 +364,7 @@ pub struct TTApp {
pub tasks: Arc<Mutex<Vec<Task>>>, pub tasks: Arc<Mutex<Vec<Task>>>,
pub task_report_table: TaskReportTable, pub task_report_table: TaskReportTable,
pub mode: AppMode, pub mode: AppMode,
pub colors: TColorConfig, pub config: TConfig,
} }
impl TTApp { impl TTApp {
@ -380,7 +380,7 @@ impl TTApp {
modify: LineBuffer::with_capacity(MAX_LINE), modify: LineBuffer::with_capacity(MAX_LINE),
error: "".to_string(), error: "".to_string(),
mode: AppMode::TaskReport, mode: AppMode::TaskReport,
colors: TColorConfig::default(), config: TConfig::default(),
task_report_table: TaskReportTable::new(), task_report_table: TaskReportTable::new(),
}; };
for c in "status:pending ".chars() { for c in "status:pending ".chars() {
@ -745,22 +745,26 @@ impl TTApp {
fn style_for_task(&self, task: &Task) -> Style { fn style_for_task(&self, task: &Task) -> Style {
let mut style = Style::default(); let mut style = Style::default();
if task.tags().unwrap_or(&vec![]).contains(&"ACTIVE".to_string()) { if task.tags().unwrap_or(&vec![]).contains(&"ACTIVE".to_string()) {
style = style.fg(self.colors.active.fg).bg(self.colors.active.bg); style = style.fg(self.config.color_active.fg).bg(self.config.color_active.bg);
} }
if task.tags().unwrap_or(&vec![]).contains(&"BLOCKING".to_string()) { if task.tags().unwrap_or(&vec![]).contains(&"BLOCKING".to_string()) {
style = style.fg(self.colors.blocking.fg).bg(self.colors.blocking.bg); style = style
.fg(self.config.color_blocking.fg)
.bg(self.config.color_blocking.bg);
} }
if task.tags().unwrap_or(&vec![]).contains(&"BLOCKED".to_string()) { if task.tags().unwrap_or(&vec![]).contains(&"BLOCKED".to_string()) {
style = style.fg(self.colors.blocked.fg).bg(self.colors.blocked.bg); style = style.fg(self.config.color_blocked.fg).bg(self.config.color_blocked.bg);
} }
if task.tags().unwrap_or(&vec![]).contains(&"DUE".to_string()) { if task.tags().unwrap_or(&vec![]).contains(&"DUE".to_string()) {
style = style.fg(self.colors.due.fg).bg(self.colors.due.bg); style = style.fg(self.config.color_due.fg).bg(self.config.color_due.bg);
} }
if task.tags().unwrap_or(&vec![]).contains(&"OVERDUE".to_string()) { if task.tags().unwrap_or(&vec![]).contains(&"OVERDUE".to_string()) {
style = style.fg(self.colors.overdue.fg).bg(self.colors.overdue.bg); style = style.fg(self.config.color_overdue.fg).bg(self.config.color_overdue.bg);
} }
if task.tags().unwrap_or(&vec![]).contains(&"TODAY".to_string()) { if task.tags().unwrap_or(&vec![]).contains(&"TODAY".to_string()) {
style = style.fg(self.colors.due_today.fg).bg(self.colors.due_today.bg); style = style
.fg(self.config.color_due_today.fg)
.bg(self.config.color_due_today.bg);
} }
return style; return style;
} }

View file

@ -1,394 +0,0 @@
use std::collections::HashMap;
use std::process::Command;
use std::str;
use tui::style::Color;
#[derive(Debug, Clone, Copy)]
pub struct TColor {
pub fg: Color,
pub bg: Color,
}
impl TColor {
pub fn default() -> Self {
TColor {
fg: Color::Indexed(0),
bg: Color::Indexed(15),
}
}
}
#[derive(Debug)]
pub struct TColorConfig {
pub enabled: bool,
pub active: TColor,
pub alternate: TColor,
pub blocked: TColor,
pub blocking: TColor,
pub burndown_done: TColor,
pub burndown_pending: TColor,
pub burndown_started: TColor,
pub calendar_due: TColor,
pub calendar_due_today: TColor,
pub calendar_holiday: TColor,
pub calendar_overdue: TColor,
pub calendar_today: TColor,
pub calendar_weekend: TColor,
pub calendar_weeknumber: TColor,
pub completed: TColor,
pub debug: TColor,
pub deleted: TColor,
pub due: TColor,
pub due_today: TColor,
pub error: TColor,
pub footnote: TColor,
pub header: TColor,
pub history_add: TColor,
pub history_delete: TColor,
pub history_done: TColor,
pub label: TColor,
pub label_sort: TColor,
pub overdue: TColor,
pub project: TColor,
pub recurring: TColor,
pub scheduled: TColor,
pub summary_background: TColor,
pub summary_bar: TColor,
pub sync_added: TColor,
pub sync_changed: TColor,
pub sync_rejected: TColor,
pub tag_next: TColor,
pub tag: TColor,
pub tagged: TColor,
pub uda_priority: TColor,
pub uda_priority_h: TColor,
pub uda_priority_l: TColor,
pub uda_priority_m: TColor,
pub undo_after: TColor,
pub undo_before: TColor,
pub until: TColor,
pub warning: TColor,
}
pub fn get_color(s: &str) -> Color {
if s.starts_with("color") {
let fg = (s.as_bytes()[5] as char).to_digit(10).unwrap() as u8;
Color::Indexed(fg)
} else if s.starts_with("rgb") {
let red = (s.as_bytes()[3] as char).to_digit(10).unwrap() as u8;
let green = (s.as_bytes()[4] as char).to_digit(10).unwrap() as u8;
let blue = (s.as_bytes()[5] as char).to_digit(10).unwrap() as u8;
Color::Indexed(16 + red * 36 + green * 6 + blue)
} else {
if s == "white" {
Color::White
} else if s == "black" {
Color::Black
} else {
Color::Indexed(15)
}
}
}
pub fn get_tcolor(line: &str) -> TColor {
if line.contains(" on ") {
let foreground = line.split(" ").collect::<Vec<&str>>()[0];
let background = line.split(" ").collect::<Vec<&str>>()[2];
TColor {
fg: get_color(foreground),
bg: get_color(background),
}
} else if line.contains("on ") {
let background = line.split(" ").collect::<Vec<&str>>()[1];
TColor {
fg: Color::Indexed(0),
bg: get_color(background),
}
} else {
let foreground = line;
TColor {
fg: get_color(foreground),
bg: Color::Indexed(15),
}
}
}
impl TColorConfig {
pub fn default() -> Self {
let output = Command::new("task")
.arg("rc.color=off")
.arg("show")
.output()
.expect("Unable to run `task show`");
let data = String::from_utf8(output.stdout).expect("Unable to convert stdout to string");
let enabled = true;
let attributes = vec![
"active",
"alternate",
"blocked",
"blocking",
"burndown.done",
"burndown.pending",
"burndown.started",
"calendar.due",
"calendar.due.today",
"calendar.holiday",
"calendar.overdue",
"calendar.today",
"calendar.weekend",
"calendar.weeknumber",
"completed",
"debug",
"deleted",
"due",
"due.today",
"error",
"footnote",
"header",
"header.add",
"history.delete",
"history.done",
"label",
"label.sort",
"overdue",
"project.none",
"recurring",
"scheduled",
"summary.background",
"summary.bar",
"sync.added",
"sync.changed",
"sync.rejected",
"tag.next",
"tag.none",
"tagged",
"uda.priority",
"uda.priority.H",
"uda.priority.L",
"uda.priority.M",
"undo.after",
"undo.before",
"undo.until",
"until",
"warning",
];
let mut color_collection = HashMap::new();
for line in data.split('\n') {
for attribute in &attributes {
let attr = format!("color.{} ", attribute);
if line.starts_with(&attr) {
color_collection.insert(
attribute.to_string(),
get_tcolor(line.trim_start_matches(&attr).trim_start_matches(" ")),
);
}
}
}
Self {
enabled: true,
active: match color_collection.get("active") {
Some(c) => *c,
None => TColor::default(),
},
alternate: match color_collection.get("alternate") {
Some(c) => *c,
None => TColor::default(),
},
blocked: match color_collection.get("blocked") {
Some(c) => *c,
None => TColor::default(),
},
blocking: match color_collection.get("blocking") {
Some(c) => *c,
None => TColor::default(),
},
burndown_done: match color_collection.get("burndown.done") {
Some(c) => *c,
None => TColor::default(),
},
burndown_pending: match color_collection.get("burndown.pending") {
Some(c) => *c,
None => TColor::default(),
},
burndown_started: match color_collection.get("burndown.started") {
Some(c) => *c,
None => TColor::default(),
},
calendar_due: match color_collection.get("calendar.due") {
Some(c) => *c,
None => TColor::default(),
},
calendar_due_today: match color_collection.get("calendar.due.today") {
Some(c) => *c,
None => TColor::default(),
},
calendar_holiday: match color_collection.get("calendar.holiday") {
Some(c) => *c,
None => TColor::default(),
},
calendar_overdue: match color_collection.get("calendar.overdue") {
Some(c) => *c,
None => TColor::default(),
},
calendar_today: match color_collection.get("calendar.today") {
Some(c) => *c,
None => TColor::default(),
},
calendar_weekend: match color_collection.get("calendar.weekend") {
Some(c) => *c,
None => TColor::default(),
},
calendar_weeknumber: match color_collection.get("calendar.weeknumber") {
Some(c) => *c,
None => TColor::default(),
},
completed: match color_collection.get("completed") {
Some(c) => *c,
None => TColor::default(),
},
debug: match color_collection.get("debug") {
Some(c) => *c,
None => TColor::default(),
},
deleted: match color_collection.get("deleted") {
Some(c) => *c,
None => TColor::default(),
},
due: match color_collection.get("due") {
Some(c) => *c,
None => TColor::default(),
},
due_today: match color_collection.get("due.today") {
Some(c) => *c,
None => TColor::default(),
},
error: match color_collection.get("error") {
Some(c) => *c,
None => TColor::default(),
},
footnote: match color_collection.get("footnote") {
Some(c) => *c,
None => TColor::default(),
},
header: match color_collection.get("header") {
Some(c) => *c,
None => TColor::default(),
},
history_add: match color_collection.get("history.add") {
Some(c) => *c,
None => TColor::default(),
},
history_delete: match color_collection.get("history.delete") {
Some(c) => *c,
None => TColor::default(),
},
history_done: match color_collection.get("history.done") {
Some(c) => *c,
None => TColor::default(),
},
label: match color_collection.get("label") {
Some(c) => *c,
None => TColor::default(),
},
label_sort: match color_collection.get("label.sort") {
Some(c) => *c,
None => TColor::default(),
},
overdue: match color_collection.get("overdue") {
Some(c) => *c,
None => TColor::default(),
},
project: match color_collection.get("project") {
Some(c) => *c,
None => TColor::default(),
},
recurring: match color_collection.get("recurring") {
Some(c) => *c,
None => TColor::default(),
},
scheduled: match color_collection.get("scheduled") {
Some(c) => *c,
None => TColor::default(),
},
summary_background: match color_collection.get("summary.background") {
Some(c) => *c,
None => TColor::default(),
},
summary_bar: match color_collection.get("summary_bar") {
Some(c) => *c,
None => TColor::default(),
},
sync_added: match color_collection.get("sync.added") {
Some(c) => *c,
None => TColor::default(),
},
sync_changed: match color_collection.get("sync.changed") {
Some(c) => *c,
None => TColor::default(),
},
sync_rejected: match color_collection.get("sync.rejected") {
Some(c) => *c,
None => TColor::default(),
},
tag_next: match color_collection.get("tag.next") {
Some(c) => *c,
None => TColor::default(),
},
tag: match color_collection.get("tag") {
Some(c) => *c,
None => TColor::default(),
},
tagged: match color_collection.get("tagged") {
Some(c) => *c,
None => TColor::default(),
},
uda_priority: match color_collection.get("uda.priority") {
Some(c) => *c,
None => TColor::default(),
},
uda_priority_h: match color_collection.get("uda.priority.h") {
Some(c) => *c,
None => TColor::default(),
},
uda_priority_l: match color_collection.get("uda.priority.l") {
Some(c) => *c,
None => TColor::default(),
},
uda_priority_m: match color_collection.get("uda.priority.m") {
Some(c) => *c,
None => TColor::default(),
},
undo_after: match color_collection.get("undo.after") {
Some(c) => *c,
None => TColor::default(),
},
undo_before: match color_collection.get("undo.before") {
Some(c) => *c,
None => TColor::default(),
},
until: match color_collection.get("until") {
Some(c) => *c,
None => TColor::default(),
},
warning: match color_collection.get("warning") {
Some(c) => *c,
None => TColor::default(),
},
}
}
}
#[cfg(test)]
mod tests {
use crate::color::TColorConfig;
#[test]
fn test_colors() {
let tc = TColorConfig::default();
println!("{:?}", tc.due_today);
}
}

324
src/config.rs Normal file
View file

@ -0,0 +1,324 @@
use std::collections::HashMap;
use std::process::Command;
use std::str;
use tui::style::Color;
#[derive(Debug, Clone, Copy)]
pub struct TColor {
pub fg: Color,
pub bg: Color,
}
impl TColor {
pub fn default() -> Self {
TColor {
fg: Color::Indexed(0),
bg: Color::Indexed(15),
}
}
}
#[derive(Debug)]
pub struct TConfig {
pub enabled: bool,
pub color_active: TColor,
pub color_alternate: TColor,
pub color_blocked: TColor,
pub color_blocking: TColor,
pub color_burndown_done: TColor,
pub color_burndown_pending: TColor,
pub color_burndown_started: TColor,
pub color_calendar_due: TColor,
pub color_calendar_due_today: TColor,
pub color_calendar_holiday: TColor,
pub color_calendar_overdue: TColor,
pub color_calendar_today: TColor,
pub color_calendar_weekend: TColor,
pub color_calendar_weeknumber: TColor,
pub color_completed: TColor,
pub color_debug: TColor,
pub color_deleted: TColor,
pub color_due: TColor,
pub color_due_today: TColor,
pub color_error: TColor,
pub color_footnote: TColor,
pub color_header: TColor,
pub color_history_add: TColor,
pub color_history_delete: TColor,
pub color_history_done: TColor,
pub color_label: TColor,
pub color_label_sort: TColor,
pub color_overdue: TColor,
pub color_project: TColor,
pub color_recurring: TColor,
pub color_scheduled: TColor,
pub color_summary_background: TColor,
pub color_summary_bar: TColor,
pub color_sync_added: TColor,
pub color_sync_changed: TColor,
pub color_sync_rejected: TColor,
pub color_tag_next: TColor,
pub color_tag: TColor,
pub color_tagged: TColor,
pub color_uda_priority: TColor,
pub color_uda_priority_h: TColor,
pub color_uda_priority_l: TColor,
pub color_uda_priority_m: TColor,
pub color_undo_after: TColor,
pub color_undo_before: TColor,
pub color_until: TColor,
pub color_warning: TColor,
pub obfuscate: bool,
pub print_empty_columns: bool,
}
pub fn get_color(s: &str) -> Color {
if s.starts_with("color") {
let fg = (s.as_bytes()[5] as char).to_digit(10).unwrap() as u8;
Color::Indexed(fg)
} else if s.starts_with("rgb") {
let red = (s.as_bytes()[3] as char).to_digit(10).unwrap() as u8;
let green = (s.as_bytes()[4] as char).to_digit(10).unwrap() as u8;
let blue = (s.as_bytes()[5] as char).to_digit(10).unwrap() as u8;
Color::Indexed(16 + red * 36 + green * 6 + blue)
} else {
if s == "white" {
Color::White
} else if s == "black" {
Color::Black
} else {
Color::Indexed(15)
}
}
}
pub fn get_tcolor(line: &str) -> TColor {
if line.contains(" on ") {
let foreground = line.split(" ").collect::<Vec<&str>>()[0];
let background = line.split(" ").collect::<Vec<&str>>()[2];
TColor {
fg: get_color(foreground),
bg: get_color(background),
}
} else if line.contains("on ") {
let background = line.split(" ").collect::<Vec<&str>>()[1];
TColor {
fg: Color::Indexed(0),
bg: get_color(background),
}
} else {
let foreground = line;
TColor {
fg: get_color(foreground),
bg: Color::Indexed(15),
}
}
}
impl TConfig {
pub fn default() -> Self {
let output = Command::new("task")
.arg("rc.color=off")
.arg("show")
.output()
.expect("Unable to run `task show`");
let data = String::from_utf8(output.stdout).expect("Unable to convert stdout to string");
let enabled = true;
let attributes = vec![
"active",
"color.alternate",
"color.blocked",
"color.blocking",
"color.burndown.done",
"color.burndown.pending",
"color.burndown.started",
"color.calendar.due",
"color.calendar.due.today",
"color.calendar.holiday",
"color.calendar.overdue",
"color.calendar.today",
"color.calendar.weekend",
"color.calendar.weeknumber",
"color.completed",
"color.debug",
"color.deleted",
"color.due",
"color.due.today",
"color.error",
"color.footnote",
"color.header",
"color.header.add",
"color.history.delete",
"color.history.done",
"color.label",
"color.label.sort",
"color.overdue",
"color.project.none",
"color.recurring",
"color.scheduled",
"color.summary.background",
"color.summary.bar",
"color.sync.added",
"color.sync.changed",
"color.sync.rejected",
"color.tag.next",
"color.tag.none",
"color.tagged",
"color.uda.priority",
"color.uda.priority.H",
"color.uda.priority.L",
"color.uda.priority.M",
"color.undo.after",
"color.undo.before",
"color.undo.until",
"color.until",
"color.warning",
// "obfuscate",
// "print.empty.columns",
];
let mut color_collection = HashMap::new();
for line in data.split('\n') {
for attribute in &attributes {
if line.starts_with(attribute) {
color_collection.insert(
attribute.to_string(),
get_tcolor(line.trim_start_matches(attribute).trim_start_matches(" ")),
);
}
}
}
Self {
enabled: true,
obfuscate: true,
print_empty_columns: true,
color_active: color_collection.get("active").cloned().unwrap_or(TColor::default()),
color_alternate: color_collection.get("alternate").cloned().unwrap_or(TColor::default()),
color_blocked: color_collection.get("blocked").cloned().unwrap_or(TColor::default()),
color_blocking: color_collection.get("blocking").cloned().unwrap_or(TColor::default()),
color_burndown_done: color_collection
.get("burndown.done")
.cloned()
.unwrap_or(TColor::default()),
color_burndown_pending: color_collection
.get("burndown.pending")
.cloned()
.unwrap_or(TColor::default()),
color_burndown_started: color_collection
.get("burndown.started")
.cloned()
.unwrap_or(TColor::default()),
color_calendar_due: color_collection
.get("calendar.due")
.cloned()
.unwrap_or(TColor::default()),
color_calendar_due_today: color_collection
.get("calendar.due.today")
.cloned()
.unwrap_or(TColor::default()),
color_calendar_holiday: color_collection
.get("calendar.holiday")
.cloned()
.unwrap_or(TColor::default()),
color_calendar_overdue: color_collection
.get("calendar.overdue")
.cloned()
.unwrap_or(TColor::default()),
color_calendar_today: color_collection
.get("calendar.today")
.cloned()
.unwrap_or(TColor::default()),
color_calendar_weekend: color_collection
.get("calendar.weekend")
.cloned()
.unwrap_or(TColor::default()),
color_calendar_weeknumber: color_collection
.get("calendar.weeknumber")
.cloned()
.unwrap_or(TColor::default()),
color_completed: color_collection.get("completed").cloned().unwrap_or(TColor::default()),
color_debug: color_collection.get("debug").cloned().unwrap_or(TColor::default()),
color_deleted: color_collection.get("deleted").cloned().unwrap_or(TColor::default()),
color_due: color_collection.get("due").cloned().unwrap_or(TColor::default()),
color_due_today: color_collection.get("due.today").cloned().unwrap_or(TColor::default()),
color_error: color_collection.get("error").cloned().unwrap_or(TColor::default()),
color_footnote: color_collection.get("footnote").cloned().unwrap_or(TColor::default()),
color_header: color_collection.get("header").cloned().unwrap_or(TColor::default()),
color_history_add: color_collection
.get("history.add")
.cloned()
.unwrap_or(TColor::default()),
color_history_delete: color_collection
.get("history.delete")
.cloned()
.unwrap_or(TColor::default()),
color_history_done: color_collection
.get("history.done")
.cloned()
.unwrap_or(TColor::default()),
color_label: color_collection.get("label").cloned().unwrap_or(TColor::default()),
color_label_sort: color_collection.get("label.sort").cloned().unwrap_or(TColor::default()),
color_overdue: color_collection.get("overdue").cloned().unwrap_or(TColor::default()),
color_project: color_collection.get("project").cloned().unwrap_or(TColor::default()),
color_recurring: color_collection.get("recurring").cloned().unwrap_or(TColor::default()),
color_scheduled: color_collection.get("scheduled").cloned().unwrap_or(TColor::default()),
color_summary_background: color_collection
.get("summary.background")
.cloned()
.unwrap_or(TColor::default()),
color_summary_bar: color_collection
.get("summary_bar")
.cloned()
.unwrap_or(TColor::default()),
color_sync_added: color_collection.get("sync.added").cloned().unwrap_or(TColor::default()),
color_sync_changed: color_collection
.get("sync.changed")
.cloned()
.unwrap_or(TColor::default()),
color_sync_rejected: color_collection
.get("sync.rejected")
.cloned()
.unwrap_or(TColor::default()),
color_tag_next: color_collection.get("tag.next").cloned().unwrap_or(TColor::default()),
color_tag: color_collection.get("tag").cloned().unwrap_or(TColor::default()),
color_tagged: color_collection.get("tagged").cloned().unwrap_or(TColor::default()),
color_uda_priority: color_collection
.get("uda.priority")
.cloned()
.unwrap_or(TColor::default()),
color_uda_priority_h: color_collection
.get("uda.priority.h")
.cloned()
.unwrap_or(TColor::default()),
color_uda_priority_l: color_collection
.get("uda.priority.l")
.cloned()
.unwrap_or(TColor::default()),
color_uda_priority_m: color_collection
.get("uda.priority.m")
.cloned()
.unwrap_or(TColor::default()),
color_undo_after: color_collection.get("undo.after").cloned().unwrap_or(TColor::default()),
color_undo_before: color_collection
.get("undo.before")
.cloned()
.unwrap_or(TColor::default()),
color_until: color_collection.get("until").cloned().unwrap_or(TColor::default()),
color_warning: color_collection.get("warning").cloned().unwrap_or(TColor::default()),
}
}
}
#[cfg(test)]
mod tests {
use crate::color::TColorConfig;
#[test]
fn test_colors() {
let tc = TColorConfig::default();
println!("{:?}", tc.due_today);
}
}

View file

@ -4,7 +4,7 @@
mod app; mod app;
mod calendar; mod calendar;
mod color; mod config;
mod util; mod util;
use crate::util::{destruct_terminal, setup_terminal, Event, EventConfig, Events}; use crate::util::{destruct_terminal, setup_terminal, Event, EventConfig, Events};