当前位置: 首页 > news >正文

FTXUI::Dom 模块

DOM 模块定义了分层的 FTXUI::Element 树,可用于构建复杂的终端界面,支持响应终端尺寸变化。

namespace ftxui {...// 定义文档 定义布局盒子
Element document = vbox({// 设置文本 设置加粗 设置文本颜色text("The window") | bold | color(Color::Blue),// 设置进度条gauge(0.5)// 设置文本text("The footer")
});// 添加边框 调用 ftxui::border 修饰函数
document = border(document);// 添加边框 使用管道操作符
document = document | border;// 添加边框 使用 |= 运算符
document |= border;...
}

Text

显示一个文本

text("你好 我是一段文本");

显示效果:

你好我是一段文本

vtext

与 Text 功能相同, 但文本是垂直显示

vtext("HELLO");

显示效果:

H
E
L
L
O

paragraph

类似 Text 用于显示大段文本,自动换行,单行字数取决于容器宽度

paragraph("A very long text");

![[paragraph.gif]]

其它方案

namespace ftxui {Element paragraph(std::string text);Element paragraphAlignLeft(std::string text);Element paragraphAlignRight(std::string text);Element paragraphAlignCenter(std::string text);Element paragraphAlignJustify(std::string text);
}

border

在元素周围添加一个边框

border(text("The element"))

显示效果:

┌───────────┐
│The element│
└───────────┘

管道操作符用法:

text("The element") | border

{: .prompt-tip }

其它方案

namespace ftxui {Element border(Element);Element borderLight(Element);Element borderHeavy(Element);Element borderDouble(Element);Element borderRounded(Element);Element borderEmpty(Element);Decorator borderStyled(BorderStyle);Decorator borderWith(Pixel);
}

window

Window 作用和 border 相同, 但在边框上有一个额外的标题,就像窗口标题一样

window("The window", text("The element"))

显示效果:

┌The window─┐
│The element│
└───────────┘

separator

分割线, 用于显示垂直或水平分割线, 将容器的内容一分为二

border(hbox({text("Left"), separator(),text("Right")})
)

显示效果:

┌────┬─────┐
│left│right│
└────┴─────┘

其它方案

namespace ftxui {Element separator(void);Element separatorLight();Element separatorHeavy();Element separatorDouble();Element separatorEmpty();Element separatorStyled(BorderStyle);Element separator(Pixel);Element separatorCharacter(std::string);Element separatorHSelector(float left,float right,Color background,Color foreground);Element separatorVSelector(float up,float down,Color background,Color foreground);
}

gauge

用于显示一个进度条 0.0~1.0

border(gauge(0.5))

显示效果:

┌────────────────────────────────────────────────────────────────────────────┐
│██████████████████████████████████████                                      │
└────────────────────────────────────────────────────────────────────────────┘

其它方案:

namespace {Element gauge(float ratio);Element gaugeLeft(float ratio);Element gaugeRight(float ratio);Element gaugeUp(float ratio);Element gaugeDown(float ratio);Element gaugeDirection(float ratio, GaugeDirection);
}

graph

显示动态图表,需配合 GraphFunction 回调

Element graph(GraphFunction);

Colors

用于设置颜色 彩色文本 彩色背景

Decorator color(Color);
Decorator bgcolor(Color);

支持以下几种颜色方案

Palette16

  • Default
  • Black
  • GrayDark
  • GrayLight
  • White
  • Blue
  • BlueLight
  • Cyan
  • CyanLight
  • Green
  • GreenLight
  • Magenta
  • MagentaLight
  • Red
  • RedLight
  • Yellow
  • YellowLight
text("Blue foreground") | color(Color::Blue);
text("Blue background") | bgcolor(Color::Blue);
text("Black on white") | color(Color::Black) | bgcolor(Color::White);

Palette256

text("HotPink") | color(Color::HotPink);

TrueColor

在支持 trueColor 的终端上,可以直接使用 24 位 RGB 色彩空间:

使用下面的构造函数指定颜色的 RGBHSV 值:

有两个构造函数:

ftxui::Color::RGB(uint8_t red, uint8_t green, uint8_t blue);
ftxui::Color::HSV(uint8_t hue, uint8_t saturation, uint8_t value);

LinearGradient

渐变色,可以用于前景/背景色

Decorator color(const LinearGradient&);
Decorator bgcolor(const LinearGradient&);

ftxui::LinearGradient由一个角度的程度和颜色停止列表来定义。

