Skip to content

Commit

Permalink
Merge pull request #301 from kdheepak/fix-project-pane-parsing
Browse files Browse the repository at this point in the history
Change parsing of task summary for project pane
  • Loading branch information
kdheepak authored Oct 30, 2021
2 parents 06eb6c2 + 161eb95 commit 1ff561b
Showing 1 changed file with 48 additions and 42 deletions.
90 changes: 48 additions & 42 deletions src/pane/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ pub struct ProjectsState {
pub rows: Vec<ProjectDetails>,
}

#[derive(Debug, Clone, Default)]
pub struct ProjectDetails {
name: Project,
remaining: usize,
avg_age: String,
complete: String,
}

impl ProjectsState {
pub(crate) fn new() -> Self {
Self {
Expand Down Expand Up @@ -118,50 +120,39 @@ impl ProjectsState {
.context("Unable to run `task summary`")
.unwrap();
let data = String::from_utf8_lossy(&output.stdout);
let contains_avg_age = data
.split('\n')
.into_iter()
.skip(1)
.collect::<Vec<&str>>()
.first()
.unwrap()
.contains("Avg age");
for line in data.split('\n').into_iter().skip(3) {
if line.is_empty() {
break;

let lines = data.split('\n').into_iter().skip(1).collect::<Vec<&str>>();

let header = lines.first().unwrap();

let contains_avg_age = header.contains("Avg age");

if contains_avg_age {
let name_index = header.find("Remaining").unwrap();
let remaining_index = header.find("Remaining").unwrap() + "Remaining".len();
let average_age_index = header.find("Avg age").unwrap() + "Avg age".len();
let complete_index = header.find("Complete").unwrap() + "Complete".len();

for line in lines.into_iter().skip(2) {
if line.is_empty() {
break;
}

let line = line.to_string();
let name = line[0..name_index].trim().to_string();
let remaining = line[name_index..remaining_index].trim().parse().unwrap();
let avg_age = line[remaining_index..average_age_index].trim().to_string();
let complete = line[average_age_index..complete_index].trim().to_string();

self.rows.push(ProjectDetails {
name,
remaining,
avg_age,
complete,
});
}
let mut row: Vec<String> = line
.split(' ')
.map(str::trim)
.map(str::trim_start)
.filter(|x| !x.is_empty())
.filter(|x| !x.chars().all(|c| c == '='))
.map(ToString::to_string)
.collect();
assert!(row.len() >= 3);
let (complete, avg_age, remaining, name) = if contains_avg_age {
(
row.pop().unwrap().parse().unwrap(),
row.pop().unwrap().parse().unwrap(),
row.pop().unwrap().parse().unwrap(),
row.join(" "),
)
} else {
(
row.pop().unwrap().parse().unwrap(),
"0s".to_string(),
row.pop().unwrap().parse().unwrap(),
row.join(" "),
)
};

self.rows.push(ProjectDetails {
name,
remaining,
avg_age,
complete,
});
}

self.list = self.rows.iter().map(|x| x.name.clone()).collect_vec();
Ok(())
}
Expand Down Expand Up @@ -229,3 +220,18 @@ fn update_task_filter_by_selection(app: &mut TaskwarriorTui) -> Result<()> {
app.update(true)?;
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_project_summary() {
let mut app = TaskwarriorTui::new("next").unwrap();

app.update(true).unwrap();

dbg!(&app.projects.rows);
dbg!(&app.projects.list);
}
}

0 comments on commit 1ff561b

Please sign in to comment.