56个版本 (4个破坏性更新)
新 0.5.13 | 2024年8月15日 |
---|---|
0.4.10 | 2024年8月3日 |
0.4.6 | 2024年7月25日 |
0.3.31 | 2024年3月30日 |
106 in 文本处理
2,484每月下载
用于 5 crate
115KB
3K SLoC
Minimo
描述
Minimo是一个用于Rust的最小化终端打印库,旨在简化创建彩色和格式化终端输出的任务。它提供了一系列功能,包括彩色文本输出、样式文本(粗体、斜体、下划线)以及增强终端交互的实用函数。
功能
- 使用简单宏的彩色文本输出。
- 文本样式(粗体、斜体、下划线)。
- 用于终端UI元素(如分隔符和定位文本)的实用函数。
- 具有随机着色字符的生动文本。
- 支持使用自定义样式的终端标题创建。
安装
将 minimo
添加到你的Rust项目的 Cargo.toml
文件
[dependencies]
minimo = "0.1.0"
用法
以下是一个使用 minimo
的快速示例
use minimo::{gray, header, in_bold, in_cyan, in_gray, in_italic, in_magenta, in_underline, in_yellow, show, showln, success, vibrant, yellow, Printable};
fn main() {
// Displaying a header
header!(success, "Minimo", "VERSION 0.1.0", "A minimalistic terminal printing library for Rust");
// Using showln! macro for colorful text segments
showln!(gray, "You can use ", magenta, "show!", gray, " or ", yellow, "showln!", gray, " macros");
showln!(gray, "to print text segments in different colors.\n (Make sure each segment starts with a color)");
// Using show! and showln! macros with Printable trait
"You can also use ".show();
"show! ".print(in_magenta);
"and ".print(in_gray);
"showln! ".println(in_cyan);
"macros to print random text segments with formatting.".showln();
// Additional demonstrations
"This is a line in gray.".println(in_gray);
"This line is in yellow.".println(in_yellow);
// Demonstrating text styling
"Bold and colorful text: ".show();
"Bold".print(in_bold);
", ".show();
"Italic".print(in_italic);
", and ".show();
"Underlined".println(in_underline);
// Showing usage of vibrant function for colorful characters
"Vibrant text example: ".print(vibrant);
"Colorful characters!".println(minimo::vibrant);
// Showing divider and reset line
minimo::divider();
"Divider above".showln();
minimo::reset_line();
"Reset line used".showln();
}
更多示例
use std::sync::Arc;
use minimo::*;
use minimo::ask::*;
use minimo::{gray, magenta, yellow, cyan, purple, red, purple_bold, white_dim, pink, white};
fn main() {
println!("{}", "This is a test".style(cyan));
// Using showln! macro for colorful text segments
showln!(
gray,
"You can use ",
magenta,
"show!",
gray,
" or ",
yellow,
"showln!",
gray,
" macros"
);
showln!(gray, "to print text segments in different colors.\n (Make sure each segment starts with a color)");
// Using show! and showln! macros with Printable trait
"You can also use ".show(white_dim);
"show! ".show(pink);
"and ".show(purple_bold);
"showln! ".show(cyan);
"macros to print random text segments with formatting.".showln(white_dim);
divider();
// Additional demonstrations
let s = (purple +"we can " + red + "mix " + yellow+ "colors " + yellow + "like " + "this") ;
s.showln();
(red + "or " + yellow + "like " + "this" + purple_bold).showln();
("or even " + cyan + "like this").showln();
( "or even " + red + "like this"+ cyan).showln();
// Showing divider and reset line
minimo::divider();
"Divider above".style(red).showln();
minimo::reset_line();
"Reset line used".show(red);
let s = StyledText::new("This is a styled text", red);
s.showln();
"This is also a styled text".style(red).showln();
divider();
divider!();
divider!("hello");
divider!("-", "hello");
divider!("-", "hello", "-");
divider!("hello", "-");
divider!("-", "hello", "-", "world");
let c = "This is a styled text".style(red);
c.showln();
paragraph!(purple_bold, "title", white,"This is a paragraph with a title and long text to see if the content will automatically be wrapped and displayed accordingly");
// Ask for input
let name = text("What is your name?").unwrap();
// Create a list of choices
let choices = vec![
choice!("First choice", "This is the first choice", || println!("First choice selected")),
choice!("Second choice", "This is the second choice", || println!("Second choice selected")),
choice!("Third choice", "This is the third choice", || println!("Third choice selected")),
];
// Ask for selection
let selected = selection!("Select an option", &choices);
showln!(gray, "You selected: ", yellow, selected.get_title());
selected.run();
}
详细API文档
函数
print_positioned
在指定位置打印提供的文本。
pub fn print_positioned(x: i16, y: i16, text: impl Into<String>)
reset_line
将光标重置到当前行的开头。
pub fn reset_line()
move_up
将光标向上移动 n
行。
pub fn move_up(n: i16)
move_down
将光标向下移动 n
行。
pub fn move_down(n: i16)
move_right
将光标向右移动 n
列。
pub fn move_right(n: i16)
move_left
将光标向左移动 n
列。
pub fn move_left(n: i16)
clear_line
清除当前行。
pub fn clear_line()
clear_screen
清除整个屏幕。
pub fn clear_screen()
enable_raw_mode
启用原始模式以进行自定义输入处理。
pub fn enable_raw_mode() -> std::io::Result<()>
disable_raw_mode
禁用原始模式。
pub fn disable_raw_mode() -> std::io::Result<()>
宏
divider!
打印带有可选文本对齐和样式的分隔符。
#[macro_export]
macro_rules! divider {
() => { /* implementation */ };
($($arg:tt)*) => { /* implementation */ };
}
paragraph!
打印具有标题和文本的段落,自动将文本包装到宽度内。
#[macro_export]
macro_rules! paragraph {
($title_style:ident, $title:expr, $text_style:ident, $text:expr) => { /* implementation */ };
}
tree!
打印JSON对象的树表示。
#[macro_export]
macro_rules! tree {
($val:expr) => { /* implementation */ };
}
async_choice!
为菜单创建异步选择。
#[macro_export]
macro_rules! async_choice {
($name:expr, $description:expr, $action:expr, $matches_if:expr) => { /* implementation */ };
($name:expr, $description:expr, $action:expr) => { /* implementation */ };
($name:expr, $action:expr) => { /* implementation */ };
}
selection!
显示菜单并返回所选选项。
#[macro_export]
macro_rules! selection {
($message:expr, $choices:expr) => { /* implementation */ };
($choices:expr) => { /* implementation */ };
}
特质
Printable
一个特质,支持在任意类型上调用 print
、write
、render
和 style
。
pub trait Printable {
fn print<F>(&self, func: F) where F: Fn(&str) -> String;
fn println<F>(&self, func: F) where F: Fn(&str) -> String;
fn print_positioned<F>(&self, x: i32, y: i32, func: F) where F: Fn(&str) -> String;
}
Renderable
用于渲染样式文本的特性。
pub trait Renderable<'a> {
fn render<'b>(&'b self, style: &'a CStyle<'a>) -> StyledText<'b> where 'a: 'b;
fn style<'b>(&'b self, style: &'a CStyle<'a>) -> StyledText<'b> where 'a: 'b;
fn show<'b>(&'b self, style: &'a CStyle<'
a>) where 'a: 'b;
fn showln<'b>(&'b self, style: &'a CStyle<'a>) where 'a: 'b;
fn write<'b>(&'b self, style: &'a CStyle<'a>, writer: &mut dyn Write) -> std::io::Result<()>
where 'a: 'b;
fn write_line<'b>(&'b self, style: &'a CStyle<'a>, writer: &mut dyn Write) -> std::io::Result<()>
where 'a: 'b;
}
可拾取
用于可以从列表中选择的项目的特性。
pub trait Pickable {
fn get_title(&self) -> String;
fn get_description(&self) -> String;
}
Minimo为在Rust中创建丰富、交互式的命令行界面提供了一套全面的工具。无论您需要简单的文本样式还是复杂的交互菜单,Minimo都能满足您的需求。
依赖项
~2–12MB
~145K SLoC