Windows中的彩色Python提示? [英] Colored Python Prompt in Windows?

查看:255
本文介绍了Windows中的彩色Python提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的脚本,目前设置我的所有电脑(无论是Windows,红帽还是OS X)的提示:

Here's my script that currently sets up my prompts for all of my computers (whether they're Windows, Red Hat, or 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

在所有系统上,这将打印当前时间,后面跟着第一行的正常>>>提示,然后如果我有一个多行输入,它有正常的...提示,但缩进,以便它与>>>提示符(记住该提示符以当前时间为前缀)。

On all systems this prints out the current time followed by the normal ">>>" prompt on the first line, and then if I have a multiline input it has the normal "..." prompt, but indented so that it aligns with the ">>>" prompt (remember that that prompt is prefixed by the current time).

这里是问题:平台除Windows之外,当前时间打印为蓝绿色(带下划线),提示为绿色,并且无论我键入的是否是以正常颜色显示。如何在Windows中实现同样的事情?我看到一些解决方案建议,但他们依赖于调用函数,而消息正在打印,我不认为这将对我有用的事实,因为 ps 变量只是调用 __ repr __ 对任何分配给他们,对吗?

Here's the question though: On every platform besides Windows, the current time prints in teal (and underlined), the prompts are in green, and whatever I type shows up in a normal color. How can I achieve this same thing in Windows? I've seen a few solutions suggested, but they rely on calling functions while the message is printing, which I don't think would work for me on account of the fact that the ps variables just call __repr__ on whatever is assigned to them, right?

这一次诀窍从这里: python:在shell上显示耗用的时间) p>

(By the way, I got this time trick from here: python: display elapsed time on shell)

推荐答案

我发现没有特别的理由限制自己只在我的 PS1中找到当前时间类,事实上,为什么返回当前时间作为 __ repr __ 时,而是我只能打印时间和提示作为副作用 __ repr __ 函数,并返回一个空字符串?

It occurred to me that there's no particular reason to limit myself to only finding the current time in my PS1 class, and in fact, why return the current time as the __repr__ when instead I could just print the time and prompt as a side-effect of the __repr__ function and instead return an empty string?

所以我添加了下面的代码在 - 我离开那些出来,所以我可以显示在Windows上做这项工作的肉和土豆):

So I added the following code (with proper platform checks mixed in - I'm leaving those out just so I can show the meat and potatoes of making this work on Windows):

from ctypes import *
# ... Skipped a lot of code which is the same as before...
STD_OUTPUT_HANDLE_ID = c_ulong(0xfffffff5)
windll.Kernel32.GetStdHandle.restype = c_ulong
std_output_hdl = windll.Kernel32.GetStdHandle(STD_OUTPUT_HANDLE_ID)
textText    = 11
greenText   = 10
defaultText = 15
class PS1(object):
    def __repr__(self):
        # ... Skipping a lot of code which is the same as before ...
        windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, tealText)
        sys.stdout.write(clock)
        windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, greenText)
        sys.stdout.write(" >>> ")
        windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, defaultText)
        return ""

现在我得到了绿色的时钟和绿色的提示 - 我想强调这个工作!

So now I get the clock in teal and the prompt in green - I'd like to emphasis that THIS MUCH WORKED!

我尝试用PS2做类似的事情:

I tried to do a similar thing with PS2:

class PS2(object):
    def __repr__(self):
        windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, greenText)
        sys.stdout.write("         ... ")
        windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, defaultText)
        return ""

这个没有工作!解释器将立即打印出 PS1 PS2 ,然后不显示 PS2 在后面的行。看起来它通常在开始时获取所有的 PS#__repr __ s,并存储结果以后显示。但是因为这种方法依赖于副作用,它被暴露为黑客。

THIS DID NOT WORK! When I tried doing this, I found that the interpreter would instantly print out PS1 and PS2 back to back and then not display PS2 on subsequent lines. It would seem that it normally gets all of the PS# __repr__s at the very start and stores the results to display later. But since this method relies on side effects, it's exposed as the hack that it is.

所以现在我只是一个普通的 ... for sys.ps2

So for now I'm sticking with just a normal " ... " for sys.ps2.

听到建议让这些 ... 的绿色(没有也做任何我键入绿色),但我怀疑,这可能是不可能的。我很乐意接受任何答案,证明我错了 - 如果2天内没有答案,我可能只接受这一个,直到有人提出更好的东西。

I'd love to hear suggestions for making those ...'s in green (without also making anything I type green) but I suspect that it may not be possible. I'll be happy to accept any answer which proves me wrong - if none arrives within 2 days I'll probably just accept this one until someone else comes up with something better.

这篇关于Windows中的彩色Python提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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