view.rs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. use prettytable::{
  2. cell,
  3. format::{consts::FORMAT_NO_COLSEP, FormatBuilder, LinePosition, LineSeparator},
  4. row, table, Cell, Row, Table,
  5. };
  6. use darkfi::{util::time::timestamp_to_date, Result};
  7. use crate::{
  8. filter::apply_filter,
  9. primitives::{Comment, TaskInfo},
  10. TaskEvent,
  11. };
  12. pub fn print_task_list(tasks: Vec<TaskInfo>, filters: Vec<String>) -> Result<()> {
  13. let mut tasks = tasks;
  14. let mut table = Table::new();
  15. table.set_format(
  16. FormatBuilder::new()
  17. .padding(1, 1)
  18. .separators(&[LinePosition::Title], LineSeparator::new('-', ' ', ' ', ' '))
  19. .build(),
  20. );
  21. table.set_titles(row!["ID", "Title", "Project", "Assigned", "Due", "Rank"]);
  22. for filter in filters {
  23. apply_filter(&mut tasks, &filter);
  24. }
  25. tasks.sort_by(|a, b| b.rank.partial_cmp(&a.rank).unwrap());
  26. let mut min_rank = 0.0;
  27. let mut max_rank = 0.0;
  28. if let Some(first) = tasks.first() {
  29. max_rank = first.rank;
  30. }
  31. if let Some(last) = tasks.last() {
  32. min_rank = last.rank;
  33. }
  34. for task in tasks {
  35. let state = task.events.last().unwrap_or(&TaskEvent::default()).action.clone();
  36. let (max_style, min_style, mid_style, gen_style) = if state == "open" {
  37. ("bFC", "Fb", "Fc", "")
  38. } else {
  39. ("iFYBd", "iFYBd", "iFYBd", "iFYBd")
  40. };
  41. let rank = task.rank.to_string();
  42. table.add_row(Row::new(vec![
  43. Cell::new(&task.id.to_string()).style_spec(gen_style),
  44. Cell::new(&task.title).style_spec(gen_style),
  45. Cell::new(&task.project.join(", ")).style_spec(gen_style),
  46. Cell::new(&task.assign.join(", ")).style_spec(gen_style),
  47. Cell::new(&timestamp_to_date(task.due.unwrap_or(0), "date")).style_spec(gen_style),
  48. if task.rank == max_rank {
  49. Cell::new(&rank).style_spec(max_style)
  50. } else if task.rank == min_rank {
  51. Cell::new(&rank).style_spec(min_style)
  52. } else {
  53. Cell::new(&rank).style_spec(mid_style)
  54. },
  55. ]));
  56. }
  57. table.printstd();
  58. Ok(())
  59. }
  60. pub fn print_task_info(taskinfo: TaskInfo) -> Result<()> {
  61. let current_state = &taskinfo.events.last().unwrap_or(&TaskEvent::default()).action.clone();
  62. let due = timestamp_to_date(taskinfo.due.unwrap_or(0), "date");
  63. let created_at = timestamp_to_date(taskinfo.created_at, "datetime");
  64. let mut table = table!(
  65. [Bd => "ref_id", &taskinfo.ref_id],
  66. ["id", &taskinfo.id.to_string()],
  67. [Bd => "owner", &taskinfo.owner],
  68. ["title", &taskinfo.title],
  69. [Bd => "desc", &taskinfo.desc.to_string()],
  70. ["assign", taskinfo.assign.join(", ")],
  71. [Bd => "project", taskinfo.project.join(", ")],
  72. ["due", due],
  73. [Bd => "rank", &taskinfo.rank.to_string()],
  74. ["created_at", created_at],
  75. [Bd => "current_state", current_state]);
  76. table.set_format(
  77. FormatBuilder::new()
  78. .padding(1, 1)
  79. .separators(&[LinePosition::Title], LineSeparator::new('-', ' ', ' ', ' '))
  80. .build(),
  81. );
  82. table.set_titles(row!["Name", "Value"]);
  83. table.printstd();
  84. let mut event_table = table!(["events", &events_as_string(taskinfo.events)]);
  85. event_table.set_format(*FORMAT_NO_COLSEP);
  86. event_table.printstd();
  87. Ok(())
  88. }
  89. pub fn comments_as_string(comments: Vec<Comment>) -> String {
  90. let mut comments_str = String::new();
  91. for comment in comments {
  92. comments_str.push_str(&format!("{}\n", comment));
  93. }
  94. comments_str.pop();
  95. comments_str
  96. }
  97. pub fn events_as_string(events: Vec<TaskEvent>) -> String {
  98. let mut events_str = String::new();
  99. for event in events {
  100. events_str.push_str(&format!("State changed to {} at {}\n", event.action, event.timestamp));
  101. }
  102. events_str
  103. }