Skip to content

Commit f30b8fc

Browse files
committed
[all] Clean up all calls to tracing::info, debug, etc to be consistent
[ script ] - Clean up environment mod functions and add better tracing [ core ] - Remove needless styling, consolidate into tui_color.rs - Add lots of beautiful color constants from giti into color! macro [ tui ] - Clean up strange &Option<&str> core and replace with Option<&str>
1 parent be965f3 commit f30b8fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1584
-1045
lines changed

2.code-search

+558-139
Large diffs are not rendered by default.

ansi_color/src/ansi_styled_text.rs

+31
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ pub fn red(text: &str) -> AnsiStyledText<'_> {
7878
}
7979
}
8080

81+
/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
82+
pub fn cyan(text: &str) -> AnsiStyledText<'_> {
83+
AnsiStyledText {
84+
text,
85+
style: &[Style::Foreground(Color::Ansi256(51))],
86+
}
87+
}
88+
89+
/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
90+
pub fn yellow(text: &str) -> AnsiStyledText<'_> {
91+
AnsiStyledText {
92+
text,
93+
style: &[Style::Foreground(Color::Ansi256(226))],
94+
}
95+
}
96+
8197
/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
8298
pub fn magenta(text: &str) -> AnsiStyledText<'_> {
8399
AnsiStyledText {
@@ -86,6 +102,14 @@ pub fn magenta(text: &str) -> AnsiStyledText<'_> {
86102
}
87103
}
88104

105+
/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
106+
pub fn blue(text: &str) -> AnsiStyledText<'_> {
107+
AnsiStyledText {
108+
text,
109+
style: &[Style::Foreground(Color::Ansi256(27))],
110+
}
111+
}
112+
89113
pub fn bold(text: &str) -> AnsiStyledText<'_> {
90114
AnsiStyledText {
91115
text,
@@ -121,6 +145,13 @@ pub fn dim(text: &str) -> AnsiStyledText<'_> {
121145
}
122146
}
123147

