【发布时间】:2014-07-20 07:06:02
【问题描述】:
这是我目前为我的所有计算机(无论是 Windows、Red Hat 还是 OS X)设置提示的脚本:
import sys
import datetime
import platform
if platform.system() is 'Windows':
tealUText = ""
tealText = ""
greenText = ""
defaultText = ""
else:
tealUText = "\001\033[4;36m\002"
tealText = "\001\033[0;36m\002"
greenText = "\001\033[0;32m\002"
defaultText = "\001\033[0;0m\002"
class ClockPS1(object):
def __repr__(self):
now = datetime.datetime.now()
clock = str(now.strftime("%H:%M:%S"))
return tealUText + clock + greenText + " >>> " + defaultText
sys.ps1 = ClockPS1()
sys.ps2 = greenText + " ... " + defaultText
在所有系统上,这会在第一行打印出当前时间,然后是正常的“>>>”提示,然后如果我有一个多行输入,它具有正常的“...”提示,但缩进以便它与“>>>”提示符对齐(请记住,该提示符以当前时间为前缀)。
但问题是:在除 Windows 之外的每个平台上,当前时间都以蓝绿色(并带下划线)打印,提示为绿色,而我输入的任何内容都以正常颜色显示。如何在 Windows 中实现同样的功能?我已经看到了一些建议的解决方案,但它们依赖于在消息打印时调用函数,我认为这对我不起作用,因为ps 变量只是调用__repr__分配给他们,对吧?
(顺便说一句,我从这里得到了这个时间技巧:python: display elapsed time on shell)
【问题讨论】:
标签: python windows-7 cmd command-prompt sys