2 个版本

0.1.1 2019年1月15日
0.1.0 2018年8月8日

533命令行界面

Download history 177/week @ 2023-11-19 221/week @ 2023-11-26 311/week @ 2023-12-03 116/week @ 2023-12-10 380/week @ 2023-12-17 114/week @ 2023-12-24 106/week @ 2023-12-31 603/week @ 2024-01-07 777/week @ 2024-01-14 861/week @ 2024-01-21 403/week @ 2024-01-28 597/week @ 2024-02-04 1172/week @ 2024-02-11 907/week @ 2024-02-18 923/week @ 2024-02-25 579/week @ 2024-03-03

3,656 每月下载量
21 个crate中使用 (直接使用2个)

ISC 许可证

105KB
2.5K SLoC

此模块定义了一个树结构,您可以使用它来构建可着色渲染的文档。

渲染树中有三种节点

  • 行,表示内容后跟换行符
  • 部分,表示可由样式规则定位的命名内容。
  • 组件,是渲染树的参数化块

构建渲染树的最简单方法是使用 tree!

#[macro_use]
extern crate render_tree;
extern crate termcolor;
use render_tree::{Document, Line, RenderComponent};
use termcolor::StandardStream;

fn main() -> std::io::Result<()> {
    let world = "world";

    let document = tree! {
        <Line as {
            "Hello" {world}
        }>

        <Line as {
            "Goodbye" {world}
        }>
    };

    document.write()
}

这将打印出

Hello world
Goodbye world

您可以使用部分和样式表来着色输出

#[macro_use]
extern crate render_tree;
extern crate termcolor;
use render_tree::{Document, Line, RenderComponent, Section, Stylesheet};
use render_tree::prelude::*;
use termcolor::StandardStream;

fn main() -> std::io::Result<()> {
    let world = "world";

    let document = tree! {
        <Line as {
            <Section name="hello" as { "Hello" }>
            {world}
        }>

        <Line as {
            <Section name="goodbye" as { "Goodbye"}>
            {world}
        }>
    };

    let stylesheet = Stylesheet::new()
        .add("hello", "fg: blue")
        .add("goodbye", "fg: red");

    document.write_styled(&stylesheet)
}

这将打印出

Hello world
Goodbye world

将单词 "Hello" 着色为蓝色,将单词 "Goodbye" 着色为红色。

您可以将部分嵌套,它们可以由样式路径定位

#[macro_use]
extern crate render_tree;
extern crate termcolor;
use render_tree::{Document, Line, RenderComponent, Section, Stylesheet};
use render_tree::prelude::*;
use termcolor::StandardStream;

fn main() -> std::io::Result<()> {
    let world = "world";

    let document = tree! {
        <Line as {
            <Section name="hello-world" as {
                <Section name="greeting" as { "Hello" }>
                {world}
            }>
        }>

        <Line as {
            "Some content in the middle here"
        }>

        <Line as {
            <Section name="goodbye-world" as {
                <Section name="greeting" as { "Goodbye" }>
                {world}
            }>
        }>
    };

    let stylesheet = Stylesheet::new()
        .add("** greeting", "weight: bold")
        .add("hello-world greeting", "fg: red")
        .add("goodbye-world greeting", "fg: blue");

    document.write_styled(&stylesheet)
}

这将打印出

Hello world
Some content in the middle here
Goodbye world

将 "Hello world" 和 "Goodbye world" 加粗,单词 "Hello" 着色为红色(当然,也是加粗的),单词 "Goodbye" 着色为红色(也是加粗的)。

规则中的glob(**)由星号(*)替代,星号由文本名称替代。glob匹配零个或多个部分名称,星号匹配确切的单个部分名称。

不使用 tree!

不使用宏也很容易构建渲染树。重复前面的示例,不使用宏也不使用样式表的字符串DSL

#[macro_use]
extern crate render_tree;
extern crate termcolor;
use render_tree::{
    Color,
    Document,
    Line,
    Render,
    RenderComponent,
    Section,
    Selector,
    Segment,
    Style,
    Stylesheet
};
use termcolor::StandardStream;

fn main() -> std::io::Result<()> {
    let world = "world";
    
    let document = Document::with(
        Line(
            Section("hello-world", |doc|
                doc.add(
                    Section("greeting", |doc| doc.add("Hello").add(world))
                )
            )
        )
    ).add(
        Line(
            "Some content in the middle here"
        )
    ).add(
        Line(
            Section("goodbye-world", |doc|
                doc.add(
                    Section("greeting", |doc| doc.add("Goodbye").add(world))
                )
            )
        )
    );

    let stylesheet = Stylesheet::new()
        .add(Selector::glob().add("greeting"), Style::new().bold())
        .add(Selector::name("hello-world").add("greeting"), Style::new().fg(Color::Red))
        .add(Selector::name("goodbye-world").add("greeting"), Style::new().fg(Color::Blue));

    document.write_styled(&stylesheet)
}

依赖关系

~550–780KB
~12K SLoC