Skip to content

Commit 3eef8f7

Browse files
committed
[ansi_color][cmdr] Replace use of crossterm::Stylize with ansi_color
1 parent fcaf6ee commit 3eef8f7

File tree

4 files changed

+121
-52
lines changed

4 files changed

+121
-52
lines changed

2.code-search

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
178 results - 47 files
66

77
CHANGELOG.md:
8-
1045: tokio tracing macros for logging, eg: `tracing::debug!`, `tracing::info!`, etc.
9-
1060: tracing, macros for for logging, eg: `tracing::debug!`, `tracing::info!`, etc. See
8+
1056: tokio tracing macros for logging, eg: `tracing::debug!`, `tracing::info!`, etc.
9+
1071: tracing, macros for for logging, eg: `tracing::debug!`, `tracing::info!`, etc. See
1010

1111
cmdr/src/analytics_client.rs:
1212
113: tracing::debug!(
@@ -109,41 +109,41 @@ script/src/github_api.rs:
109109
43: tracing::debug!(
110110

111111
terminal_async/examples/terminal_async.rs:
112-
38: use tracing::info;
112+
35: use tracing::info;
113113

114114
tui/examples/demo/ex_app_no_layout/app_main.rs:
115-
325: tracing::info! {
115+
335: tracing::info! {
116116

117117
tui/examples/demo/ex_app_with_1col_layout/single_column_component.rs:
118118
265: tracing::info!(
119119

120120
tui/examples/demo/ex_app_with_2col_layout/app_main.rs:
121-
326: tracing::info!(
122-
346: tracing::error!("No focus id has been set, and it should be set!");
121+
334: tracing::info!(
122+
354: tracing::error!("No focus id has been set, and it should be set!");
123123

124124
tui/examples/demo/ex_app_with_2col_layout/column_render_component.rs:
125125
275: tracing::info!(
126126

127127
tui/examples/demo/ex_editor/app_main.rs:
128-
123: tracing::debug!("🪙 construct ex_rc::AppMain");
129-
339: tracing::error!("📣 Error activating simple modal: {msg:?}");
130-
368: tracing::error!(
131-
450: tracing::debug!(
132-
490: tracing::debug!(
133-
586: tracing::info!(
134-
618: tracing::debug!(
135-
703: tracing::debug!(
136-
789: tracing::debug!(
128+
125: tracing::debug!("🪙 construct ex_rc::AppMain");
129+
348: tracing::error!("📣 Error activating simple modal: {msg:?}");
130+
377: tracing::error!(
131+
459: tracing::debug!(
132+
499: tracing::debug!(
133+
595: tracing::info!(
134+
627: tracing::debug!(
135+
712: tracing::debug!(
136+
798: tracing::debug!(
137137

138138
tui/examples/demo/ex_pitch/app_main.rs:
139-
99: tracing::debug!("🪙 construct ex_pitch::AppWithLayout");
140-
309: tracing::info!(
141-
345: tracing::debug!(
139+
103: tracing::debug!("🪙 construct ex_pitch::AppWithLayout");
140+
320: tracing::info!(
141+
356: tracing::debug!(
142142

143143
tui/examples/demo/ex_rc/app_main.rs:
144-
179: tracing::debug!("🪙 construct ex_rc::AppWithLayout");
145-
430: tracing::info!(
146-
466: tracing::debug!(
144+
182: tracing::debug!("🪙 construct ex_rc::AppWithLayout");
145+
440: tracing::info!(
146+
476: tracing::debug!(
147147

148148
tui/src/tui/dialog/dialog_component/dialog_component_struct.rs:
149149
175: tracing::debug!(
@@ -187,13 +187,13 @@ tui/src/tui/terminal_lib_backends/input_device_ext.rs:
187187
104: tracing::error!("Error: {e:?}");
188188

189189
tui/src/tui/terminal_lib_backends/paint.rs:
190-
205: tracing::info!(
191-
215: tracing::info!(
190+
216: tracing::info!(
191+
226: tracing::info!(
192192

193193
tui/src/tui/terminal_lib_backends/render_pipeline_to_offscreen_buffer.rs:
194-
67: tracing::info!(
195-
205: tracing::info! {
196-
271: tracing::debug!(message = message);
194+
69: tracing::info!(
195+
204: tracing::info! {
196+
270: tracing::debug!(message = message);
197197

198198
tui/src/tui/terminal_lib_backends/crossterm_backend/offscreen_buffer_paint_impl.rs:
199199
75: tracing::info!(
@@ -212,17 +212,17 @@ tui/src/tui/terminal_lib_backends/crossterm_backend/render_op_impl.rs:
212212
566: call_if_true!(DEBUG_TUI_SHOW_TERMINAL_BACKEND, tracing::error!(msg));
213213

214214
tui/src/tui/terminal_window/main_event_loop.rs:
215-
149: tracing::info!(
216-
220: tracing::info!(
217-
270: tracing::info!(
218-
285: tracing::info!(
219-
306: tracing::info!(
220-
437: tracing::error!(
221-
508: tracing::error!(
222-
648: // level_filter: tracing::Level::DEBUG.into(),
215+
158: tracing::info!(
216+
243: tracing::info!(
217+
308: tracing::info!(
218+
324: tracing::info!(
219+
345: tracing::info!(
220+
477: tracing::error!(
221+
548: tracing::error!(
222+
688: // level_filter: tracing::Level::DEBUG.into(),
223223

224224
tui/src/tui/terminal_window/shared_global_data.rs:
225-
105: tracing::info!("main_event_loop -> Resize ⇲"=?new_size)
225+
130: tracing::info!("main_event_loop -> Resize ⇲"=?new_size)
226226

227227
tuify/examples/main_interactive.rs:
228228
35: tracing::debug!("Start logging... terminal window size: {:?}", get_size()?)

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,11 @@ Updated:
457457
the `write!` macro to write to a pre-existing buffer.
458458

459459
Added:
460+
- Add new target in `run` Nushell script called `nu run release-examples-no-log` to run
461+
the examples without logging. This is useful for performance testing. Now that HUD is
462+
displayed in the examples, there is no need to enable logging just to see this
463+
information (via `nu run log`).
464+
- Add `spinner_impl` module from `r3bl_terminal_async` crate.
460465
- Add `ResponseTimesRingBuffer` to replace `telemetry_global_static` module in
461466
`static_global_data.rs`. This is more accurate, performant, and space efficient. It
462467
uses a fixed backing store (`RingBuffer`) and a rate limiter (`RateLimiter`) to ensure
@@ -472,6 +477,8 @@ Removed:
472477
(and its dependencies `RateLimiter` and `RingBuffer`) in `r3bl_core`.
473478

474479
Changed:
480+
- Update all the demo examples in the `examples` folder to use the new telemetry API and
481+
display a HUD above the status bar to display FPS counter.
475482
- Use `smallvec` and `smallstr` crates to increase memory latency performance (for
476483
access, mutation, and allocation).
477484
- Use `Drop` trait implementation for `EditorBufferMut` to perform validation and clean
@@ -840,7 +847,7 @@ exhaustively tested and is able to handle many more corner cases.
840847

841848
### v_next_release_r3bl_core
842849

843-
This release has **lots** of major breaking changes. The codebase has rougly 140K lines of
850+
This release has **lots** of major breaking changes. The codebase has roughly 140K lines of
844851
code. And about 25K lines of code have been added, and 14K lines have been removed. This
845852
is a major release that is part of a total reorganization of the `r3bl-open-core` repo.
846853
These changes pay down all the technical debt accrued over the past 2 years of
@@ -1504,6 +1511,9 @@ that `truecolor` is supported.
15041511
- Added:
15051512
- Support for `$TERM_PROGRAM` = `vscode` to the list of environment variables that mean
15061513
that `truecolor` is supported. This is in `check_ansi_color.rs` file.
1514+
- Lots of new helper functions to make it easy to colorize `&str` like `red("hello")`,
1515+
`green("world")`, etc. This removes the requirement to depend on `crossterm::Stylize`
1516+
for colorizing strings that are intended to be displayed in console stdout / stderr.
15071517

15081518
### v0.7.0 (2024-10-18)
15091519

ansi_color/src/ansi_styled_text.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,65 @@ mod ansi_styled_text_impl {
6262
}
6363
}
6464

65+
/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
66+
pub fn green(text: &str) -> AnsiStyledText<'_> {
67+
AnsiStyledText {
68+
text,
69+
style: &[Style::Foreground(Color::Ansi256(34))],
70+
}
71+
}
72+
73+
/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
74+
pub fn red(text: &str) -> AnsiStyledText<'_> {
75+
AnsiStyledText {
76+
text,
77+
style: &[Style::Foreground(Color::Ansi256(196))],
78+
}
79+
}
80+
81+
/// More info: <https://www.ditig.com/256-colors-cheat-sheet>
82+
pub fn magenta(text: &str) -> AnsiStyledText<'_> {
83+
AnsiStyledText {
84+
text,
85+
style: &[Style::Foreground(Color::Ansi256(201))],
86+
}
87+
}
88+
89+
pub fn bold(text: &str) -> AnsiStyledText<'_> {
90+
AnsiStyledText {
91+
text,
92+
style: &[Style::Bold],
93+
}
94+
}
95+
96+
pub fn italic(text: &str) -> AnsiStyledText<'_> {
97+
AnsiStyledText {
98+
text,
99+
style: &[Style::Italic],
100+
}
101+
}
102+
103+
pub fn underline(text: &str) -> AnsiStyledText<'_> {
104+
AnsiStyledText {
105+
text,
106+
style: &[Style::Underline],
107+
}
108+
}
109+
110+
pub fn strikethrough(text: &str) -> AnsiStyledText<'_> {
111+
AnsiStyledText {
112+
text,
113+
style: &[Style::Strikethrough],
114+
}
115+
}
116+
117+
pub fn dim(text: &str) -> AnsiStyledText<'_> {
118+
AnsiStyledText {
119+
text,
120+
style: &[Style::Dim],
121+
}
122+
}
123+
65124
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumCount)]
66125
pub enum Style {
67126
Foreground(Color),

cmdr/src/analytics_client.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ use std::{fmt::{Display, Formatter, Result},
2020
path::PathBuf,
2121
sync::atomic::AtomicBool};
2222

23-
use crossterm::style::Stylize as _;
2423
use dirs::config_dir;
2524
use miette::IntoDiagnostic as _;
2625
use r3bl_analytics_schema::AnalyticsEvent;
26+
use r3bl_ansi_color::{green, magenta, red};
2727
use r3bl_core::{CommonError,
2828
CommonErrorType,
2929
CommonResult,
@@ -112,15 +112,15 @@ pub mod config_folder {
112112
call_if_true!(DEBUG_ANALYTICS_CLIENT_MOD, {
113113
tracing::debug!(
114114
"Successfully created config folder: {}",
115-
format!("{config_folder_path:?}").green()
115+
green(&format!("{config_folder_path:?}"))
116116
);
117117
});
118118
Ok(config_folder_path)
119119
}
120120
Err(error) => {
121121
tracing::error!(
122122
"Could not create config folder.\n{}",
123-
format!("{error:?}").red()
123+
red(&format!("{error:?}"))
124124
);
125125
CommonError::new_error_result_with_only_type(
126126
CommonErrorType::ConfigFolderCountNotBeCreated,
@@ -131,7 +131,7 @@ pub mod config_folder {
131131
None => {
132132
tracing::error!(
133133
"Could not get config folder.\n{}",
134-
format!("{:?}", try_get_config_folder_path()).red()
134+
red(&format!("{:?}", try_get_config_folder_path()))
135135
);
136136
CommonError::new_error_result_with_only_type(
137137
CommonErrorType::ConfigFolderPathCouldNotBeGenerated,
@@ -175,7 +175,7 @@ pub mod proxy_machine_id {
175175
call_if_true!(DEBUG_ANALYTICS_CLIENT_MOD, {
176176
tracing::debug!(
177177
"Successfully read proxy machine ID from file: {}",
178-
format!("{contents:?}").green()
178+
green(&format!("{contents:?}"))
179179
);
180180
});
181181
contents
@@ -194,14 +194,14 @@ pub mod proxy_machine_id {
194194
call_if_true!(DEBUG_ANALYTICS_CLIENT_MOD, {
195195
tracing::debug!(
196196
"Successfully wrote proxy machine ID to file: {}",
197-
format!("{new_id:?}").green()
197+
green(&format!("{new_id:?}"))
198198
);
199199
});
200200
}
201201
Err(error) => {
202202
tracing::error!(
203203
"Could not write proxy machine ID to file.\n{}",
204-
format!("{error:?}").red()
204+
red(&format!("{error:?}"))
205205
);
206206
}
207207
}
@@ -253,21 +253,21 @@ pub mod report_analytics {
253253
Ok(_) => {
254254
tracing::debug!(
255255
"Successfully reported analytics event to r3bl-base.\n{}",
256-
format!("{json:#?}").green()
256+
green(&format!("{json:#?}"))
257257
);
258258
}
259259
Err(error) => {
260260
tracing::error!(
261261
"Could not report analytics event to r3bl-base.\n{}",
262-
format!("{error:#?}").red()
262+
red(&format!("{error:#?}"))
263263
);
264264
}
265265
}
266266
}
267267
Err(error) => {
268268
tracing::error!(
269269
"Could not report analytics event to r3bl-base.\n{}",
270-
format!("{error:#?}").red()
270+
red(&format!("{error:#?}"))
271271
);
272272
}
273273
}
@@ -301,14 +301,14 @@ pub mod upgrade_check {
301301
let latest_version = body_text.trim().to_string();
302302
tracing::info!(
303303
"\n📦📦📦\nLatest version of cmdr is: {}",
304-
latest_version.clone().magenta()
304+
magenta(&latest_version)
305305
);
306306
let current_version = UPDATE_IF_NOT_THIS_VERSION.to_string();
307307
if latest_version != current_version {
308308
UPDATE_REQUIRED.store(true, std::sync::atomic::Ordering::Relaxed);
309309
tracing::info!(
310310
"\n💿💿💿\nThere is a new version of cmdr available: {}",
311-
latest_version.clone().magenta()
311+
magenta(&latest_version)
312312
);
313313
}
314314
}
@@ -330,13 +330,13 @@ pub mod http_client {
330330
call_if_true!(DEBUG_ANALYTICS_CLIENT_MOD, {
331331
tracing::debug!(
332332
"GET request succeeded: {}",
333-
format!("{response:#?}").green()
333+
green(&format!("{response:#?}"))
334334
);
335335
});
336336
Ok(response)
337337
} else {
338338
// Handle error response.
339-
tracing::error!("GET request failed: {}", format!("{response:#?}").red());
339+
tracing::error!("GET request failed: {}", red(&format!("{response:#?}")));
340340
response.error_for_status()
341341
}
342342
}
@@ -352,13 +352,13 @@ pub mod http_client {
352352
call_if_true!(DEBUG_ANALYTICS_CLIENT_MOD, {
353353
tracing::debug!(
354354
"POST request succeeded: {}",
355-
format!("{response:#?}").green()
355+
green(&format!("{response:#?}"))
356356
);
357357
});
358358
Ok(response)
359359
} else {
360360
// Handle error response.
361-
tracing::error!("POST request failed: {}", format!("{response:#?}").red());
361+
tracing::error!("POST request failed: {}", red(&format!("{response:#?}")));
362362
response.error_for_status()
363363
}
364364
}

0 commit comments

Comments
 (0)