Skip to content

Commit f12c859

Browse files
AliwiWalideva-cosma
authored andcommitted
fix: resolved clippy warnings and cleaned up code style
- Used named arguments within the format strings instead of passing them as separate arguments. - Declared constants for ANSI escape codes which i used throughout the entire code to make it more readable. - Added RESET after each color or each line.
1 parent 6f00a65 commit f12c859

1 file changed

Lines changed: 101 additions & 110 deletions

File tree

tockloader-cli/src/display.rs

Lines changed: 101 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -5,234 +5,225 @@
55
use tockloader_lib::attributes::app_attributes::AppAttributes;
66
use tockloader_lib::attributes::system_attributes::SystemAttributes;
77

8-
// TODO(george-cosma): Fix this
9-
#[allow(clippy::uninlined_format_args)]
8+
// ANSI escape codes for colors
9+
const RESET: &str = "\x1b[0m";
10+
const BOLD_MAGENTA: &str = "\x1b[1;35m";
11+
const BOLD_RED: &str = "\x1b[1;31m";
12+
const BOLD_GREEN: &str = "\x1b[1;32m";
13+
const BOLD_YELLOW: &str = "\x1b[1;33m";
14+
1015
pub async fn print_list(app_details: &[AppAttributes]) {
1116
for (i, details) in app_details.iter().enumerate() {
12-
println!("\n\x1b[0m\x1b[1;35m ┏━━━━━━━━━━━━━━━━┓");
13-
println!(
14-
"\x1b[0m\x1b[1;31m ┃ \x1b[0m\x1b[1;32m App_{:<9?} \x1b[0m\x1b[1;31m┃",
15-
i
16-
);
17-
println!("\x1b[0m\x1b[1;33m ┗━━━━━━━━━━━━━━━━┛");
17+
println!("\n{RESET}{BOLD_MAGENTA} ┏━━━━━━━━━━━━━━━━┓");
18+
println!("{RESET}{BOLD_RED} ┃ {RESET}{BOLD_GREEN} App_{i} {RESET}{BOLD_RED}┃",);
19+
println!("{RESET}{BOLD_YELLOW} ┗━━━━━━━━━━━━━━━━┛");
1820
println!(
19-
"\n \x1b[1;32m Name: {}",
20-
details.tbf_header.get_package_name().unwrap()
21+
"\n {BOLD_GREEN} Name: {RESET}{}",
22+
details.tbf_header.get_package_name().unwrap(),
2123
);
2224

2325
println!(
24-
" \x1b[1;32m Version: {}",
25-
details.tbf_header.get_binary_version()
26+
" {BOLD_GREEN} Version: {RESET}{}",
27+
details.tbf_header.get_binary_version(),
2628
);
2729

2830
println!(
29-
" \x1b[1;32m Enabled: {}",
30-
details.tbf_header.enabled()
31+
" {BOLD_GREEN} Enabled: {RESET}{}",
32+
details.tbf_header.enabled(),
3133
);
3234

3335
println!(
34-
" \x1b[1;32m Sticky: {}",
35-
details.tbf_header.sticky()
36+
" {BOLD_GREEN} Sticky: {RESET}{}",
37+
details.tbf_header.sticky(),
3638
);
3739

3840
println!(
39-
" \x1b[1;32m Total_Size: {}\n\n",
40-
details.tbf_header.total_size()
41+
" {BOLD_GREEN} Total_Size: {RESET}{}\n\n",
42+
details.tbf_header.total_size(),
4143
);
4244
}
4345
}
4446

