如何从OMF主题中覆盖fish_prompt函数

发布于 2025-01-25 20:28:26 字数 285 浏览 4 评论 0原文

我浪费了一些时间,所以我创建了这个Q& a。

我正在使用一个OMF主题,我想改变主题提示。不幸的是,我想做的更改是不可能通过设置主题配置变量来实现的。

我尝试使用funced Fish_prompt编辑fish_prompt函数; funcsave fish_prompt,但是如果我这样做,则不再加载主题,因此我无法使用主题中定义的功能。如果我只在我的config.fish中创建fish_prompt函数,也会发生同样的情况。

I lost some time on it so I'm creating this Q&A.

I'm using an omf theme and I would like to make a change in the theme prompt. Unfortunately, the change I want to do is not possible by setting the theme config variables.

I tried to edit fish_prompt function using funced fish_prompt; funcsave fish_prompt but if I do so the theme is not loaded anymore so I can't use the functions defined within the theme. The same happens if I just create a fish_prompt function in my config.fish.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

溺深海 2025-02-01 20:28:26

时, tldr

在定义自己的fish_prompt函数

source $OMF_PATH/init.fish

# Read current theme
test -f $OMF_CONFIG/theme
  and read -l theme < $OMF_CONFIG/theme
  or set -l theme default

set -l theme_functions_path {$OMF_CONFIG,$OMF_PATH}/themes*/$theme/functions/
for conf in $theme_functions_path/*.fish
  source $conf
end

说明

添加这些行添加这些行,当加载OMF时,它将主题函数文件添加到$ fish_function_path_path。 ( source code

根据 fish document

当鱼需要加载功能时,它会通过列表变量中的任何目录进行搜索,$ fish_function_path,用于包含函数名称加上后缀的名称的文件。鱼和第一个找到的fish。

文档没有明确说的是是否已经定义了该函数(例如,在Config.Fish中),它不会尝试从$ fish_function_path加载。

因此,问题是,当您创建自己的fish_prompt函数时,它会阴影.../&lt; theme&gt;/functions/fish_prompt.fish.fish.fish

您需要做的工作是在重新定义它之前强制加载主题功能文件。
例如:

# Read current theme
test -f $OMF_CONFIG/theme
  and read -l theme < $OMF_CONFIG/theme
  or set -l theme default

set -l theme_functions_path {$OMF_CONFIG,$OMF_PATH}/themes*/$theme/functions/fish_prompt.fish
for conf in $theme_functions_path
  source $conf
end

function fish_prompt
  # prompt_theme_foo
  # prompt_theme_bar
end

请确保在OMF init.fish加载后将其运行。
您可以通过手动来源来确保,源$ omf_path/init.fish,或确保omf.fish.fish的字母顺序比您的文件要小。

tldr

add these lines before you define your own fish_prompt function

source $OMF_PATH/init.fish

# Read current theme
test -f $OMF_CONFIG/theme
  and read -l theme < $OMF_CONFIG/theme
  or set -l theme default

set -l theme_functions_path {$OMF_CONFIG,$OMF_PATH}/themes*/$theme/functions/
for conf in $theme_functions_path/*.fish
  source $conf
end

explanation

When omf is loaded, it add the theme functions files to the $fish_function_path. (source code)

According to fish documentation

When fish needs to load a function, it searches through any directories in the list variable $fish_function_path for a file with a name consisting of the name of the function plus the suffix .fish and loads the first it finds.

What the documentation doesn't explicitly say is if the function is already defined (in config.fish, for example) it will not try to load from $fish_function_path.

So the issue is, when you creates your own fish_prompt function, it shadows the .../<theme>/functions/fish_prompt.fish.

What you need to do to work around it is force loading the theme function file before you redefine it.
For example:

# Read current theme
test -f $OMF_CONFIG/theme
  and read -l theme < $OMF_CONFIG/theme
  or set -l theme default

set -l theme_functions_path {$OMF_CONFIG,$OMF_PATH}/themes*/$theme/functions/fish_prompt.fish
for conf in $theme_functions_path
  source $conf
end

function fish_prompt
  # prompt_theme_foo
  # prompt_theme_bar
end

Be sure to run it after omf init.fish is loaded.
You can assure by sourcing omf manually source $OMF_PATH/init.fish or be sure omf.fish is in an alphabetical order lesser than your file.

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