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

Fix bugs in _hydro_pwd #46

Open
wants to merge 2 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Modify variables using `set --universal` from the command line or `set --global`

| Variable | Type | Description | Default |
| ------------------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------ | ------- |
| `fish_prompt_pwd_dir_length` | numeric | The number of characters to display when path shortening. Set it to `0` to display only the topmost (current) directory. | `1` |
| `hydro_pwd_dir_length` | numeric | The number of characters to display when path shortening. Set it to `0` to display only the topmost (current) directory. | `1` |
| `hydro_ignored_git_paths` | strings | Space separated list of paths where no git info should be displayed. | `""` |
| `hydro_cmd_duration_threshold` | numeric | Minimum command duration, in milliseconds, after which command duration is displayed. | `1000` |

Expand Down
54 changes: 34 additions & 20 deletions conf.d/hydro.fish
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@ function $_hydro_git --on-variable $_hydro_git
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 --local path_sep /
function _hydro_pretty_path
string replace --regex --all -- "(\.?[^/]{$hydro_pwd_dir_length})[^/]*/" '$1/' $argv[1] |
string replace --regex -- '([^/]+)$' "\x1b[1m\$1\x1b[22m" |
string replace --regex --all -- '(?!^/$)/|^$' "\x1b[2m/\x1b[22m"
end

test "$fish_prompt_pwd_dir_length" = 0 && set path_sep
function _hydro_pwd --on-variable PWD --on-variable hydro_ignored_git_paths --on-variable hydro_pwd_dir_length
if test "$hydro_pwd_dir_length" = 0
set --global _hydro_pwd (path basename $PWD)
return
end

set --local dir (string replace --regex -- "^$(string escape --style=regex -- ~)" '~' $PWD)

if set --query git_root[1] && ! contains -- $git_root $hydro_ignored_git_paths
set --erase _hydro_skip_git_prompt
if ! set --query _hydro_git_root[1]
set --global _hydro_pwd (_hydro_pretty_path $dir)
else
set --global _hydro_skip_git_prompt
end
set --local git_dir (string replace --regex -- "^$(string escape --style=regex -- ~)" '~' $_hydro_git_root)
set --local after_git (string replace -- "$git_dir" "" "$dir")

set --global _hydro_pwd (
string replace --ignore-case -- ~ \~ $PWD |
string replace -- "/$git_base/" /:/ |
string replace --regex --all -- "(\.?[^/]{"(
string replace --regex --all -- '^$' 1 "$fish_prompt_pwd_dir_length"
)"})[^/]*/" "\$1$path_sep" |
string replace -- : "$git_base" |
string replace --regex -- '([^/]+)$' "\x1b[1m\$1\x1b[22m" |
string replace --regex --all -- '(?!^/$)/|^$' "\x1b[2m/\x1b[22m"
)
if test -z $after_git
set --global _hydro_pwd (_hydro_pretty_path $dir)
else
set --global _hydro_pwd "$(_hydro_pretty_path $git_dir)$(_hydro_pretty_path $after_git)"
end
end
end

function _hydro_postexec --on-event fish_postexec
Expand Down Expand Up @@ -63,7 +66,17 @@ function _hydro_prompt --on-event fish_prompt

command kill $_hydro_last_pid 2>/dev/null

set --query _hydro_skip_git_prompt && set $_hydro_git && return
set --local git_root (command git --no-optional-locks rev-parse --show-toplevel 2>/dev/null)

if test "$git_root" != "$_hydro_git_root"
set --global _hydro_git_root $git_root
_hydro_pwd
end

if ! set --query _hydro_git_root[1] || contains -- "$_hydro_git_root" $hydro_ignored_git_paths
set $_hydro_git
return
end

fish --private --command "
set branch (
Expand Down Expand Up @@ -135,4 +148,5 @@ set --query hydro_symbol_git_dirty || set --global hydro_symbol_git_dirty •
set --query hydro_symbol_git_ahead || set --global hydro_symbol_git_ahead ↑
set --query hydro_symbol_git_behind || set --global hydro_symbol_git_behind ↓
set --query hydro_multiline || set --global hydro_multiline false
set --query hydro_pwd_dir_length || set --global hydro_pwd_dir_length 1
set --query hydro_cmd_duration_threshold || set --global hydro_cmd_duration_threshold 1000