最佳Ansi Escape起点 [英] Best Ansi Escape beginning

查看:147
本文介绍了最佳Ansi Escape起点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个Ansi转义序列最容易携带和/或最简单,为什么?

Which Ansi escape sequence is the most portable and/or simply best and why?

1. "\u001B[32;1mThis is bright green\u001B[0m"
2. "\x1B[33;1mThis is bright yellow\x1B[0m"
3. "\e[35;4;1mThis is bright purple underlined\e[0m"

我一直在使用 printf \x1B [32; 1mgreen\x1B [0m (例如,在unix bash脚本中的例子)出于习惯,但我想知道是否有原因之一使用另一个。一个比其他的更便携吗?

I have been using printf "\x1B[32;1mgreen\x1B[0m" (that's an example in unix bash script for example) out of habit, but I was wondering if there were any reasons to use one over the other. Is one more portable than the others? That would be my assumption.

此外,如果您知道其他任何Ansi Escape序列,请随时在评论中或答案结尾处分享。

Also, if you know of any other Ansi Escape sequence feel free to share it in the comments or at the end of your answer.

如果您不知道Ansi Escape序列是什么,或者想对其更加熟悉,那么您就可以这样做: http://en.wikipedia.org/wiki/ANSI_escape_code

If you don't know what an Ansi Escape sequence is or want to become more familiar with it, then here you go: http://en.wikipedia.org/wiki/ANSI_escape_code

注意:

以上所有转义序列都可以在我使用过的所有Unix系统上运行,但是仍然必须依靠系统本身来解释转义码。例如,Windows不允许不允许进行任何形式的转义码,但四种除外(BEL,LF或换行,CR或回车,当然还有BS或退格键),因此Ansi转义序列将不起作用

All of the escape sequences above have worked on all of the Unix systems I have been on, however one must still rely on the system itself to interpret the escape codes. Windows, for example, does not permit any sort of escape codes except four (BEL, L-F or linefeed, C-R or carriage return and, of course, BS or backspace), so Ansi escape sequences will not work.

推荐答案

简短答案:这取决于主机字符串解析器。

长答案:

这取决于字符串解析器;也就是说,实际上将您的字符串( \x1b [1mSome string\x1b [0m ]作为文本并接受解析的代码段= http://en.wikipedia.org/wiki/Escape_character rel = nofollow>使用反斜杠 ANSI转义序列

It depends on the string parser; that is, the piece of code that actually takes in your string ("\x1b[1mSome string\x1b[0m") as a literal and parses the escape characters using the backslash ANSI escape sequence.


  • 对于支持十六进制转义的解析器( \ \x ),然后 \x1b (字符0x1B)应该可以正常工作。

  • 对于支持八进制转义( \ddd ),然后 \033 (八进制33)应该可以工作。

  • 对于支持Unicode转义( \u )的解析器,则 \u001B 应该起作用。

  • For parsers that support hexadecimal escapes (\x), then \x1b (character 0x1B) should work.
  • For parsers that support octal escapes (\ddd), then \033 (octal 33) should work.
  • For parsers that support unicode escapes (\u), then \u001B should work.

快速阐述: \x \u 相似; \x 通常指的是 h 十进制基数的单个字符,范围为​​0-255。 \u 的含义相同(以十六进制表示),但支持两个字节(在大多数解析器中),通常指16位 u

Quick elaboration: \x and \u are similar; \x usually refers to a single character, 0-255, in hexadecimal radix. \u means the same (as it is represented in hexadecimal), but supports two bytes (in most parsers) and generally refers to 16-bit unicode characters.






您所提到的较少使用/支持的转义字符,是 \e 。这种转义最常用于期望发生大量ANSI转义的解析器/语言,例如bash(和大多数其他shell)。


A lesser used/supported escape character, as you mentioned, is \e. This escape is most commonly used with parsers/languages that expect a lot of ANSI escaping to happen, such as bash (and most other shells).

例如, Node.js 不支持 \e

> console.log("\x1b[31mhello\x1b[0m")
hello
undefined
> console.log("\e[31mhello\e[0m")
e[31mhelloe[0m
undefined

卢阿也没有:

> print('\x1b[31mhello\x1b[0m')
hello
> print('\e[31mhello\e[0m')
stdin:1: invalid escape sequence near '\e'

附近的无效转义序列

甚至 Python

>>> print("\x1b[31mhello\x1b[0m")
hello
>>> print("\e[31mhello\e[0m")
\e[31mhello\e[0m
>>>

尽管 PHP 可以:

<?php
echo "\x1b[31mhello\x1b[0m\n"; // hello
echo "\e[31mhello\e[0m\n"; // hello

这篇关于最佳Ansi Escape起点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