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 1 commit
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
Prev Previous commit
Changes in _hydro_pwd and _hydro_prompt
These changes will make it so `_hydro_pwd` will run only when needed.

Before this, `_hydro_pwd` would always run, not just when pwd changes
beacuse of `--on-variable fish_prompt_pwd_dir_length`.

Now the `hydro_pwd_dir_length` is used instead.

The part about finding git root in `_hydro_pwd` we do want to run every
time, so it is moved to `_hydro_prompt`. Otherwise things break when
initializing and deleting git repos.
  • Loading branch information
blt-r committed Nov 3, 2024
commit 6533e02f061b19f286546a9950764ce52a31fb24
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
34 changes: 17 additions & 17 deletions conf.d/hydro.fish
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,23 @@ function $_hydro_git --on-variable $_hydro_git
end

function _hydro_pretty_path
set --local dir_len "$fish_prompt_pwd_dir_length"
test -z $dir_len && set dir_len 1

string replace --regex --all -- "(\.?[^/]{$dir_len})[^/]*/" '$1/' $argv[1] |
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

function _hydro_pwd --on-variable PWD --on-variable hydro_ignored_git_paths --on-variable fish_prompt_pwd_dir_length
if test "$fish_prompt_pwd_dir_length" = 0
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 git_root (command git --no-optional-locks rev-parse --show-toplevel 2>/dev/null)

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 --local dir (string replace --regex -- "^$(string escape --style=regex -- ~)" '~' $PWD)

if test -z $git_root
if ! set --query _hydro_git_root[1]
set --global _hydro_pwd (_hydro_pretty_path $dir)
else
set --local git_dir (string replace --regex -- "^$(string escape --style=regex -- ~)" '~' $git_root)
set --local git_dir (string replace --regex -- "^$(string escape --style=regex -- ~)" '~' $_hydro_git_root)
set --local after_git (string replace -- "$git_dir" "" "$dir")

if test -z $after_git
Expand Down Expand Up @@ -77,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 @@ -149,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