Columnizing a list with ANSI-color codes?

huangapple go评论62阅读模式
英文:

Columnizing a list with ANSI-color codes?

问题

Using columnpr -T3 列化带有 ANSI 转义的输入会导致项目的对齐出现问题,它们可能会过度或不足对齐。例如:ls -1 --color=always | column

我正在编写一个文件管理器,想要实现两列的格式,就像 Midnight Commander 中的样式一样。好消息是,该项目是用 Zsh 脚本编写的,我希望有一种良好的方法来将颜色化的 fd ./exa -1 --color=always/ls -1 --color=always 输出进行列化,包括一个 Bash/Zsh/Awk 等脚本作为解决方案。是否有办法?

英文:

Using column or pr -T3 to columnize an input with ANSI escapes results in bad align of the items – they are under or overaligned. For example: ls -1 --color=always|column.

I'm writing a file manager and would want 2 columns format, like in Midnight Commander. The good part is that the project is written in Zsh script and I would want a nice way of columnizing color fd ./exa -1 --color=always/ls -1 --color=always output, including a Bash/Zsh/Awk/etc. script as a solution. Is there a way?

答案1

得分: 0

zsh内置的print命令似乎可以处理颜色,当使用-C-c选项创建列时。源代码中甚至有这样的注释:

通过跳过转义序列来防止由于它们导致的列对齐不正确。八进制\033和\233是ANSI识别的可能转义字符。

将命令输出转换为print命令的参数可能有点棘手,但使用$(...)的命令替代和${(f)...}的行扩展应该可以工作:

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

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

英文:

It looks like the zsh builtin print can handle colors when creating columns with the -C and -c options. The source code even has this note:

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

Converting the command output to arguments for print can be slightly tricky, but this combination of a command substitution with $(...) and an expansion split by line using ${(f)...} should work:

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

You also can pass in data via a coprocess.

答案2

得分: 0

https://github.com/tecolicom/App-ansicolumn 可能是你正在寻找的工具。作者在GitHub和CPAN上有一个名为ansifold和其他实用工具。

英文:

https://github.com/tecolicom/App-ansicolumn is probably the tools you are looking for. The author has an ansifold and other utilities on gihub and CPAN.

huangapple
  • 本文由 发表于 2023年4月13日 23:44:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定