Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce add-ons #37

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions conf.d/hydro.fish
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
status is-interactive || exit

set --global _hydro_git _hydro_git_$fish_pid
set --global _hydro_addons _hydro_items_$fish_pid

function $_hydro_git --on-variable $_hydro_git
function $_hydro_git --on-variable $_hydro_git --on-variable $_hydro_addons
commandline --function repaint
end

function _hydro_pwd --on-variable PWD --on-variable hydro_ignored_git_paths --on-variable fish_prompt_pwd_dir_length
set --local git_root (command git --no-optional-locks rev-parse --show-toplevel 2>/dev/null)
set --local git_base (string replace --all --regex -- "^.*/" "" "$git_root")
set --global _hydro_git_root (command git --no-optional-locks rev-parse --show-toplevel 2>/dev/null)
set --local git_base (string replace --all --regex -- "^.*/" "" "$_hydro_git_root")
set --local path_sep /

set --query _hydro_git_root[1] &&
contains -- $_hydro_git_root $hydro_ignored_git_paths && set _hydro_git_root

test "$fish_prompt_pwd_dir_length" = 0 && set path_sep

if set --query git_root[1] && ! contains -- $git_root $hydro_ignored_git_paths
set --erase _hydro_skip_git_prompt
else
set --global _hydro_skip_git_prompt
end

set --global _hydro_pwd (
string replace --ignore-case -- ~ \~ $PWD |
string replace -- "/$git_base/" /:/ |
Expand Down Expand Up @@ -61,11 +59,15 @@ function _hydro_prompt --on-event fish_prompt
set --query _hydro_status || set --global _hydro_status "$_hydro_newline$_hydro_color_prompt$hydro_symbol_prompt"
set --query _hydro_pwd || _hydro_pwd

command kill $_hydro_last_pid 2>/dev/null
set --local addons _hydro_addon_{$hydro_prompt_addons}\;

set --query _hydro_skip_git_prompt && set $_hydro_git && return
command kill $_hydro_last_pid 2>/dev/null

fish --private --command "
Copy link

@simontong simontong Jul 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we are defining the prompt in private mode, it doesn't appear possible to conditionally display a character in the prompt if we are in private mode. Could we introduce an additional global variable (something like is_fish_private_mode) that gets set just before we enter private mode in the code above? Then we can have the following addon in the prompt:

set --global hydro_prompt_addons private

function _hydro_addon_private
  if test "$is_fish_private_mode" = 1
    echo [p]
  end
end

The above would result in the prompt:

/home/johnsmith/abc > fish -P
/home/johnsmith/abc [p] >

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may not even need --private mode to be honest! 🤔

Copy link
Collaborator

@mattmc3 mattmc3 Jul 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to change anything in Hydro to handle this particular addon example. In your config.fish, you can just check that the shell is interactive so that subshells that run your config (like those Hydro creates) don't nuke your flag. This code will do what you want with the current implementation:

# config.fish
status --is-interactive && set --export is_fish_private_mode $fish_private_mode
function _hydro_addon_private
    if test "$is_fish_private_mode" = 1
        echo [p]
    end
end
set --global hydro_prompt_addons private

I like having Hydro use --private so it doesn't clutter your fish history with all the non-user git commands it fires off, but that's not a huge deal either way. But, if this is work-around is acceptable, we can leave --private in and everyone wins.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I concur!

set --universal $_hydro_addons ($addons) \"\"

test -z \"$_hydro_git_root\" && set $_hydro_git && exit

set branch (
command git symbolic-ref --short HEAD 2>/dev/null ||
command git describe --tags --exact-match HEAD 2>/dev/null ||
Expand Down Expand Up @@ -102,7 +104,7 @@ function _hydro_prompt --on-event fish_prompt
end

function _hydro_fish_exit --on-event fish_exit
set --erase $_hydro_git
set --erase $_hydro_git $_hydro_addons
end

function _hydro_uninstall --on-event hydro_uninstall
Expand All @@ -112,8 +114,6 @@ function _hydro_uninstall --on-event hydro_uninstall
functions --erase (functions --all | string match --entire --regex "^_?hydro_")
end

set --global hydro_color_normal (set_color normal)

for color in hydro_color_{pwd,git,error,prompt,duration}
function $color --on-variable $color --inherit-variable color
set --query $color && set --global _$color (set_color $$color)
Expand Down
2 changes: 1 addition & 1 deletion functions/fish_prompt.fish
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function fish_prompt --description Hydro
echo -e "$_hydro_color_pwd$_hydro_pwd$hydro_color_normal $_hydro_color_git$$_hydro_git$hydro_color_normal$_hydro_color_duration$_hydro_cmd_duration$hydro_color_normal$_hydro_status$hydro_color_normal "
echo -e "$_hydro_color_pwd$_hydro_pwd\x1b[0m $_hydro_color_git$$_hydro_git\x1b[0m$_hydro_color_duration$_hydro_cmd_duration\x1b[0m$$_hydro_addons$_hydro_status\x1b[0m "
end