From a33f52aa94d0bc57e79cd7852735122de0486bee Mon Sep 17 00:00:00 2001 From: blt__ Date: Mon, 17 Apr 2023 19:38:12 +0400 Subject: [PATCH 1/2] Fix bugs in _hydro_pwd There were a couple of bugs in the implementation of _hydro_pwd which caused path to be rendered incorrectly: - if there is a parent directory named the same way as git root, it will be "highlighted" instead of the actual git root. For example, if you are in `/foo/repo/bar/repo/baz` and the second `repo` is git root, the first one will be highlighted. - if you are in directory named `:` (but not in git repo) it will not be shown. For example, if you are in `/tmp/:` you will see `/t/` instead of `/t/:`. - if one of your parent directories is `:` and you are in git repo, the name of the repo will be shown instead of `:`. For example, if you are in `/foo/:/bar/repo/baz` you will see `/f/repo/b/r/baz` instead of `/f/:/b/repo/baz`. - if `fish_prompt_pwd_dir_length` is set to `0` and one of the parent directories starts with dot, the directory shown will start with dot. For example, if you are in `/home/me/.config/fish` you will see `.fish` instead of `fish`. - if you are in a directory that looks like your home directory but isn't, it will be replaced with `~`. For example, if you are in `/tmp/home/me` you will see `/tmp~`. --- conf.d/hydro.fish | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/conf.d/hydro.fish b/conf.d/hydro.fish index 39bf3a8..721fc1b 100644 --- a/conf.d/hydro.fish +++ b/conf.d/hydro.fish @@ -6,29 +6,43 @@ function $_hydro_git --on-variable $_hydro_git commandline --function repaint 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 -- '([^/]+)$' "\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 - 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 / + if test "$fish_prompt_pwd_dir_length" = 0 + set --global _hydro_pwd (path basename $PWD) + return + end - test "$fish_prompt_pwd_dir_length" = 0 && set path_sep + 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 + 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/" /:/ | - 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" - ) + set --local dir (string replace --regex -- "^$(string escape --style=regex -- ~)" '~' $PWD) + + if test -z $git_root + set --global _hydro_pwd (_hydro_pretty_path $dir) + else + set --local git_dir (string replace --regex -- "^$(string escape --style=regex -- ~)" '~' $git_root) + set --local after_git (string replace -- "$git_dir" "" "$dir") + + 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 From 6533e02f061b19f286546a9950764ce52a31fb24 Mon Sep 17 00:00:00 2001 From: blt__ Date: Sun, 31 Mar 2024 14:31:30 +0400 Subject: [PATCH 2/2] 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. --- README.md | 2 +- conf.d/hydro.fish | 34 +++++++++++++++++----------------- 2 files changed, 18 insertions(+), 18 deletions(-) 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 721fc1b..047d17b 100644 --- a/conf.d/hydro.fish +++ b/conf.d/hydro.fish @@ -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 @@ -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 ( @@ -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