45-
// TODO(george-cosma): Fix this
46-
#[allow(clippy::uninlined_format_args)]
4747
pub async fn print_info(app_details: &mut [AppAttributes], system_details: &mut SystemAttributes) {
4848
for (i, details) in app_details.iter().enumerate() {
49-
println!("\n\x1b[0m\x1b[1;35m ┏━━━━━━━━━━━━━━━━┓");
50-
println!(
51-
"\x1b[0m\x1b[1;31m ┃ \x1b[0m\x1b[1;32m App_{:<9?} \x1b[0m\x1b[1;31m┃",
52-
i
53-
);
54-
println!("\x1b[0m\x1b[1;33m ┗━━━━━━━━━━━━━━━━┛");
49+
println!("\n{RESET}{BOLD_MAGENTA} ┏━━━━━━━━━━━━━━━━┓");
50+
println!("{RESET}{BOLD_RED} ┃ {RESET}{BOLD_GREEN} App_{i} {RESET}{BOLD_RED}┃");
51+
println!("{RESET}{BOLD_YELLOW} ┗━━━━━━━━━━━━━━━━┛");
52+
5553
println!(
56-
"\n \x1b[1;32m Name: {}",
57-
details.tbf_header.get_package_name().unwrap()
54+
"\n {BOLD_GREEN} Name: {RESET}{}",
55+
details.tbf_header.get_package_name().unwrap(),
5856
);
5957

6058
println!(
61-
" \x1b[1;32m Version: {}",
62-
details.tbf_header.get_binary_version()
59+
" {BOLD_GREEN} Version: {RESET}{}",
60+
details.tbf_header.get_binary_version(),
6361
);
6462

6563
println!(
66-
" \x1b[1;32m Enabled: {}",
67-
details.tbf_header.enabled()
64+
" {BOLD_GREEN} Enabled: {RESET}{}",
65+
details.tbf_header.enabled(),
6866
);
6967

7068
println!(
71-
" \x1b[1;32m Stricky: {}",
72-
details.tbf_header.sticky()
69+
" {BOLD_GREEN} Sticky: {RESET}{}",
70+
details.tbf_header.sticky(),
7371
);
7472

7573
println!(
76-
" \x1b[1;32m Total_Size: {}",
77-
details.tbf_header.total_size()
74+
" {BOLD_GREEN} Total_Size: {RESET}{}",
75+
details.tbf_header.total_size(),
7876
);
7977

8078
println!(
81-
" \x1b[1;32m Address in Flash: {}",
82-
system_details.appaddr.unwrap()
79+
" {BOLD_GREEN} Address in Flash: {RESET}{}",
80+
system_details.appaddr.unwrap(),
8381
);
8482

8583
println!(
86-
" \x1b[1;32m TBF version: {}",
87-
details.tbf_header.get_binary_version()
84+
" {BOLD_GREEN} TBF version: {RESET}{}",
85+
details.tbf_header.get_binary_version(),
8886
);
8987

9088
println!(
91-
" \x1b[1;32m header_size: {}",
92-
details.tbf_header.header_size()
89+
" {BOLD_GREEN} header_size: {RESET}{}",
90+
details.tbf_header.header_size(),
9391
);
9492

9593
println!(
96-
" \x1b[1;32m total_size: {}",
97-
details.tbf_header.total_size()
94+
" {BOLD_GREEN} total_size: {RESET}{}",
95+
details.tbf_header.total_size(),
9896
);
9997

10098
println!(
101-
" \x1b[1;32m checksum: {}",
102-
details.tbf_header.checksum()
99+
" {BOLD_GREEN} checksum: {RESET}{}",
100+
details.tbf_header.checksum(),
103101
);
104102

105-
println!(" \x1b[1;32m flags:");
103+
println!(" {BOLD_GREEN} flags:{RESET}");
106104
println!(
107-
" \x1b[1;32m enabled: {}",
108-
details.tbf_header.enabled()
105+
" {BOLD_GREEN} enabled: {RESET}{}",
106+
details.tbf_header.enabled(),
109107
);
110108

111109
println!(
112-
" \x1b[1;32m sticky: {}",
113-
details.tbf_header.sticky()
110+
" {BOLD_GREEN} sticky: {RESET}{}",
111+
details.tbf_header.sticky(),
114112
);
115113

116-
println!(" \x1b[1;32m TVL: Main (1)",);
117-
114+
println!(" {BOLD_GREEN} TVL: Main (1){RESET}");
118115
println!(
119-
" \x1b[1;32m init_fn_offset: {}",
120-
details.tbf_header.get_init_function_offset()
116+
" {BOLD_GREEN} init_fn_offset: {RESET}{}",
117+
details.tbf_header.get_init_function_offset(),
121118
);
122119

123120
println!(
124-
" \x1b[1;32m protected_size: {}",
125-
details.tbf_header.get_protected_size()
121+
" {BOLD_GREEN} protected_size: {RESET}{}",
122+
details.tbf_header.get_protected_size(),
126123
);
127124

128125
println!(
129-
" \x1b[1;32m minimum_ram_size: {}",
130-
details.tbf_header.get_minimum_app_ram_size()
126+
" {BOLD_GREEN} minimum_ram_size: {RESET}{}",
127+
details.tbf_header.get_minimum_app_ram_size(),
131128
);
132129

133-
println!(" \x1b[1;32m TVL: Program (9)",);
134-
130+
println!(" {BOLD_GREEN} TVL: Program (9){RESET}");
135131
println!(
136-
" \x1b[1;32m init_fn_offset: {}",
137-
details.tbf_header.get_init_function_offset()
132+
" {BOLD_GREEN} init_fn_offset: {RESET}{}",
133+
details.tbf_header.get_init_function_offset(),
138134
);
139135

140136
println!(
141-
" \x1b[1;32m protected_size: {}",
142-
details.tbf_header.get_protected_size()
137+
" {BOLD_GREEN} protected_size: {RESET}{}",
138+
details.tbf_header.get_protected_size(),
143139
);
144140

145141
println!(
146-
" \x1b[1;32m minimum_ram_size: {}",
147-
details.tbf_header.get_minimum_app_ram_size()
142+
" {BOLD_GREEN} minimum_ram_size: {RESET}{}",
143+
details.tbf_header.get_minimum_app_ram_size(),
148144
);
149145

150146
println!(
151-
" \x1b[1;32m binary_end_offset: {}",
152-
details.tbf_header.get_binary_end()
147+
" {BOLD_GREEN} binary_end_offset: {RESET}{}",
148+
details.tbf_header.get_binary_end(),
153149
);
154150

155151
println!(
156-
" \x1b[1;32m app_version: {}",
157-
details.tbf_header.get_binary_version()
152+
" {BOLD_GREEN} app_version: {RESET}{}",
153+
details.tbf_header.get_binary_version(),
158154
);
159155

160-
println!(" \x1b[1;32m TVL: Package Name (3)",);
161-
156+
println!(" {BOLD_GREEN} TVL: Package Name (3){RESET}");
162157
println!(
163-
" \x1b[1;32m package_name: {}",
164-
details.tbf_header.get_package_name().unwrap()
158+
" {BOLD_GREEN} package_name: {RESET}{}",
159+
details.tbf_header.get_package_name().unwrap(),
165160
);
166161

167-
println!(" \x1b[1;32m TVL: Kernel Version (8)",);
168-
162+
println!(" {BOLD_GREEN} TVL: Kernel Version (8){RESET}");
169163
println!(
170-
" \x1b[1;32m kernel_major: {}",
171-
details.tbf_header.get_kernel_version().unwrap().0
164+
" {BOLD_GREEN} kernel_major: {RESET}{}",
165+
details.tbf_header.get_kernel_version().unwrap().0,
172166
);
173167

174168
println!(
175-
" \x1b[1;32m kernel_minor: {}",
169+
" {BOLD_GREEN} kernel_minor: {RESET}{}",
176170
details.tbf_header.get_kernel_version().unwrap().1,
177171
);
178172

179-
println!("\n \x1b[1;32m Footer");
173+
println!("\n {BOLD_GREEN} Footer{RESET}");
180174

181175
let mut total_footer_size: u32 = 0;
182176

183-
// Usage of +4 is a result of the structure of the Tock Binary Format (https://book.tockos.org/doc/tock_binary_format)
184-
// Because we need the real size of the footer including the type and length.
177+
// Usage of +4 is a result of the structure of the Tock Binary Format (https://book.tockos.org/doc/tock_binary_format)
178+
// Because we need the real size of the footer including the type and length.
185179
for footer_details in details.tbf_footers.iter() {
186180
total_footer_size += footer_details.size + 4;
187181
}
188182

189-
println!(
190-
" \x1b[1;32m footer_size: {}",
191-
total_footer_size
192-
);
183+
println!(" {BOLD_GREEN} footer_size: {RESET}{total_footer_size}");
193184

194-
for (i, footer_details) in details.tbf_footers.iter().enumerate() {
195-
println!(" \x1b[1;32m Footer [{i}] TVL: Credentials");
185+
for (j, footer_details) in details.tbf_footers.iter().enumerate() {
186+
println!(" {BOLD_GREEN} Footer [{j}] TVL: Credentials{RESET}");
196187

197188
println!(
198-
" \x1b[1;32m Type: {}",
199-
footer_details.credentials.get_type()
189+
" {BOLD_GREEN} Type: {RESET}{}",
190+
footer_details.credentials.get_type(),
200191
);
201192

202-
// Usage of -4 is a result of the structure of the Tock Binary Format (https://book.tockos.org/doc/tock_binary_format)
203-
// Because we only need the size of the credentials without the type and length bytes.
193+
// Usage of -4 is a result of the structure of the Tock Binary Format (https://book.tockos.org/doc/tock_binary_format)
194+
// Because we only need the size of the credentials without the type and length bytes.
204195
println!(
205-
" \x1b[1;32m Length: {}",
206-
footer_details.size - 4
196+
" {BOLD_GREEN} Length: {RESET}{}",
197+
footer_details.size - 4,
207198
);
208199
}
209200
}
210201

211-
println!("\n\n\x1b[1;32m Kernel Attributes");
202+
println!("\n\n{BOLD_GREEN} Kernel Attributes{RESET}");
212203
println!(
213-
"\x1b[1;32m Sentinel: {:<10}",
214-
system_details.sentinel.clone().unwrap()
204+
"{BOLD_GREEN} Sentinel: {:<10}{RESET}",
205+
system_details.sentinel.clone().unwrap(),
215206
);
216207
println!(
217-
"\x1b[1;32m Version: {:<10}",
218-
system_details.kernel_version.unwrap()
208+
"{BOLD_GREEN} Version: {:<10}{RESET}",
209+
system_details.kernel_version.unwrap(),
219210
);
220-
println!("\x1b[1;32m KATLV: APP Memory");
211+
println!("{BOLD_GREEN} KATLV: APP Memory{RESET}");
221212
println!(
222-
"\x1b[1;32m app_memory_start: {:<10}",
223-
system_details.app_mem_start.unwrap()
213+
"{BOLD_GREEN} app_memory_start: {:<10}{RESET}",
214+
system_details.app_mem_start.unwrap(),
224215
);
225216
println!(
226-
"\x1b[1;32m app_memory_len: {:<10}",
227-
system_details.app_mem_len.unwrap()
217+
"{BOLD_GREEN} app_memory_len: {:<10}{RESET}",
218+
system_details.app_mem_len.unwrap(),
228219
);
229-
println!("\x1b[1;32m KATLV: Kernel Binary");
220+
println!("{BOLD_GREEN} KATLV: Kernel Binary{RESET}");
230221
println!(
231-
"\x1b[1;32m kernel_binary_start: {:<10}",
232-
system_details.kernel_bin_start.unwrap()
222+
"{BOLD_GREEN} kernel_binary_start: {:<10}{RESET}",
223+
system_details.kernel_bin_start.unwrap(),
233224
);
234225
println!(
235-
"\x1b[1;32m kernel_binary_len: {:<10}\n\n",
236-
system_details.kernel_bin_len.unwrap()
226+
"{BOLD_GREEN} kernel_binary_len: {:<10}{RESET}\n\n",
227+
system_details.kernel_bin_len.unwrap(),
237228
);
238229
}

0 commit comments

Comments
 (0)