148+
pub fn dim_underline(text: &str) -> AnsiStyledText<'_> {
149+
AnsiStyledText {
150+
text,
151+
style: &[Style::Dim, Style::Underline],
152+
}
153+
}
154+
124155
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumCount)]
125156
pub enum Style {
126157
Foreground(Color),

cmdr/src/analytics_client.rs

+31-82
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use std::{fmt::{Display, Formatter, Result},
2323
use dirs::config_dir;
2424
use miette::IntoDiagnostic as _;
2525
use r3bl_analytics_schema::AnalyticsEvent;
26-
use r3bl_ansi_color::{green, magenta, red};
2726
use r3bl_core::{CommonError,
2827
CommonErrorType,
2928
CommonResult,
@@ -110,25 +109,19 @@ pub mod config_folder {
110109
match result_create_dir_all {
111110
Ok(_) => {
112111
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
113-
let message = "Successfully created config folder.";
114-
let config_folder = string_storage!("{config_folder_path:?}");
115-
let config_folder_fmt = green(&config_folder);
116112
// % is Display, ? is Debug.
117113
tracing::debug!(
118-
message = %message,
119-
config_folder_path = %config_folder_fmt
114+
message = "Successfully created config folder.",
115+
config_folder = ?config_folder_path
120116
);
121117
});
122118
Ok(config_folder_path)
123119
}
124120
Err(error) => {
125-
let message = "Could not create config folder.";
126-
let error = string_storage!("{error:?}");
127-
let error_fmt = red(&error);
128121
// % is Display, ? is Debug.
129122
tracing::error!(
130-
message = %message,
131-
error_fmt = %error_fmt,
123+
message = "Could not create config folder.",
124+
error = ?error
132125
);
133126
CommonError::new_error_result_with_only_type(
134127
CommonErrorType::ConfigFolderCountNotBeCreated,
@@ -137,17 +130,13 @@ pub mod config_folder {
137130
}
138131
}
139132
None => {
140-
let message = "Could not get config folder.";
141-
let config_folder_path =
142-
string_storage!("{:?}", try_get_config_folder_path());
143-
let config_folder_fmt = red(&config_folder_path);
144133
// % is Display, ? is Debug.
145134
tracing::error!(
146-
message = %message,
147-
config_folder_path = %config_folder_fmt
135+
message = "Could not access config folder.",
136+
error = "None"
148137
);
149138
CommonError::new_error_result_with_only_type(
150-
CommonErrorType::ConfigFolderPathCouldNotBeGenerated,
139+
CommonErrorType::ConfigFolderPathCouldNotBeAccessed,
151140
)
152141
}
153142
}
@@ -186,13 +175,10 @@ pub mod proxy_machine_id {
186175
match result {
187176
Ok(contents) => {
188177
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
189-
let message = "Successfully read proxy machine ID from file.";
190-
let contents = string_storage!("{contents:?}");
191-
let contents_fmt = green(&contents);
192178
// % is Display, ? is Debug.
193179
tracing::debug!(
194-
message = %message,
195-
contents = %contents_fmt
180+
message = "Successfully read proxy machine ID from file.",
181+
contents = %contents
196182
);
197183
});
198184
contents
@@ -208,25 +194,18 @@ pub mod proxy_machine_id {
208194
AnalyticsAction::MachineIdProxyCreate,
209195
);
210196
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
211-
let message =
212-
"Successfully wrote proxy machine ID to file.";
213-
let new_id = string_storage!("{new_id:?}");
214-
let new_id_fmt = green(&new_id);
215197
// % is Display, ? is Debug.
216198
tracing::debug!(
217-
message = %message,
218-
new_id = %new_id_fmt
199+
message = "Successfully wrote proxy machine ID to file.",
200+
new_id = %new_id
219201
);
220202
});
221203
}
222204
Err(error) => {
223-
let message = "Could not write proxy machine ID to file.";
224-
let error = string_storage!("{error:?}");
225-
let error_fmt = red(&error);
226205
// % is Display, ? is Debug.
227206
tracing::error!(
228-
message = %message,
229-
error = %error_fmt
207+
message = "Could not write proxy machine ID to file.",
208+
error = ?error
230209
);
231210
}
232211
}
@@ -277,38 +256,27 @@ pub mod report_analytics {
277256
match result {
278257
Ok(_) => {
279258
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
280-
let message =
281-
"Successfully reported analytics event to r3bl-base.";
282-
let json = string_storage!("{json:#?}");
283-
let json_fmt = green(&json);
284259
// % is Display, ? is Debug.
285260
tracing::debug!(
286-
message = %message,
287-
json = %json_fmt
261+
message = "Successfully reported analytics event to r3bl-base.",
262+
json = %string_storage!("{json:#?}")
288263
);
289264
});
290265
}
291266
Err(error) => {
292-
let message =
293-
"Could not report analytics event to r3bl-base.";
294-
let error = string_storage!("{error:#?}");
295-
let error_fmt = red(&error);
296267
// % is Display, ? is Debug.
297268
tracing::error!(
298-
message = %message,
299-
error = %error_fmt
269+
message = "Could not report analytics event to r3bl-base.",
270+
error = ?error
300271
);
301272
}
302273
}
303274
}
304275
Err(error) => {
305-
let message = "Could not serialize analytics event to JSON.";
306-
let error = string_storage!("{error:#?}");
307-
let error_fmt = red(&error);
308276
// % is Display, ? is Debug.
309277
tracing::error!(
310-
message = %message,
311-
error = %error_fmt
278+
message = "Could not serialize analytics event to JSON.",
279+
error = ?error
312280
);
313281
}
314282
}
@@ -341,25 +309,20 @@ pub mod upgrade_check {
341309
if let Ok(body_text) = response.text().await {
342310
let latest_version = body_text.trim().to_string();
343311
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
344-
let message = "\n📦📦📦\nLatest version of cmdr";
345-
let version_fmt = magenta(&latest_version);
346312
// % is Display, ? is Debug.
347313
tracing::info!(
348-
message = %message,
349-
version = %version_fmt
314+
message = "📦📦📦 Latest version of cmdr",
315+
version = %latest_version
350316
);
351317
});
352318
let current_version = UPDATE_IF_NOT_THIS_VERSION.to_string();
353319
if latest_version != current_version {
354320
UPDATE_REQUIRED.store(true, std::sync::atomic::Ordering::Relaxed);
355321
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
356-
let message =
357-
"\n💿💿💿\nThere is a new version of cmdr available";
358-
let version_fmt = magenta(&latest_version);
359322
// % is Display, ? is Debug.
360323
tracing::info!(
361-
message = %message,
362-
version = %version_fmt
324+
message = "💿💿💿 There is a new version of cmdr available",
325+
version = %latest_version
363326
);
364327
});
365328
}
@@ -380,25 +343,18 @@ pub mod http_client {
380343
if response.status().is_success() {
381344
// Handle successful response.
382345
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
383-
let message = "GET request succeeded.";
384-
let response = string_storage!("{response:#?}");
385-
let response_fmt = green(&response);
386-
// % is Display, ? is Debug.
387346
tracing::debug!(
388-
message = %message,
389-
response = %response_fmt
347+
message = "GET request succeeded.",
348+
response = %string_storage!("{response:#?}")
390349
);
391350
});
392351
Ok(response)
393352
} else {
394353
// Handle error response.
395-
let message = "GET request failed.";
396-
let response_msg = string_storage!("{response:#?}");
397-
let response_msg_fmt = red(&response_msg);
398354
// % is Display, ? is Debug.
399355
tracing::error!(
400-
message = %message,
401-
response = %response_msg_fmt
356+
message = "GET request failed.",
357+
response = %string_storage!("{response:#?}")
402358
);
403359
response.error_for_status()
404360
}
@@ -413,25 +369,18 @@ pub mod http_client {
413369
if response.status().is_success() {
414370
// Handle successful response.
415371
DEBUG_ANALYTICS_CLIENT_MOD.then(|| {
416-
let message = "POST request succeeded.";
417-
let response_msg = string_storage!("{response:#?}");
418-
let response_msg_fmt = green(&response_msg);
419372
// % is Display, ? is Debug.
420373
tracing::debug!(
421-
message = %message,
422-
response = %response_msg_fmt
374+
message = "POST request succeeded.",
375+
response = %string_storage!("{response:#?}")
423376
);
424377
});
425378
Ok(response)
426379
} else {
427380
// Handle error response.
428-
let message = "POST request failed.";
429-
let response_msg = string_storage!("{response:#?}");
430-
let response_msg_fmt = red(&response_msg);
431-
// % is Display, ? is Debug.
432381
tracing::error!(
433-
message = %message,
434-
response = %response_msg_fmt
382+
message = "POST request failed.",
383+
response = %string_storage!("{response:#?}")
435384
);
436385
response.error_for_status()
437386
}

cmdr/src/bin/edi.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use r3bl_core::{ColorWheel,
2424
CommonResult,
2525
GradientGenerationPolicy,
2626
TextColorizationPolicy,
27-
call_if_true,
2827
throws};
2928
use r3bl_log::try_initialize_logging_global;
3029
use r3bl_tuify::{LIZARD_GREEN, SLATE_GRAY, SelectionMode, StyleSheet, select_from_list};
@@ -40,9 +39,14 @@ async fn main() -> CommonResult<()> {
4039

4140
// Start logging.
4241
let enable_logging = cli_arg.global_options.enable_logging;
43-
call_if_true!(enable_logging, {
42+
43+
enable_logging.then(|| {
4444
try_initialize_logging_global(tracing_core::LevelFilter::DEBUG).ok();
45-
tracing::debug!("Start logging... cli_args {:?}", cli_arg);
45+
// % is Display, ? is Debug.
46+
tracing::debug!(
47+
message = "Start logging...",
48+
cli_arg = ?cli_arg
49+
);
4650
});
4751

4852
// Check analytics reporting.
@@ -87,8 +91,8 @@ async fn main() -> CommonResult<()> {
8791
}
8892

8993
// Stop logging.
90-
call_if_true!(enable_logging, {
91-
tracing::debug!("Stop logging...");
94+
enable_logging.then(|| {
95+
tracing::debug!(message = "Stop logging...");
9296
});
9397

9498
// Exit message.

cmdr/src/bin/giti.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use r3bl_cmdr::{AnalyticsAction,
3434
try_make_new_branch},
3535
report_analytics,
3636
upgrade_check};
37-
use r3bl_core::{CommonResult, call_if_true, throws};
37+
use r3bl_core::{CommonResult, throws};
3838
use r3bl_log::try_initialize_logging_global;
3939
use r3bl_tuify::{SelectionMode, StyleSheet, select_from_list_with_multi_line_header};
4040

@@ -47,9 +47,10 @@ async fn main() -> CommonResult<()> {
4747
let cli_arg = CLIArg::parse();
4848

4949
let enable_logging = cli_arg.global_options.enable_logging;
50-
call_if_true!(enable_logging, {
50+
enable_logging.then(|| {
5151
try_initialize_logging_global(tracing_core::LevelFilter::DEBUG).ok();
52-
tracing::debug!("Start logging... cli_args {:?}", cli_arg);
52+
// % is Display, ? is Debug.
53+
tracing::debug!(message = "Start logging...", cli_arg = ?cli_arg);
5354
});
5455

5556
// Check analytics reporting.
@@ -65,8 +66,8 @@ async fn main() -> CommonResult<()> {
6566

6667
launch_giti(cli_arg);
6768

68-
call_if_true!(enable_logging, {
69-
tracing::debug!("Stop logging...");
69+
enable_logging.then(|| {
70+
tracing::debug!(message = "Stop logging...");
7071
});
7172
})
7273
}
@@ -101,13 +102,17 @@ pub fn launch_giti(cli_arg: CLIArg) {
101102
AnalyticsAction::GitiFailedToRun,
102103
);
103104

104-
let err_msg = format!(
105-
" Could not run giti due to the following problem.\n{:#?}",
106-
error
105+
// % is Display, ? is Debug.
106+
tracing::error!(
107+
message = "Could not run giti due to the following problem",
108+
error = ?error
107109
);
108-
tracing::error!(err_msg);
110+
109111
AnsiStyledText {
110-
text: &err_msg.to_string(),
112+
text: &format!(
113+
" Could not run giti due to the following problem.\n{:#?}",
114+
error
115+
),
111116
style: &[Style::Foreground(GuardsRed.as_ansi_color())],
112117
}
113118
.println();

0 commit comments

Comments
 (0)