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

Conversation

blt-r
Copy link

@blt-r blt-r commented Apr 17, 2023

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~.

(Sorry I didn't make a commit per bug fix, probably would have been more readable that way)

@blt-r
Copy link
Author

blt-r commented Apr 18, 2023

Also, it runs for every single prompt (or even twice per prompt?) instead of only when $PWD is changing. This is caused by --on-variable fish_prompt_pwd_dir_length. I know the standard prompt_pwd is setting the $fish_prompt_pwd_dir_length, and there is probably something else in fish that sets it.

I would recommend not dealing with $fish_prompt_pwd_dir_length and instead have something like $hydro_pwd_dir_length

@blt-r
Copy link
Author

blt-r commented Nov 2, 2024

Just added here another commit that I've also been using for past 8 months.

(see commit message for details)

For me, these two commits fixed the small annoyances I had before.

blt-r added 2 commits November 3, 2024 21:01
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~`.
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.
@maciej-lech
Copy link

Hi. Why this is not yet merged?

@jorgebucaran
Copy link
Owner

Since testing the code thoroughly would require significant effort and Hydro works well for me, I just haven't had the time to research this further. If you have the opportunity, we'd appreciate your feedback on this PR—even just to help add weight toward it being merged, hopefully soon.

@blt-r
Copy link
Author

blt-r commented Jan 25, 2025

So, having --on-variable fish_prompt_pwd_dir_length from _hydro_pwd causes the function to run unnecessarily.

To see this, you can add "echo hydro pwd!" in the beginning of _hydro_pwd. Currently, for me, it results in:

hydro pwd!
~ ❯ hydro pwd!
~ ❯
hydro pwd!
~ ❯ hydro pwd!
~ ❯
hydro pwd!
~ ❯ hydro pwd!
~cd Documents
hydro pwd!
hydro pwd!
hydro pwd!
~/Documents ❯ hydro pwd!
~/Documents ❯
hydro pwd!
~/Documents ❯ hydro pwd!
~/Documents ❯ ..
hydro pwd!
hydro pwd!
hydro pwd!
~ ❯ hydro pwd!
~

So it just runs twice per prompt for no reason and one more time when pwd is actually changed. If I then remove --on-variable fish_prompt_pwd_dir_length, I will see the following:

~~~~cd Documents
hydro pwd!
~/Documents ❯
~/Documents ❯
~/Documents ❯
~/Documents ❯ ..
hydro pwd!
~~~

Which is what is expected. So I removed --on-variable fish_prompt_pwd_dir_length from _hydro_pwd.

Except finding the git root is done in _hydro_pwd, which is wrong, since user can init/deinit git repos, which will change the git root without changing the pwd. Really, finding the git root should be happening in _hydro_postexec, so I moved it there. You just never noticed that this is wrong because _hydro_pwd is running on every prompt anyway.

Since _hydro_pwd doesn't depend on fish_prompt_pwd_dir_length, I changed hydro to use a new config variable hydro_pwd_dir_length, which is a breaking change.

It is probably a good idea to make hydro_pwd_dir_length default to the value of fish_prompt_pwd_dir_length to maintain backwards compatibility, but I thought of that only just now.

While I was figuring all this out, I also cleaned up how the pwd string is actually computed, because the current implementation is hacky and more complicated than it needs to be. The main issue I encountered with it is this bug from original PR comment:

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.

To simplify this process of constructing the pwd string, I factored it into a _hydro_pretty_path function, which just takes a normal path and does all the prettifying.

These two changes should ideally have been separate commits (or PRs), but they are weirdly separated into two commits, because I was doing them in parallel.

If you're interested, I can restructure commits in a way for them to make sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants