Skip to content

Commit 12041f2

Browse files
committed
test1: complete print_with_color exercise
1 parent 5367df9 commit 12041f2

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

arceos/exercises/print_with_color/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
axstd = { workspace = true, optional = true }
7+
axstd = { workspace = true,optional = true }
8+
9+
[features]
10+
default = ["axstd"] # 关键:默认启用 axstd

arceos/ulib/axstd/src/io/stdio.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,20 @@ pub fn stdout() -> Stdout {
163163

164164
#[doc(hidden)]
165165
pub fn __print_impl(args: core::fmt::Arguments) {
166-
if cfg!(feature = "smp") {
166+
// 添加编译错误来测试
167+
// compile_error!("AXSTD_COLOR_DEBUG: 确认axstd被编译");
168+
169+
let color_code = "\x1b[31m"; // 代表显示红色的控制序列
170+
let reset_code = "\x1b[0m";//reset_code (\x1b[0m) 是最重要的控制序列之一,
171+
// 它告诉终端:"到此为止,把所有文本属性恢复到默认状态"
172+
// 添加测试输出
173+
// let test_output = format_args!("{}[DEBUG]彩色输出测试{} ", color_code, reset_code);
174+
if cfg!(feature = "smp") {//这里的cfg! 是一个编译时布尔检查,返回 true 或 false
167175
// synchronize using the lock in axlog, to avoid interleaving
168176
// with kernel logs
169-
arceos_api::stdio::ax_console_write_fmt(args).unwrap();
177+
arceos_api::stdio::ax_console_write_fmt(format_args!("{}{}{}",color_code,args,reset_code)).unwrap();
170178
} else {
171-
stdout().lock().write_fmt(args).unwrap();
172-
}
179+
stdout().lock().write_fmt(format_args!("{}{}{}",color_code,args,reset_code)).unwrap();
180+
}//format_args!是标准化输出宏 可以记录输出的组合信息
181+
// 将控制序列与args组合到一起时 其中的控制序列不会显示 而是影响后续文本的属性
173182
}

0 commit comments

Comments
 (0)