Cargo.toml 文件添加依赖:
[dependencies]
crossterm = "0.23"
crossterm 设置字体样式,非常简单一看就懂!
use crossterm::{
style::{Stylize}
};
fn main() {
println!("{}", "Bold".bold());
println!("{}", "Underlined".underlined());
println!("{}", "Negative".negative());
}
运行效果如下图:
![](https://img.haomeiwen.com/i1144920/8224f2f6f319edfe.png)
下面是一个使用 crossterm 的简单例子:
use crossterm::{
cursor::{Hide, MoveTo, MoveToNextLine, Show},
execute,
style::{Stylize, SetAttribute, Attribute, Color, Print, PrintStyledContent, ResetColor, SetForegroundColor, SetBackgroundColor},
terminal::{Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen, SetSize},
Result,
};
use std::io::{stdout};
fn main() -> Result<()> {
crossterm::terminal::enable_raw_mode()?; //raw 模式可用
execute!( //使用这个 execute 宏执行设置。
stdout(), //第一个参数必须是这个!
SetSize(60, 30), // 设置窗口的尺寸。
Clear(ClearType::All), //清屏。
EnterAlternateScreen, //进入备用画面。
SetForegroundColor(Color::Green), //设置前景色为绿色,就是字体的颜色 。
SetBackgroundColor(Color::Red), //设置背景色为红色。
Hide, //隐藏光标
MoveTo(20, 10), //移动光标到第20行,第10列。
PrintStyledContent( "你好\n".white()), //打印一些白色的东东!!
MoveToNextLine(2), //向下移动2行。
SetAttribute(Attribute::Bold), //设置属性:加粗。
Print("hello world!\n".black()), //打印一些黑色的东东!!
SetAttribute(Attribute::Reset),//设置属性:恢复默认。
MoveToNextLine(1),
Print("hello world!\n".blue()), //比一比粗细。
MoveToNextLine(3),
ResetColor, //恢复默认颜色。
Show, //显示光标。
//LeaveAlternateScreen, //离开备用画面,什么也不显示了!
)?;
crossterm::terminal::disable_raw_mode()?;//raw 模式不可用。
Ok(())
}
运行效果如下图:
![](https://img.haomeiwen.com/i1144920/2cc571a271c74899.png)
网友评论