读取当前文本颜色的的xterm [英] Read the current text color in a xterm

查看:268
本文介绍了读取当前文本颜色的的xterm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写各种实用程序,以及我真的很喜欢彩色文本。没有什么特别的,只是使用转义序列。我创建了一个具有pprint(味精,颜色)函数的简单类。我知道了这里找到codeS 工作后比较容易。

这是我遇到的问题是,我应该能够打印后关闭色彩。例如,假设一个用户运行我的程序在默认终端颜色打印几乎所有的东西,但有一个错误,我想打印错误红色。我preFIX我的错误消息,'\\ 033 [0;32米,消息为红色。不幸的是,直到我改变这一切的文字为红色。这是一般没事,而我的程序正在运行,因为我知道这些消息应该是什么颜色。然而,颜色保持我的程序结束后,。基本上,我想我的程序启动时读取当前的颜色,并在完成时恢复它。同样的方式,脚本退出时恢复PWD。

我如何读取当前的转义序列?

系统:
红帽5.x的
巴什
在Python 2.3

感谢您的帮助。


解决方案

我不相信这是可能的,它的的不可能是便携式如果它是。你能做的最好是发送 sgr0 复位所有的属性设置为默认(未previous)。上的xterm, sgr0 Esc键[M 。如果要重置的颜色,并不会影响到其他属性,送其上的xterm是 Esc键[39;49米

这些codeS应该不难codeD。您应该使用的terminfo,termcap中或[n]的诅咒。

I'm writing various utilities, and I'm really liking colorized text. Nothing fancy, just using escape sequences. I've created a simple class that has a pprint(msg, color) function. I've got it working rather easily after finding the codes here.

The problem that I'm having is that I should be able to turn off the color after printing. For example, let's say a user runs my program that prints almost everything in the default terminal color, but there is an error, and I want to print the error in red. I prefix my error message with '\033[0;32m', and the message is in red. Unfortunately, all text is red until I change it. That's generally fine while my program is running because I know what color the messages should be. However, the color remains after my program ends. Basically, I'd like to read the current color when my program starts, and restore it when finished. The same way that scripts restore the pwd when exiting.

How do I read the current escape sequence?

System: Red Hat 5.x Bash Python 2.3

Thanks for the help.

解决方案

I don't believe that's possible and it's unlikely to be portable if it were. The best you can do is send sgr0 which resets all attributes to default (not previous). On xterms, sgr0 is Esc[m. If you want to reset the colors and not affect other attributes, send op which on xterms is Esc[39;49m.

These codes should not be hardcoded. You should use terminfo, termcap or [n]curses.

这篇关于读取当前文本颜色的的xterm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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