diff --git a/README.md b/README.md index 6731b13..04b6278 100644 --- a/README.md +++ b/README.md @@ -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` | diff --git a/conf.d/hydro.fish b/conf.d/hydro.fish index 39bf3a8..047d17b 100644 --- a/conf.d/hydro.fish +++ b/conf.d/hydro.fish @@ -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 @@ -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 ( @@ -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