feat: Support taskwarrior v2.6.x context

This commit is contained in:
Dheepak Krishnamurthy 2021-10-27 16:03:45 -06:00
parent 2738f417a2
commit dce55ebf89
2 changed files with 36 additions and 12 deletions

View file

@ -1215,6 +1215,7 @@ impl TaskwarriorTui {
let contexts = self
.contexts
.iter()
.filter(|c| &c.typ == "read")
.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()];
@ -1245,6 +1246,7 @@ impl TaskwarriorTui {
task::block_on(self.update_task_details())?;
}
self.selection_fix();
Ok(())
}
@ -1528,27 +1530,46 @@ impl TaskwarriorTui {
self.contexts = vec![];
for (i, line) in data.trim().split('\n').enumerate() {
if line.starts_with(" ") && line.trim().starts_with("write") {
continue;
}
let line = line.trim();
if line.is_empty() || line == "Use 'task context none' to unset the current context." {
continue;
}
let mut s = line.split(' ');
let name = s.next().unwrap_or_default();
let active = s.last().unwrap_or_default();
let definition = line.replacen(name, "", 1);
let definition = definition.strip_suffix(active).unwrap_or_default();
if i == 0 || i == 1 {
continue;
}
let context = Context::new(name.to_string(), definition.trim().to_string(), active.to_string());
let mut s = line.split_whitespace();
let name = s.next().unwrap_or_default();
let typ = s.next().unwrap_or_default();
let active = s.last().unwrap_or_default();
let definition = line.replacen(name, "", 1);
let definition = definition.replacen(typ, "", 1);
let definition = definition.strip_suffix(active).unwrap_or_default();
let context = Context::new(
name.to_string(),
definition.trim().to_string(),
active.to_string(),
typ.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(), "read".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(),
"read".to_string(),
),
);
}
Ok(())
@ -4226,13 +4247,14 @@ mod tests {
test_case(&expected);
}
// #[test]
fn test_draw_context_menu() {
let test_case = |expected: &Buffer| {
let mut app = TaskwarriorTui::new("next").unwrap();
app.mode = Mode::Tasks(Action::ContextMenu);
app.task_report_next();
app.context_next();
// app.context_next();
app.update(true).unwrap();
let backend = TestBackend::new(80, 10);

View file

@ -13,14 +13,16 @@ pub struct Context {
pub name: String,
pub description: String,
pub active: String,
pub typ: String,
}
impl Context {
pub fn new(name: String, description: String, active: String) -> Self {
pub fn new(name: String, description: String, active: String, typ: String) -> Self {
Self {
name,
description,
active,
typ,
}
}
}