如何根据当前目录在 Konsole 中以编程方式更改颜色?

发布于 2024-08-19 13:02:05 字数 417 浏览 6 评论 0原文

我目前使用基于我正在工作的目录的配色方案。我手动打开 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2024-08-26 13:02:05

为了帮助您入门,这里有一些信息关于使用 D-Bus 编写 Konsole 脚本。

您没有说明您正在使用哪个 shell,但如果是 Bash,您可能需要使用 $PROMPT_COMMAND 变量,该变量保存每次发出 $PS1 提示符时要执行的命令。 Z shell 有类似的功能,但可能更强大一些(请参阅 man zshmisc 了解 chpwdprecmd)。

否则,您也许可以使用 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 (see man zshmisc for chpwd and precmd).

Otherwise, you might be able to use xterm escape sequences.

往事风中埋 2024-08-26 13:02:05

上下文

实际上,Konsole 支持他们的配置文件。配置文件是一组设置(不仅仅是背景),您可以在“设置”|“设置”下手动定义它们。管理个人资料及相关内容。

此外,还有一个名为 konsoleprofile 的命令行实用程序,它允许以编程方式更改配置文件。

实际答案

  1. 转到设置|编辑当前个人资料... |外观
  2. 为每个您想要有特殊背景的目录定义新的配色方案,例如 myprofile1、myprofile2、mystandard
  3. 确保您可以手动调用 konsoleprofile ColorScheme=myprofile1konsoleprofile ColorScheme=standard 等。
  4. 将对 konsoleprofile 的调用插入到您的 $PROMPT_COMMAND 中,例如将其添加到您的 .bashrc 中:
PROMPT_COMMAND='[[ "$PWD" = /home/me/src/java* ]] && konsoleprofile ColorScheme=myprofile1 || konsoleprofile ColorScheme=mystandard'";$PROMPT_COMMAND"

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

  1. Go to Settings | Edit Current Profile... | Appearance
  2. Define new Color Scheme for each of the directories you want to have special background for, e.g. myprofile1, myprofile2, mystandard
  3. Make sure you can manually call konsoleprofile ColorScheme=myprofile1, konsoleprofile ColorScheme=standard, etc.
  4. Plug in the calls to konsoleprofile into your $PROMPT_COMMAND, e.g. add this to your .bashrc:
PROMPT_COMMAND='[[ "$PWD" = /home/me/src/java* ]] && konsoleprofile ColorScheme=myprofile1 || konsoleprofile ColorScheme=mystandard'";$PROMPT_COMMAND"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文