view.rs 4.2 KB

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