1use crate::Mapping;
2
3pub struct ColumnLayout {
4 pub sequence: usize,
5 pub command: usize,
6 pub insert_type: usize,
7 pub evaluate: usize,
8 pub execute: usize,
9 pub description: usize,
10 pub source: usize,
11}
12
13pub fn render_header(layout: &ColumnLayout) -> String {
14 format!(
15 "{:<seq$} {:<cmd$} {:<typ$} {:<eval$} {:<exec$} {:<desc$} {:<src$}\n",
16 "Sequence",
17 "Command",
18 "Type",
19 "Evaluate",
20 "Execute",
21 "Description",
22 "Source",
23 seq = layout.sequence,
24 cmd = layout.command,
25 typ = layout.insert_type,
26 eval = layout.evaluate,
27 exec = layout.execute,
28 desc = layout.description,
29 src = layout.source,
30 )
31}
32
33pub fn render_separator(layout: &ColumnLayout) -> String {
34 format!(
35 "{:-<seq$} {:-<cmd$} {:-<typ$} {:-<eval$} {:-<exec$} {:-<desc$} {:-<src$}\n",
36 "",
37 "",
38 "",
39 "",
40 "",
41 "",
42 "",
43 seq = layout.sequence,
44 cmd = layout.command,
45 typ = layout.insert_type,
46 eval = layout.evaluate,
47 exec = layout.execute,
48 desc = layout.description,
49 src = layout.source,
50 )
51}
52
53fn truncate_string(cmd: &str, max_len: usize) -> String {
54 if cmd.chars().count() > max_len {
55 cmd.chars()
56 .take(max_len.saturating_sub(3))
57 .collect::<String>()
58 + "..."
59 } else {
60 cmd.to_string()
61 }
62}
63
64pub fn render_row(layout: &ColumnLayout, sequence: &str, mapping: &Mapping) -> String {
65 let source = mapping
66 .source_file
67 .as_ref()
68 .map(|p| {
69 let path_str = p.display().to_string();
70 if let Some(pos) = path_str.find("mappings/") {
71 path_str[pos..].to_string()
72 } else if path_str.ends_with("mappings.toml") {
73 "mappings.toml".to_string()
74 } else {
75 path_str
76 }
77 })
78 .unwrap_or_default()
79 .to_string();
80
81 format!(
82 "{:<seq$} {:<cmd$} {:<typ$} {:<eval$} {:<exec$} {:<desc$} {:<src$}\n",
83 sequence,
84 truncate_string(&mapping.command, layout.command),
85 format!("{:?}", mapping.insert_type),
86 if mapping.evaluate { "Yes" } else { "No" },
87 if mapping.execute { "Yes" } else { "No" },
88 truncate_string(
89 &mapping.description.clone().unwrap_or_default(),
90 layout.description
91 ),
92 truncate_string(&source, layout.source),
93 seq = layout.sequence,
94 cmd = layout.command,
95 typ = layout.insert_type,
96 eval = layout.evaluate,
97 exec = layout.execute,
98 desc = layout.description,
99 src = layout.source,
100 )
101}