如何根据当前目录在 Konsole 中以编程方式更改颜色?
我目前使用基于我正在工作的目录的配色方案。我手动打开 Konsole shell,然后 cd 进入目录并进入“设置”并更改配色方案。
我想做的是让 Konsole 根据我所在的目录自动设置其前景色和背景色。基本上,如果我位于 /home/me/src/java 下面的任何子目录中,那么我想使用文本白色,例如背景蓝色。例如,如果我位于 /home/me/src/documentation 下面,我想要文本黑色,背景白色。我希望当我调用“cd”命令时,以编程方式自动发生颜色变化。
这可能吗?如果是这样,您能给我一些指导吗?
在我看来,我需要能够做几件事:
- 能够检测 shell 进程 (bash) 正在哪个 Konsole 中运行。
- 能够告诉 Konsole(可能通过 API 调用)更改配色方案在飞行中。
I currently use a color scheme based on which directory that I'm working in. I manually open up a Konsole shell and then cd into a directory and got to Settings and change the color scheme.
What I would like to do is have Konsole automatically set its foreground and background colors based on which directory I'm in. Basically if I'm in any subdirectory below /home/me/src/java then I would like to use text white, background blue, for example. If I'm below /home/me/src/documentation I want text black, background white, for example. I would like the color change to occur automatically, programmatically, when I call the "cd" command.
Is this possible? If so, can you provide me some direction as to how?
The way I see it I will need to be able to do a couple of things:
- Be able to detect which Konsole the shell process (bash) is running in.
- Be able to tell Konsole, probably via API call, to change the color scheme on the fly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了帮助您入门,这里有一些信息关于使用 D-Bus 编写 Konsole 脚本。
您没有说明您正在使用哪个 shell,但如果是 Bash,您可能需要使用
$PROMPT_COMMAND
变量,该变量保存每次发出 $PS1 提示符时要执行的命令。 Z shell 有类似的功能,但可能更强大一些(请参阅man zshmisc
了解chpwd
和precmd
)。否则,您也许可以使用 xterm 转义序列。
To get you started, here's a little information about using D-Bus to script Konsole.
You don't say which shell you're using, but if it's Bash you may want to use the
$PROMPT_COMMAND
variable which holds a command to be executed each time the $PS1 prompt is issued. The Z shell has a similar facility that's probably a bit more powerful (seeman zshmisc
forchpwd
andprecmd
).Otherwise, you might be able to use xterm escape sequences.
上下文
实际上,Konsole 支持他们的配置文件。配置文件是一组设置(不仅仅是背景),您可以在“设置”|“设置”下手动定义它们。管理个人资料及相关内容。
此外,还有一个名为
konsoleprofile
的命令行实用程序,它允许以编程方式更改配置文件。实际答案
设置|编辑当前个人资料... |外观
konsoleprofile ColorScheme=myprofile1
、konsoleprofile ColorScheme=standard
等。konsoleprofile
的调用插入到您的$PROMPT_COMMAND
中,例如将其添加到您的.bashrc
中:Context
Actually, Konsole has support for what they profiles. A profile is a group of settings (not only background), which you can manually define under
Settings | Manage Profiles
and around.Also, there exists a command line utility called
konsoleprofile
which allows for programmatic changing of the profiles.Actual answer
Settings | Edit Current Profile... | Appearance
konsoleprofile ColorScheme=myprofile1
,konsoleprofile ColorScheme=standard
, etc.konsoleprofile
into your$PROMPT_COMMAND
, e.g. add this to your.bashrc
: