我经常使用类似的东西read -e -p "> All good ? (y/n)" -n 1 confirm;向用户询问.
我正在寻找一种颜色化输出的方法,正如命令echo -e所做的那样:
echo -e "\033[31m";
echo "Foobar"; // will be displayed in red
echo -e "\033[00m";
Run Code Online (Sandbox Code Playgroud)
我正在使用xterm.
在man echo,它说:
-e启用反斜杠转义的解释
有没有办法用read命令做同样的事情?(手册页中没有任何内容:( -r选项不起作用)
我使用标准技巧来定制交互式Python会话:
$ cat ~/.bashrc
export PYTHONSTARTUP=~/.pystartup
$ cat ~/.pystartup
import os
import sys
import atexit
import readline
import rlcompleter
historyPath = os.path.expanduser("~/.pyhistory")
def save_history(historyPath=historyPath):
import readline
readline.write_history_file(historyPath)
if os.path.exists(historyPath):
readline.read_history_file(historyPath)
term_with_colors = ['xterm', 'xterm-color', 'xterm-256color', 'linux', 'screen', 'screen-256color', 'screen-bce']
if os.environ.get('TERM') in term_with_colors:
green='\033[32m'
red='\033[31m'
reset='\033[0m'
sys.ps1 = red + '>>> ' + reset
sys.ps2 = green + '... ' + reset
del term_with_colors
atexit.register(save_history)
del os, sys, atexit, readline, rlcompleter, save_history, historyPath
现在我得到上下文敏感的完成和颜色提示.
问题来自颜色提示 - 当我在交互式Python会话中调用历史搜索 - 向后搜索 …
我正在使用Ruby中的readline为一些输出着色,但是我没有运气让换行正常工作.例如:
"\e[01;32mThis prompt is green and bold\e[00m > "
Run Code Online (Sandbox Code Playgroud)
期望的结果是:
This prompt is green and bold > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Run Code Online (Sandbox Code Playgroud)
我实际得到的是:
aaaaaaaaaaa is green and bold > aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Run Code Online (Sandbox Code Playgroud)
如果我删除颜色代码,换行工作正常.我知道用bash,如果颜色代码被错误地终止,就会发生这种情况,但我已经尝试了所有我能想到的东西,包括一些不同的宝石,行为是一样的.它也出现在具有不同版本Readline的多个系统上.这种特殊的项目使用rb-readline,而不是到C readline.