auto gradient = LinearGradient().Angle(45).AddStop(0.0, Color::Red).AddStop(0.5, Color::Green).AddStop(1.0, Color::Blue);

也可以使用简化的构造函数:

LinearGradient(Color::Red, Color::Blue);
LinearGradient(45, Color::Red, Color::Blue);

Style

除了彩色文字和彩色背景。许多终端支持文本效果,例如:bold,italic,dim,underlined,inverted,blink. . .

Element bold(Element);
Element italic(Element);
Element dim(Element);
Element inverted(Element);
Element underlined(Element);
Element underlinedDouble(Element);
Element strikethrough(Element);
Element blink(Element);
Decorator color(Color);
Decorator bgcolor(Color);
Decorator colorgrad(LinearGradient);
Decorator bgcolorgrad(LinearGradient);

显示效果:

![[Style.png]]

要使用这些效果,只需将元素与所需的效果包装:

underlined(bold(text("This text is bold and underlined")))

或者,使用管道操作员将其链在元件上:

text("This text is bold") | bold | underlined

Layout

允许元素通过以下方式排列:

  • 水平 ftxui::hbox
  • 垂直 ftxui::vbox
  • 网格 ftxui::gridbox . . .

代码示例: ftxui::hbox ftxui::vbox ftxui::filler

![[Pasted image 20250609165748.png]]

代码示例: ftxui::gridbox

![[Pasted image 20250609165841.png]]

代码示例: ftxui::flexbox

![[Pasted image 20250609165920.png]]

元素也可以使用 ftxui::flex 实现自适应

hbox({text("left") | border ,text("middle") | border | flex,text("right") | border,
});

显示效果:

┌────┐┌─────────────────────────────────────────────────────┐┌─────┐
│left││middle                                               ││right│
└────┘└─────────────────────────────────────────────────────┘└─────┘
hbox({text("left") | border ,text("middle") | border | flex,text("right") | border | flex,
});

显示效果:

┌────┐┌───────────────────────────────┐┌───────────────────────────────┐
│left││middle                         ││right                          │
└────┘└───────────────────────────────┘└───────────────────────────────┘

Table

用于实现表格 让数据更加整洁

gridbox({{ text("ID") | border, text("Name") | border, text("Age") | border },{ text("1") | border, text("Alice") | border, text("23") | border },{ text("2") | border, text("Bob") | border, text("30") | border },
});

Canvas

用于绘制矢量图形

auto c = Canvas(100, 100);
c.DrawPointLine(10, 10, 80, 10, Color::Red);
auto element = canvas(c);
http://www.xdnf.cn/news/960931.html

相关文章:

  • 足球数据如何驱动 AI 模型进化:从数据采集到智能决策的技术解析
  • PH热榜 | 2025-06-09
  • 小红本批量改写 v1.2.0绿色版
  • 标注工具核心代码解析——def load_image【canvas.py]
  • BeckHoff -->电脑与PLC连接
  • 全微分证明 链式法则 乘法法则 除法法则
  • 基于正点原子阿波罗F429开发板的LWIP应用(6)——SNTP功能和lwiperf测速
  • 第一章 空间解析几何与向量代数 ~ 空间直角坐标系
  • 【Fifty Project - D35】
  • 在线学堂-第二章媒资管理模块上
  • 高效清理C盘
  • Quick BI 自定义组件开发 -- 第一篇 Lifecycle 接口的定义
  • esp_image: invalid segment length 0xffffffff
  • MySQL自定义函数零基础学习教程
  • FastAPI 与 JWT 身份验证:保护你的 API
  • SpringBoot配置最新的AI版本加入Maven的配置方式
  • CDBench论文精读
  • 树莓派4B, ubuntu20.04, 安装Ros Noetic[踩坑记录]
  • 当拼音文字遇上回文:英语中的诗意镜像与文化密码
  • Profinet转CAN网关如何实现profinet与can协议互转
  • 如何通过API接口获取淘宝商品列表?操作详解
  • Quick BI 自定义组件开发 -- 第二篇 添加 echart 组件,开发图表
  • Spring AMQP
  • 打造高效能技术组织的逆向法则
  • 解读新交规中关于“电动自行车能否在快车道骑行”的核心问题
  • Shellshock漏洞与永恒之蓝(WannaCry)勒索病毒深度分析
  • [大A量化专栏] 看盘界面设置(未完待续)
  • Linux进程信号(一)
  • AI Bot到底是真助手,还是又一个流量收割伎俩?
  • 软件功能测试有哪些类型?软件测评机构