Skip to content

Commit

Permalink
Changes in _hydro_pwd and _hydro_prompt
Browse files Browse the repository at this point in the history
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 2, 2024
1 parent eb80b44 commit b0ea451
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,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 @@ -148,4 +147,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

0 comments on commit b0ea451

Please sign in to comment.