Skip to content

cenxuantian/FMT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FMT supported syntax

language state-machine

We can simple split it into 2 parts:

  • Navigator: used for find the exact item that is going to be processed
  • Processor: used for process the found item
[Navigator]:[Processor]

Navigator

  • Non: If Navigator is empty, find the item according to the index of the current formatter
  • Number: If Navigator is a number, then navigate to the item at that specific position

Processor

Detailed

Function Python Rust
Syntax f"Hello {name}"
"Hello {}".format(name)
format!("Hello {}", name)
println!("Hello {}", name)
Pos Param "{} {} {}".format(a, b, c) println!("{} {} {}", a, b, c)
Alignment "{:<10}"left "{:>10}" right"{:^10}"mid "{:<10}" left "{:>10}" right "{:^10}"mid
Char fill "{:*^10}".format("Hi") → "***Hi*****" format!("{:*^10}", "Hi") → "***Hi*****"
Format numbers "{:d}"integer"{:f}"floating point"{:.2f}" keep only 2 digits "{:b}" binary"{:x}" hex"{:.2}" number of digits
Symbol display (do not know) "{:+d}".format(42) → "+42" "{:+}"+42
Width and precision "{:8.3f}".format(3.14159) "{:8.3}"
Percentage "{:.2%}".format(0.1234) → "12.34%" Not supported
Escape { or } "{{}}".format() → "{}" "{{}}" → "{}"
Debug f"{obj!r}" 使用 repr() "{:?}" 调试输出(需 Debug trait)
Display f"{obj}" 使用 str() "{}" 普通输出(需 Display trait)
指数表示 "{:e}".format(12345) "{:e}"
进制转换 "{:b}".format(10)1010
"{:x}".format(255)ff
"{:b}""{:x}""{:o}" 同样可用
动态宽度/精度 "{:{width}.{prec}f}".format(3.14, width=8, prec=2) "{:width$.prec$}"(带 $ 变量)
自定义类型格式化 自定义类中实现 __format__ 实现 DisplayDebug trait

Print usage

Detailed

类型 格式 示例 效果说明
basic color front \033[30m ~ \033[37m \033[31mHello\033[0m 设置前景色(字体颜色)
basic color background \033[40m ~ \033[47m \033[41mText\033[0m 设置背景色
highlight front \033[90m ~ \033[97m \033[92mGreen\033[0m 更亮的前景色
highlight background \033[100m ~ \033[107m \033[104mBlueBG\033[0m 更亮的背景色
样式控制 见下表 控制字体样式(加粗、下划线等)
cursor control \033[{n};{m}H \033[10;20H 移动光标到第10行第20列
光标移动(相对) \033[{n}A(上移n行)
\033[{n}B(下移n行)
\033[{n}C(右移n列)
\033[{n}D(左移n列)
\033[2A 光标相对移动
Clear entire screen \033[2J 清除整个屏幕
Clear line \033[K 清除从光标到行尾的内容
save cursor pos \033[s 保存当前位置
restore cursor pos \033[u 恢复保存的光标位置
hide cursor \033[?25l 隐藏光标
show cursor \033[?25h 显示光标
reset all \033[0m 恢复默认样式(必须结束样式时加上)

Color table

Color front background highlight front highlight background
Black 30 40 90 100
Red 31 41 91 101
Green 32 42 92 102
Yellow 33 43 93 103
Blue 34 44 94 104
品红(洋红) 35 45 95 105
青(青蓝) 36 46 96 106
white 37 47 97 107

Control command

Stryle Ctrl Code Instance Effect
Reset 0 \033[0m 取消所有效果
Bold 1 \033[1m 字体加粗
Week 2 \033[2m 字体变淡
Italic 3 \033[3m 字体倾斜(部分终端支持)
Underline 4 \033[4m 添加下划线
Blink 5 \033[5m 文本闪烁(不推荐)
Reverse 7 \033[7m 交换前景与背景色
Hiden 8 \033[8m 文本不可见
DeleteLine 9 \033[9m 加删除线
!bold 21 或 22 \033[22m 关闭加粗/弱化
!underline 24 \033[24m 关闭下划线
!reverse 27 \033[27m 恢复正常显示

RGB

模式 格式 示例
256 color front \033[38;5;<n>m \033[38;5;196mRed Text\033[0m
256 color background \033[48;5;<n>m \033[48;5;45mCyan BG\033[0m
RGB front \033[38;2;R;G;Bm \033[38;2;255;100;0mOrange\033[0m
RGB background \033[48;2;R;G;Bm \033[48;2;0;100;255mBlueBG\033[0m

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors