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

DRYer: Get hostname #5712

Merged
merged 1 commit into from
Nov 26, 2022
Merged

DRYer: Get hostname #5712

merged 1 commit into from
Nov 26, 2022

Conversation

page-down
Copy link
Contributor

The cached hostname cannot be used in open_url as it may have been changed.


About launching with current cwd, I found an issue.

kitty --config=NONE -d=/tmp -o 'shell zsh' -o 'map f1 new_window_with_cwd'

ssh ksi-enabled-remote
# Ctrl+D
# F1

After ssh disconnects, create a new window with the current cwd, which will then use the remote cwd.
The remote path probably does not exist locally and the shell starts at /, which is annoying.

I can think of a few ways to improve this user experience.

Check if the hostname matches the local hostname before return reported_cwd.

def hostname_from_osc7_url(url: str) -> str:
    netloc = ''
    if url.startswith('kitty-shell-cwd://'):
        netloc = url.split('/', 3)[2]
    if url.startswith('file://'):
        from urllib.parse import urlparse
        netloc = urlparse(url).netloc
    return netloc.partition(':')[0]

Provide more information when reporting cwd in shell integration. For example:

# local
kitty-shell-cwd://host/path#kitty-pid=$KITTY_PID
# ssh kitten
kitty-shell-cwd://host/path#kitty-ssh-kitten=yes
# ssh (with shell integration)
kitty-shell-cwd://host/path
# ssh
file://host/path

Store to last_reported_local_cwd when the reported kitty-pid is the same as the current one when processing cwd notifications.
When window.child_is_remote is False and the last reported cwd explicitly has kitty-ssh-kitten or no kitty-pid, it falls back to the last recorded last_reported_local_cwd.

Outputting the current cwd in prompt would be more accurate, but repeated output of the same cwd is too wasteful of resources and not really worth it for me.

Do you have a better idea?

@kovidgoyal
Copy link
Owner

kovidgoyal commented Nov 26, 2022 via email

@page-down page-down deleted the hostname branch November 26, 2022 10:19
@page-down
Copy link
Contributor Author

I dont see how that's possible ... the local shell will draw a new prompt ...

Currently the new cwd is only reported when the cwd changes, at least for zsh and fish (the official fish one and the one that comes with kitty), which I think is good practice.

shell-integration/zsh/kitty-integration

chpwd_functions=(${chpwd_functions[@]} "_ksi_report_pwd")

shell-integration/bash/kitty.bash

# unfortunately bash provides no hooks to detect cwd changes
# in particular this means cwd reporting will not happen for a
# command like cd /test && cat.

Since the cwd is not changed after exiting ssh, the problem I mentioned will occur.

@kovidgoyal
Copy link
Owner

Then the way to do it is to store last_reported_local_cwd (when
is_remote returns false) and last_reported_remote_cwd.

Then, use the one that matches the current value of is_remote.

@kovidgoyal
Copy link
Owner

The downside to that is it will only work for ssh it wont work for instance if you another program that reports cwd. The only robust fix is to report on every prompt draw.

@kovidgoyal
Copy link
Owner

I have changed the zsh integration to report cwd at every new prompt draw. So now bash and zsh both do that. Could you do the same for fish please, as it will take me a lot of time to figure out the best way to do it for fish.

@page-down
Copy link
Contributor Author

Check if the hostname matches ...

To answer my own question: No, for example when connecting from one "localhost" to another "localhost" via IP, this will not work.

... when is_remote returns false ...

Need to check this every time CWD is received. This will be expensive.

The only robust fix is to report on every prompt draw.

Unfortunately, there doesn't seem to be a better way to get acceptable improvements at minimal cost.

... Could you do the same for fish ...

I will implement it in another PR.
In my personal configuration I will use no-cwd and just update the cwd asynchronously via events after the execution of programs like ssh.

This does not apply to normal users, as a list of programs needs to be maintained.
And the shell does not know the OSC7 sent by the executed program, so it also cannot automatically know that cwd needs to be updated again.

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.

2 participants