【发布时间】:2011-04-13 00:18:16
【问题描述】:
我在使用 ruby printf 时发现了这一点,但它也适用于 C 的 printf。
如果您在输出字符串中包含 ANSI 颜色转义码,则会打乱对齐方式。
鲁比:
ruby-1.9.2-head > printf "%20s\n%20s\n", "\033[32mGreen\033[0m", "Green"
Green # 6 spaces to the left of this one
Green # correctly padded to 20 chars
=> nil
C 程序中的同一行产生相同的输出。
有没有办法让 printf(或其他东西)对齐输出而不为非打印字符添加空格?
这是一个错误,还是有充分的理由?
更新:由于在存在 ANSI 代码和宽字符时无法依赖 printf 来对齐数据,是否有最佳实践方法在 ruby 中的控制台中排列彩色表格数据?
【问题讨论】:
标签: terminal language-agnostic console printf ansi-colors