用 ANSI 颜色代码对列表进行分栏?

问题描述 投票:0回答:1

使用

column
pr -T3
将带有 ANSI 转义符的输入列化会导致项目对齐不良——它们对齐不足或过度对齐。例如:
ls -1 --color=always|column
.

我正在写一个文件管理器并且想要 2 列格式,就像在 Midnight Commander 中一样。好的部分是该项目是用 Zsh 脚本编写的,我想要一种很好的方式来列化颜色

fd .
/
exa -1 --color=always
/
ls -1 --color=always
输出,包括 Bash/Zsh/Awk/等。脚本作为解决方案。有办法吗?

multiple-columns zsh ansi-escape ansi-colors
1个回答
0
投票

看起来

zsh
内置
print
可以在使用
-C
-c
选项创建列时处理颜色。源码中还有这样的注释:

* Prevent misaligned columns due to escape sequences by
* skipping over them. Octals \033 and \233 are the
* possible escape characters recognized by ANSI.

将命令输出转换为

print
的参数可能有点棘手,但是使用
$(...)
的命令替换和使用
${(f)...}
按行拆分的这种组合应该有效:

print -C 2 ${(f)"$(ls -1 --color=always)"}

您还可以通过 coprocess 传递数据。

© www.soinside.com 2019 - 2024. All rights reserved.