view.rs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::{cmp::Ordering, fmt::Write, str::FromStr};
  19. use prettytable::{
  20. format::{consts::FORMAT_NO_COLSEP, FormatBuilder, LinePosition, LineSeparator},
  21. row, table, Cell, Row, Table,
  22. };
  23. use textwrap::fill;
  24. use darkfi::{
  25. util::time::{timestamp_to_date, DateFormat},
  26. Result,
  27. };
  28. use crate::{
  29. primitives::{State, TaskInfo},
  30. TaskEvent,
  31. };
  32. pub fn print_task_list(tasks: Vec<TaskInfo>, ws: String) -> Result<()> {
  33. let mut tasks = tasks;
  34. let mut table = Table::new();
  35. table.set_format(
  36. FormatBuilder::new()
  37. .padding(1, 1)
  38. .separators(&[LinePosition::Title], LineSeparator::new('-', ' ', ' ', ' '))
  39. .build(),
  40. );
  41. table.set_titles(row!["ID", "Title", "Tags", "Project", "Assigned", "Due", "Rank"]);
  42. // group tasks by state.
  43. tasks.sort_by_key(|task| task.state.clone());
  44. // sort tasks by there rank only if they are not stopped.
  45. tasks.sort_by(|a, b| {
  46. if a.state != "stop" && b.state != "stop" {
  47. b.rank.partial_cmp(&a.rank).unwrap()
  48. } else {
  49. // because sort_by does not reorder equal elements
  50. Ordering::Equal
  51. }
  52. });
  53. let mut min_rank = None;
  54. let mut max_rank = None;
  55. if let Some(first) = tasks.first() {
  56. max_rank = first.rank;
  57. }
  58. if let Some(last) = tasks.last() {
  59. min_rank = last.rank;
  60. }
  61. for task in tasks {
  62. let state = State::from_str(&task.state.clone())?;
  63. let (max_style, min_style, mid_style, gen_style) = if state.is_start() {
  64. ("bFg", "Fc", "Fg", "Fg")
  65. } else if state.is_pause() {
  66. ("iFYBd", "iFYBd", "iFYBd", "iFYBd")
  67. } else if state.is_stop() {
  68. ("Fr", "Fr", "Fr", "Fr")
  69. } else {
  70. ("", "", "", "")
  71. };
  72. let rank = if let Some(r) = task.rank { r.to_string() } else { "".to_string() };
  73. let mut print_tags = vec![];
  74. for tag in &task.tags {
  75. let t = tag.replace('+', "");
  76. print_tags.push(t)
  77. }
  78. let due_ = match task.due {
  79. Some(ts) => ts.0,
  80. None => 0,
  81. };
  82. table.add_row(Row::new(vec![
  83. Cell::new(&task.id.to_string()).style_spec(gen_style),
  84. Cell::new(&task.title).style_spec(gen_style),
  85. Cell::new(&print_tags.join(", ")).style_spec(gen_style),
  86. Cell::new(&task.project.join(", ")).style_spec(gen_style),
  87. Cell::new(&task.assign.join(", ")).style_spec(gen_style),
  88. Cell::new(&timestamp_to_date(due_, DateFormat::Date)).style_spec(gen_style),
  89. if task.rank == max_rank {
  90. Cell::new(&rank).style_spec(max_style)
  91. } else if task.rank == min_rank {
  92. Cell::new(&rank).style_spec(min_style)
  93. } else {
  94. Cell::new(&rank).style_spec(mid_style)
  95. },
  96. ]));
  97. }
  98. let workspace = format!("Workspace: {}", ws);
  99. let mut ws_table = table!([workspace]);
  100. ws_table.set_format(
  101. FormatBuilder::new()
  102. .padding(1, 1)
  103. .separators(&[LinePosition::Bottom], LineSeparator::new('-', ' ', ' ', ' '))
  104. .build(),
  105. );
  106. if unsafe { libc::isatty(libc::STDOUT_FILENO) } == 1 {
  107. ws_table.printstd();
  108. table.printstd();
  109. } else {
  110. for row in table.row_iter() {
  111. for cell in row.iter() {
  112. print!("{}\t", cell.get_content());
  113. }
  114. println!();
  115. }
  116. }
  117. Ok(())
  118. }
  119. pub fn taskinfo_table(taskinfo: TaskInfo) -> Result<Table> {
  120. let due_ = match taskinfo.due {
  121. Some(ts) => ts.0,
  122. None => 0,
  123. };
  124. let due = timestamp_to_date(due_, DateFormat::Date);
  125. let created_at = timestamp_to_date(taskinfo.created_at.0, DateFormat::DateTime);
  126. let rank = if let Some(r) = taskinfo.rank { r.to_string() } else { "".to_string() };
  127. let mut table = table!(
  128. [Bd => "ref_id", &taskinfo.ref_id],
  129. ["workspace", &taskinfo.workspace],
  130. [Bd =>"id", &taskinfo.id.to_string()],
  131. ["owner", &taskinfo.owner],
  132. [Bd =>"title", &taskinfo.title],
  133. ["tags", &taskinfo.tags.join(", ")],
  134. [Bd =>"desc", &taskinfo.desc.to_string()],
  135. ["assign", taskinfo.assign.join(", ")],
  136. [Bd =>"project", taskinfo.project.join(", ")],
  137. ["due", due],
  138. [Bd =>"rank", rank],
  139. ["created_at", created_at],
  140. [Bd =>"current_state", &taskinfo.state]);
  141. table.set_format(
  142. FormatBuilder::new()
  143. .padding(1, 1)
  144. .separators(&[LinePosition::Title], LineSeparator::new('-', ' ', ' ', ' '))
  145. .build(),
  146. );
  147. table.set_titles(row!["Name", "Value"]);
  148. Ok(table)
  149. }
  150. pub fn events_table(taskinfo: TaskInfo) -> Result<Table> {
  151. let (events, timestamps) = &events_as_string(taskinfo.events);
  152. let mut events_table = table!([events, timestamps]);
  153. events_table.set_format(*FORMAT_NO_COLSEP);
  154. events_table.set_titles(row!["Events"]);
  155. Ok(events_table)
  156. }
  157. pub fn comments_table(taskinfo: TaskInfo) -> Result<Table> {
  158. let (events, timestamps) = &comments_as_string(taskinfo.events);
  159. let mut comments_table = table!([events, timestamps]);
  160. comments_table.set_format(*FORMAT_NO_COLSEP);
  161. comments_table.set_titles(row!["Comments"]);
  162. Ok(comments_table)
  163. }
  164. pub fn print_task_info(taskinfo: TaskInfo) -> Result<()> {
  165. let table = taskinfo_table(taskinfo.clone())?;
  166. table.printstd();
  167. let events_table = events_table(taskinfo.clone())?;
  168. events_table.printstd();
  169. let comments_table = comments_table(taskinfo)?;
  170. comments_table.printstd();
  171. println!();
  172. Ok(())
  173. }
  174. pub fn events_as_string(events: Vec<TaskEvent>) -> (String, String) {
  175. let mut events_str = String::new();
  176. let mut timestamps_str = String::new();
  177. let width = 50;
  178. for event in events {
  179. if event.action.as_str() == "comment" {
  180. continue
  181. }
  182. writeln!(timestamps_str, "{}", event.timestamp).unwrap();
  183. match event.action.as_str() {
  184. "title" => {
  185. writeln!(events_str, "- {} changed title to {}", event.author, event.content)
  186. .unwrap();
  187. }
  188. "rank" => {
  189. writeln!(events_str, "- {} changed rank to {}", event.author, event.content)
  190. .unwrap();
  191. }
  192. "state" => {
  193. writeln!(events_str, "- {} changed state to {}", event.author, event.content)
  194. .unwrap();
  195. }
  196. "assign" => {
  197. writeln!(events_str, "- {} assigned {}", event.author, event.content).unwrap();
  198. }
  199. "project" => {
  200. writeln!(events_str, "- {} changed project to {}", event.author, event.content)
  201. .unwrap();
  202. }
  203. "tags" => {
  204. writeln!(events_str, "- {} changed tags to {}", event.author, event.content)
  205. .unwrap();
  206. }
  207. "due" => {
  208. writeln!(
  209. events_str,
  210. "- {} changed due date to {}",
  211. event.author,
  212. timestamp_to_date(event.content.parse::<u64>().unwrap_or(0), DateFormat::Date)
  213. )
  214. .unwrap();
  215. }
  216. "desc" => {
  217. // wrap long description
  218. let ev_content =
  219. fill(&event.content, textwrap::Options::new(width).subsequent_indent(" "));
  220. // skip wrapped lines to align timestamp with the first line
  221. for _ in 1..ev_content.lines().count() {
  222. writeln!(timestamps_str, " ").unwrap();
  223. }
  224. writeln!(events_str, "- {} changed description to: {}", event.author, ev_content)
  225. .unwrap();
  226. }
  227. _ => {}
  228. }
  229. }
  230. (events_str, timestamps_str)
  231. }
  232. pub fn comments_as_string(events: Vec<TaskEvent>) -> (String, String) {
  233. let mut events_str = String::new();
  234. let mut timestamps_str = String::new();
  235. let width = 50;
  236. for event in events {
  237. if event.action.as_str() != "comment" {
  238. continue
  239. }
  240. writeln!(timestamps_str, "{}", event.timestamp).unwrap();
  241. if event.action.as_str() == "comment" {
  242. // wrap long comments
  243. let ev_content =
  244. fill(&event.content, textwrap::Options::new(width).subsequent_indent(" "));
  245. // skip wrapped lines to align timestamp with the first line
  246. for _ in 1..ev_content.lines().count() {
  247. writeln!(timestamps_str, " ").unwrap();
  248. }
  249. writeln!(events_str, "{}> {}", event.author, ev_content).unwrap();
  250. }
  251. }
  252. (events_str, timestamps_str)
  253. }