jirust_cli/utils/
table_printer.rs

1use prettytable::{Attr, Cell, Row, color};
2use std::collections::HashMap;
3
4use jira_v3_openapi::models::{
5    CreatedIssue, FieldCreateMetadata, IssueBean, IssueTransition, IssueTypeIssueCreateMetadata,
6    Project, ProjectCategory, Version,
7};
8
9use super::PrintableData;
10
11/// This function allows to print the objects details in a pretty way (full data).
12///
13/// It uses the prettytable library to print the version details.
14///
15/// # Arguments
16///
17/// * `data` - A TablePrintable enum
18///     * Project: A vector of Project structs
19///     * Version: A vector of Version structs
20///     * IssueType: A vector of IssueType structs
21///     * IssueTypeFields: A vector of IssueTypeFields structs
22///
23/// # Examples
24///
25/// ```
26/// use jira_v3_openapi::models::Version;
27/// use jirust_cli::utils::{PrintableData, table_printer::print_table_full};
28///
29/// let versions: Vec<Version> = vec![Version::new()];
30///
31/// print_table_full(PrintableData::Version{ versions });
32/// ```
33///
34pub fn print_table_full(data: PrintableData) {
35    let mut table = prettytable::Table::new();
36    match data {
37        PrintableData::Generic { data } => todo!("To Be Implemented! {:?}", data),
38        PrintableData::IssueCreated { issues } => {
39            table.add_row(row![
40                bFC->"Issue ID",
41                bFy->"Issue Key",
42                bFm->"Issue URL",
43            ]);
44            for issue in issues {
45                table.add_row(row![
46                    Fc->issue.id.unwrap_or("".to_string()),
47                    Fy->issue.key.unwrap_or("".to_string()),
48                    Fm->issue.param_self.unwrap_or("".to_string()),
49                ]);
50            }
51        }
52        PrintableData::IssueData { issues } => {
53            table.add_row(row![
54                bFC->"Issue ID",
55                bFy->"Issue Key",
56                bFm->"Issue Fields",
57                bFw->"Issue Transitions"
58            ]);
59            for issue in issues {
60                let fields = issue
61                    .fields
62                    .unwrap_or(HashMap::new())
63                    .iter()
64                    .map(|field| format!("{}: {:?}", field.0, field.1.to_string()))
65                    .collect::<Vec<String>>()
66                    .join(", ");
67                let transitions = issue
68                    .transitions
69                    .unwrap_or_default()
70                    .iter()
71                    .map(|transition| {
72                        format!(
73                            "{}: {} ({})",
74                            transition.clone().id.unwrap_or_default(),
75                            transition.clone().name.unwrap_or_default(),
76                            transition.is_available.unwrap_or_default()
77                        )
78                    })
79                    .collect::<Vec<String>>()
80                    .join(", ");
81                table.add_row(row![
82                    Fc->issue.id.unwrap_or("".to_string()),
83                    Fy->issue.key.unwrap_or("".to_string()),
84                    Fm->fields,
85                    Fw->transitions
86                ]);
87            }
88        }
89        PrintableData::Project { projects } => {
90            table.add_row(row![
91                bFC->"Project ID",
92                bFc->"Project Key",
93                bFm->"Name",
94                bFw->"Description",
95                bFb->"Category",
96            ]);
97            for project in projects {
98                table.add_row(row![
99                    Fc->project.id.unwrap_or("".to_string()),
100                    Fc->project.key.unwrap_or("".to_string()),
101                    Fm->project.name.unwrap_or("".to_string()),
102                    Fw->project.description.unwrap_or("".to_string()),
103                    Fb->project.project_category.unwrap_or(Box::new(ProjectCategory::default())).name.unwrap_or("".to_string()),
104                ]);
105            }
106        }
107        PrintableData::Version { versions } => {
108            table.add_row(row![
109                bFC->"Project ID",
110                bFc->"ID",
111                bFm->"Name",
112                bFw->"Description",
113                bFy->"Start Date",
114                bFr->"Release Date",
115                bFb->"Archived",
116                bFg->"Released"
117            ]);
118
119            for version in versions {
120                table.add_row(row![
121                    FC->version.project_id.unwrap_or_default(),
122                    Fc->version.id.unwrap_or_default(),
123                    Fm->version.name.unwrap_or_default(),
124                    Fw->version.description.unwrap_or_default(),
125                    Fy->version.start_date.unwrap_or_default(),
126                    Fr->version.release_date.unwrap_or_default(),
127                    Fb->version.archived.unwrap_or_default(),
128                    Fg->version.released.unwrap_or_default()
129                ]);
130            }
131        }
132        PrintableData::IssueTransitions { transitions } => {
133            table.add_row(row![
134                bFC->"Transition ID",
135                bFc->"Name",
136                bFm->"fields",
137            ]);
138            for transition in transitions {
139                table.add_row(row![
140                    Fc->transition.id.unwrap_or("".to_string()),
141                    Fc->transition.name.unwrap_or("".to_string()),
142                    Fm->transition.fields.unwrap_or(HashMap::new()).iter().map(|field| format!("{}: {:?}", field.0, field.1)).collect::<Vec<String>>().join(", "),
143                ]);
144            }
145        }
146        PrintableData::IssueType { issue_types } => {
147            table.add_row(row![
148                bFC->"Issue Type ID",
149                bFc->"Name",
150                bFm->"Description",
151                bFw->"Hierarchy Level",
152                bFb->"Subtasks",
153            ]);
154            for issue_type in issue_types {
155                table.add_row(row![
156                    Fc->issue_type.id.unwrap_or("".to_string()),
157                    Fc->issue_type.name.unwrap_or("".to_string()),
158                    Fm->issue_type.description.unwrap_or("".to_string()),
159                    Fw->issue_type.hierarchy_level.unwrap_or(-1),
160                    Fb->issue_type.subtask.unwrap_or(false),
161                ]);
162            }
163        }
164        PrintableData::IssueTypeField { issue_type_fields } => {
165            table.add_row(row![
166                bFC->"Field ID",
167                bFc->"Field Key",
168                bFm->"Field Name",
169                bFb->"Required",
170            ]);
171            for field in issue_type_fields {
172                table.add_row(row![
173                    Fc->field.field_id,
174                    Fc->field.key,
175                    Fm->field.name,
176                    Fb->field.required,
177                ]);
178            }
179        }
180        PrintableData::TransitionedIssue { issues } => {
181            table.add_row(row![
182                bFC->"Issue ID",
183                bFy->"Transitioned",
184                bFm->"Assigned",
185                bFw->"Fix Version"
186            ]);
187            for issue in issues {
188                table.add_row(Row::new(vec![
189                    Cell::new(issue.0.as_str())
190                        .with_style(Attr::Bold)
191                        .with_style(Attr::ForegroundColor(color::CYAN)),
192                    Cell::new(issue.1.as_str()).with_style(if issue.1 == "OK" {
193                        Attr::ForegroundColor(color::GREEN)
194                    } else {
195                        Attr::ForegroundColor(color::RED)
196                    }),
197                    Cell::new(issue.2.as_str()).with_style(if issue.2 == "OK" {
198                        Attr::ForegroundColor(color::GREEN)
199                    } else {
200                        Attr::ForegroundColor(color::RED)
201                    }),
202                    Cell::new(issue.3.as_str()).with_style(if issue.3 != "NO fixVersion set" {
203                        Attr::ForegroundColor(color::GREEN)
204                    } else {
205                        Attr::ForegroundColor(color::RED)
206                    }),
207                ]));
208            }
209        }
210    }
211    table.printstd();
212}
213
214/// This function allows to print the objects details in a pretty way (basic data).
215///
216/// It uses the prettytable library to print the version details.
217///
218/// # Arguments
219///
220/// * `data` - A TablePrintable enum
221///     * Project: A vector of Project structs
222///     * Version: A vector of Version structs
223///     * IssueType: A vector of IssueType structs
224///     * IssueTypeFields: A vector of IssueTypeFields structs
225///
226/// # Examples
227///
228/// ```
229/// use jira_v3_openapi::models::Version;
230/// use jirust_cli::utils::{PrintableData, table_printer::print_table_basic};
231///
232/// let versions: Vec<Version> = vec![Version::new()];
233/// print_table_basic(PrintableData::Version { versions });
234/// ```
235pub fn print_table_basic(data: PrintableData) {
236    let mut table = prettytable::Table::new();
237    match data {
238        PrintableData::Generic { data } => todo!("To Be Implemented! {:?}", data),
239        PrintableData::IssueCreated { issues } => {
240            table.add_row(row![
241                bFC->"Issue ID",
242                bFy->"Issue Key",
243                bFm->"Issue URL",
244            ]);
245            for issue in issues {
246                table.add_row(row![
247                    Fc->issue.id.unwrap_or("".to_string()),
248                    Fy->issue.key.unwrap_or("".to_string()),
249                    Fm->issue.param_self.unwrap_or("".to_string()),
250                ]);
251            }
252        }
253        PrintableData::IssueData { issues } => {
254            table.add_row(row![
255                bFC->"Issue ID",
256                bFy->"Issue Key",
257            ]);
258            for issue in issues {
259                table.add_row(row![
260                    Fc->issue.id.unwrap_or("".to_string()),
261                    Fy->issue.key.unwrap_or("".to_string()),
262                ]);
263            }
264        }
265        PrintableData::Project { projects } => {
266            table.add_row(row![
267                bFC->"Project ID",
268                bFc->"Project Key",
269                bFm->"Name",
270                bFw->"Description",
271                bFb->"Category",
272            ]);
273            for project in projects {
274                table.add_row(row![
275                    Fc->project.id.unwrap_or("".to_string()),
276                    Fc->project.key.unwrap_or("".to_string()),
277                    Fm->project.name.unwrap_or("".to_string()),
278                    Fw->project.description.unwrap_or("".to_string()),
279                    Fb->project.project_category.unwrap_or(Box::new(ProjectCategory::default())).name.unwrap_or("".to_string()),
280                ]);
281            }
282        }
283        PrintableData::Version { versions } => {
284            table.add_row(row![
285                bFC->"Project ID",
286                bFc->"ID",
287                bFm->"Name",
288                bFy->"Start Date",
289                bFr->"Release Date",
290                bFb->"Archived",
291                bFg->"Released"
292            ]);
293
294            for version in versions {
295                table.add_row(row![
296                    FC->version.project_id.unwrap_or_default(),
297                    Fc->version.id.unwrap_or_default(),
298                    Fm->version.name.unwrap_or_default(),
299                    Fy->version.start_date.unwrap_or_default(),
300                    Fr->version.release_date.unwrap_or_default(),
301                    Fb->version.archived.unwrap_or_default(),
302                    Fg->version.released.unwrap_or_default()
303                ]);
304            }
305        }
306        PrintableData::IssueTransitions { transitions } => {
307            table.add_row(row![
308                bFC->"Transition ID",
309                bFc->"Name",
310                bFm->"fields",
311            ]);
312            for transition in transitions {
313                table.add_row(row![
314                    Fc->transition.id.unwrap_or("".to_string()),
315                    Fc->transition.name.unwrap_or("".to_string()),
316                    Fm->transition.fields.unwrap_or(HashMap::new()).iter().map(|field| format!("{}: {:?}", field.0, field.1)).collect::<Vec<String>>().join(", "),
317                ]);
318            }
319        }
320        PrintableData::IssueType { issue_types } => {
321            table.add_row(row![
322                bFC->"Issue Type ID",
323                bFc->"Name",
324                bFm->"Description",
325                bFw->"Hierarchy Level",
326                bFb->"Subtasks",
327            ]);
328            for issue_type in issue_types {
329                table.add_row(row![
330                    Fc->issue_type.id.unwrap_or("".to_string()),
331                    Fc->issue_type.name.unwrap_or("".to_string()),
332                    Fm->issue_type.description.unwrap_or("".to_string()),
333                    Fw->issue_type.hierarchy_level.unwrap_or(-1),
334                    Fb->issue_type.subtask.unwrap_or(false),
335                ]);
336            }
337        }
338        PrintableData::IssueTypeField { issue_type_fields } => {
339            table.add_row(row![
340                bFC->"Field ID",
341                bFc->"Field Key",
342                bFm->"Field Name",
343                bFb->"Required",
344            ]);
345            for field in issue_type_fields {
346                table.add_row(row![
347                    Fc->field.field_id,
348                    Fc->field.key,
349                    Fm->field.name,
350                    Fb->field.required,
351                ]);
352            }
353        }
354        PrintableData::TransitionedIssue { issues } => {
355            table.add_row(row![
356                bFC->"Issue ID",
357                bFy->"Transitioned",
358                bFm->"Assigned",
359                bFw->"Fix Version"
360            ]);
361            for issue in issues {
362                table.add_row(Row::new(vec![
363                    Cell::new(issue.0.as_str())
364                        .with_style(Attr::Bold)
365                        .with_style(Attr::ForegroundColor(color::CYAN)),
366                    Cell::new(issue.1.as_str()).with_style(if issue.1 == "OK" {
367                        Attr::ForegroundColor(color::GREEN)
368                    } else {
369                        Attr::ForegroundColor(color::RED)
370                    }),
371                    Cell::new(issue.2.as_str()).with_style(if issue.2 == "OK" {
372                        Attr::ForegroundColor(color::GREEN)
373                    } else {
374                        Attr::ForegroundColor(color::RED)
375                    }),
376                    Cell::new(issue.3.as_str()).with_style(if issue.3 != "NO fixVersion set" {
377                        Attr::ForegroundColor(color::GREEN)
378                    } else {
379                        Attr::ForegroundColor(color::RED)
380                    }),
381                ]));
382            }
383        }
384    }
385    table.printstd();
386}
387
388/// This function allows to print the objects details in a pretty way (single data).
389///
390/// It uses the prettytable library to print the version details.
391///
392///
393/// # Arguments
394///
395/// * `data` - A TablePrintable enum
396///     * Project: A vector of Project structs
397///     * Version: A vector of Version structs
398///     * IssueType: A vector of IssueType structs
399///     * IssueTypeFields: A vector of IssueTypeFields structs
400///
401/// # Examples
402///
403/// ```
404/// use jira_v3_openapi::models::Version;
405/// use jirust_cli::utils::{PrintableData, table_printer::print_table_single};
406///
407/// let version: Version = Version::new();
408/// print_table_single(PrintableData::Version { versions: vec![version] });
409/// ```
410pub fn print_table_single(data: PrintableData) {
411    let mut table = prettytable::Table::new();
412    match data {
413        PrintableData::Generic { data } => todo!("To Be Implemented! {:?}", data),
414        PrintableData::IssueCreated { issues } => {
415            let issue = issues.first().unwrap_or(&CreatedIssue::default()).clone();
416            table.add_row(row![
417                bFC->"Issue ID",
418                bFy->"Issue Key",
419                bFm->"Issue URL",
420            ]);
421
422            table.add_row(row![
423                Fc->issue.id.unwrap_or("".to_string()),
424                Fy->issue.key.unwrap_or("".to_string()),
425                Fm->issue.param_self.unwrap_or("".to_string()),
426            ]);
427        }
428        PrintableData::IssueData { issues } => {
429            let issue = issues.first().unwrap_or(&IssueBean::default()).clone();
430            table.add_row(row![
431                bFC->"Issue ID",
432                bFy->"Issue Key",
433                bFm->"Issue Fields",
434                bFw->"Issue Transitions"
435            ]);
436            let fields = issue
437                .fields
438                .unwrap_or(HashMap::new())
439                .iter()
440                .map(|field| format!("{}: {:?}", field.0, field.1.to_string()))
441                .collect::<Vec<String>>()
442                .join(", ");
443            let transitions = issue
444                .transitions
445                .unwrap_or_default()
446                .iter()
447                .map(|transition| {
448                    format!(
449                        "{}: {} ({})",
450                        transition.clone().id.unwrap_or_default(),
451                        transition.clone().name.unwrap_or_default(),
452                        transition.is_available.unwrap_or_default()
453                    )
454                })
455                .collect::<Vec<String>>()
456                .join(", ");
457            table.add_row(row![
458                Fc->issue.id.unwrap_or("".to_string()),
459                Fy->issue.key.unwrap_or("".to_string()),
460                Fm->fields,
461                Fw->transitions
462            ]);
463        }
464        PrintableData::Project { projects } => {
465            let project = projects.first().unwrap_or(&Project::default()).clone();
466            table.add_row(row![
467                bFC->"Project ID",
468                bFc->"Project Key",
469                bFm->"Name",
470                bFw->"Description",
471                bFb->"Category",
472            ]);
473
474            table.add_row(row![
475                    Fc->project.id.unwrap_or("".to_string()),
476                    Fc->project.key.unwrap_or("".to_string()),
477                    Fm->project.name.unwrap_or("".to_string()),
478                    Fw->project.description.unwrap_or("".to_string()),
479                    Fb->project.project_category.unwrap_or(Box::new(ProjectCategory::default())).name.unwrap_or("".to_string()),
480                ]);
481        }
482        PrintableData::Version { versions } => {
483            let version = versions.first().unwrap_or(&Version::default()).clone();
484            table.add_row(row![
485                bFm->"Name",
486                bFy->"Start Date",
487                bFr->"Release Date",
488            ]);
489
490            table.add_row(row![
491                Fm->version.name.unwrap_or_default(),
492                Fy->version.start_date.unwrap_or_default(),
493                Fr->version.release_date.unwrap_or_default(),
494            ]);
495        }
496        PrintableData::IssueTransitions { transitions } => {
497            let transition = transitions
498                .first()
499                .unwrap_or(&IssueTransition::default())
500                .clone();
501            table.add_row(row![
502                bFC->"Transition ID",
503                bFc->"Name",
504                bFm->"fields",
505            ]);
506            table.add_row(row![
507                Fc->transition.id.unwrap_or("".to_string()),
508                Fc->transition.name.unwrap_or("".to_string()),
509                Fm->transition.fields.unwrap_or(HashMap::new()).iter().map(|field| format!("{}: {:?}", field.0, field.1)).collect::<Vec<String>>().join(", "),
510            ]);
511        }
512        PrintableData::IssueType { issue_types } => {
513            let issue_type = issue_types
514                .first()
515                .unwrap_or(&IssueTypeIssueCreateMetadata::default())
516                .clone();
517            table.add_row(row![
518                bFC->"Issue Type ID",
519                bFc->"Name",
520                bFm->"Description",
521                bFw->"Hierarchy Level",
522                bFb->"Subtasks",
523            ]);
524
525            table.add_row(row![
526                Fc->issue_type.id.unwrap_or("".to_string()),
527                Fc->issue_type.name.unwrap_or("".to_string()),
528                Fm->issue_type.description.unwrap_or("".to_string()),
529                Fw->issue_type.hierarchy_level.unwrap_or(-1),
530                Fb->issue_type.subtask.unwrap_or(false),
531            ]);
532        }
533        PrintableData::IssueTypeField { issue_type_fields } => {
534            let field = issue_type_fields
535                .first()
536                .unwrap_or(&FieldCreateMetadata::default())
537                .clone();
538            table.add_row(row![
539                bFC->"Field ID",
540                bFc->"Field Key",
541                bFm->"Field Name",
542                bFb->"Required",
543            ]);
544
545            table.add_row(row![
546                Fc->field.field_id,
547                Fc->field.key,
548                Fm->field.name,
549                Fb->field.required,
550            ]);
551        }
552        PrintableData::TransitionedIssue { issues } => {
553            table.add_row(row![
554                bFC->"Issue ID",
555                bFy->"Transitioned",
556                bFm->"Assigned",
557                bFw->"Fix Version"
558            ]);
559            for issue in issues {
560                table.add_row(Row::new(vec![
561                    Cell::new(issue.0.as_str())
562                        .with_style(Attr::Bold)
563                        .with_style(Attr::ForegroundColor(color::CYAN)),
564                    Cell::new(issue.1.as_str()).with_style(if issue.1 == "OK" {
565                        Attr::ForegroundColor(color::GREEN)
566                    } else {
567                        Attr::ForegroundColor(color::RED)
568                    }),
569                    Cell::new(issue.2.as_str()).with_style(if issue.2 == "OK" {
570                        Attr::ForegroundColor(color::GREEN)
571                    } else {
572                        Attr::ForegroundColor(color::RED)
573                    }),
574                    Cell::new(issue.3.as_str()).with_style(if issue.3 != "NO fixVersion set" {
575                        Attr::ForegroundColor(color::GREEN)
576                    } else {
577                        Attr::ForegroundColor(color::RED)
578                    }),
579                ]));
580            }
581        }
582    }
583
584    table.printstd();
585}