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

Prevent hostname from showing up in local X sessions #398

Merged
merged 4 commits into from
May 13, 2018

Conversation

mafredri
Copy link
Collaborator

This change prevents the hostname from showing up when a terminal is opened inside a local X session (displayed as (:1)).

Fixes a regression introduced in #393.

@mafredri mafredri changed the title Prevent hostname from showing up in loal X sessions Prevent hostname from showing up in local X sessions Apr 29, 2018
@open-dynaMIX
Copy link

I was just writing an issue for that regression, then I saw this PR. I'd like to add, that on my system (up-to-date Arch Linux with who (GNU coreutils) 8.29) I get the following:

❯ who am i
myusername    pts/1        2018-04-30 19:25 (f)

This will not be handeled by grep -v '(:[0-9]*)$'.

@mafredri
Copy link
Collaborator Author

Interesting, thanks for reporting, what does f mean?

@open-dynaMIX
Copy link

Unfortunately I don't know. The only thing that comes to mind is, that it's the first character of my hostname and username.

@open-dynaMIX
Copy link

Just realized: This happens in guake and in xterm. In termite who am i does not even output anything. But I guess that could be an issue unrelated to pure...

@mafredri
Copy link
Collaborator Author

I notice tmux also causes false positives on Linux ((tmux(PID).%0). I wouldn't want to put too much logic into this, but essentially we want to trigger this condition when parens contain:

  • IPv4 address
  • IPv6 address
  • Remote hostname

It seems the coreutils implementation of who has a flag, --ips, which changes the output to exclude parens and just shows the IP at the end. This could be useful, but is not cross platform, which in turn means we need to detect the flavor of who.

Unless I can find a nice solution, might be I have to revert this feature :(.

@open-dynaMIX
Copy link

On my Arch system with who (GNU coreutils) 8.29 --ips is not recognized, but on one of my debian VMs with who (GNU coreutils) 8.26 --ips it is...

This change prevents the hostname from showing up when a terminal is
opened inside a local X session (displayed as (:1)).

Fixes a regression introduced in sindresorhus#393.
First, we switch from `who am i` to `who -m` because this is the POSIX
way and macOS supports it as well.

Second, if the above fails, we fall back to using just `who` and grep
the line that represents our TTY. This works e.g. with busybox systems.

Finally, we use (extremely) simplified regular expressions for detecting
IPv4, IPv6 and hostname. This is a best-effort attempt at detecting the
remote connections when SSH_CONNECTION is not set.
@mafredri
Copy link
Collaborator Author

mafredri commented May 6, 2018

Ok, so I went back to the drawing board with this one. @open-dynaMIX would you be willing to try this out?

I rebased this on the latest master, but the changes here are essentially:

Implement a more robust remote connection detection (who) (1283d83)

First, we switch from who am i to who -m because this is the POSIX way and macOS supports it as well.

Second, if the above fails, we fall back to using just who and grep the line that represents our TTY. This works e.g. with busybox systems.

Finally, we use (extremely) simplified regular expressions for detecting IPv4, IPv6 and hostname. This is a best-effort attempt at detecting the remote connections when SSH_CONNECTION is not set.

Inherit SSH connection detection inside tmux or other subshells (c801cfa)

@open-dynaMIX
Copy link

Nice! Unfortunately just yesterday I killed my last remote host, where I had zsh and pure installed, so I was only able to try it with SSH into localhost (from ::1 and from 127.0.0.1). It detects the SSH session correctly in both cases.

Thank you for your work on this!

This will also help with debugging if a user ever runs into a problem
where the user/host is shown when it should not be:

	# typeset -p PROMPT_PURE_SSH_CONNECTION
	export PROMPT_PURE_SSH_CONNECTION='(::1)'
@mafredri mafredri merged commit 2e48793 into sindresorhus:master May 13, 2018
@mafredri mafredri deleted the fix-ssh-X-detection branch May 13, 2018 18:58
@mafredri
Copy link
Collaborator Author

Thanks for testing @open-dynaMIX! This has now landed on master.

kgrz added a commit to kgrz/pure that referenced this pull request Sep 19, 2018
* upstream/master: (21 commits)
  Avoid calling zle reset-prompt in precmd (sindresorhus#431)
  Simplify async tasks by not passing $PWD (sindresorhus#430)
  Abort git check if pwd has changed after invocation (sindresorhus#428)
  Update zsh-async to 1.7.0 (sindresorhus#429)
  1.8.0
  Advice against enabling incompatible Oh-My-Zsh plugins
  Show warning when Oh My Zsh themes are enabled (sindresorhus#426)
  Add support for VI-mode indicator (sindresorhus#405)
  Remove unused function for computing string length (sindresorhus#418)
  Update fpath-reference link (sindresorhus#417)
  Prevent IPv6 regexp from capturing the time (sindresorhus#413)
  Prevent hostname from showing up in local X sessions (sindresorhus#398)
  Make sure local HUP trap is unset during git fetch
  Improve the debug prompt (PS4) (sindresorhus#396)
  Show options as a table in readme (sindresorhus#407)
  Remove unfrequent FAQs from readme (sindresorhus#406)
  Prevent interactive prompts during git fetch (sindresorhus#397)
  Set title via atomic print statement (sindresorhus#399)
  Revert local prompt_opts, breaks promptinit
  Fix for setopt not taking effect when sourcing pure
  ...
filipekiss added a commit to filipekiss/pure that referenced this pull request Apr 26, 2019
* upstream/master:
  Add pure-pwsh to the ports section of the readme (sindresorhus#467)
  Skip grep fork, use native zsh matching (sindresorhus#459)
  Add pure-now to Ports section in the readme (sindresorhus#458)
  1.9.0
  Update to zsh-async 1.7.1 and recover from unexpected worker death (sindresorhus#454)
  Add conda environment name to precmd (sindresorhus#440)
  Add Mímir to Ports section in the readme (sindresorhus#438)
  Avoid calling zle reset-prompt in precmd (sindresorhus#431)
  Simplify async tasks by not passing $PWD (sindresorhus#430)
  Abort git check if pwd has changed after invocation (sindresorhus#428)
  Update zsh-async to 1.7.0 (sindresorhus#429)
  1.8.0
  Advice against enabling incompatible Oh-My-Zsh plugins
  Show warning when Oh My Zsh themes are enabled (sindresorhus#426)
  Add support for VI-mode indicator (sindresorhus#405)
  Remove unused function for computing string length (sindresorhus#418)
  Update fpath-reference link (sindresorhus#417)
  Prevent IPv6 regexp from capturing the time (sindresorhus#413)
  Prevent hostname from showing up in local X sessions (sindresorhus#398)
  Make sure local HUP trap is unset during git fetch
  Improve the debug prompt (PS4) (sindresorhus#396)
  Show options as a table in readme (sindresorhus#407)
  Remove unfrequent FAQs from readme (sindresorhus#406)
  Prevent interactive prompts during git fetch (sindresorhus#397)
  Set title via atomic print statement (sindresorhus#399)
  Revert local prompt_opts, breaks promptinit
  Fix for setopt not taking effect when sourcing pure
  Fix prompt_pure_state on older versions of zsh
  Fix wrong placement of localoptions during prompt init
  Fix line erasure when terminal output does not end in newline (sindresorhus#391)
  Try to detech SSH connection when SSH_CONNECTION is unset (sindresorhus#393)
  Always force BatchMode for the Git SSH command (sindresorhus#392)
  1.7.0
  Avoid setting title over serial console (sindresorhus#388)
  Hide virtualenv when explicitly disabled by the user (sindresorhus#381)
  Add install guide for Zplugin (sindresorhus#386)
  Update URL to Droid Sans Mono font (sindresorhus#387)
  Prevent multiple prompt resets in one execution cycle (sindresorhus#368)
  More thorough handling (hiding) of match results
  Avoid implicit creation of global var prompt_pure_git_arrows
  1.6.0
  Link to pure.zsh and async.zsh for better clarity (sindresorhus#358)
  Readme tweaks
  Link to a Pure-inspired prompt done in Rust
  Avoid implicit global var creation and cleanup (sindresorhus#347)
kutsan pushed a commit to kutsan/pure that referenced this pull request Jun 19, 2023
* Prevent hostname from showing up in local X sessions

This change prevents the hostname from showing up when a terminal is
opened inside a local X session (displayed as (:1)).

Fixes a regression introduced in sindresorhus#393.

* Implement a more robust remote connection detection (who)

First, we switch from `who am i` to `who -m` because this is the POSIX
way and macOS supports it as well.

Second, if the above fails, we fall back to using just `who` and grep
the line that represents our TTY. This works e.g. with busybox systems.

Finally, we use (extremely) simplified regular expressions for detecting
IPv4, IPv6 and hostname. This is a best-effort attempt at detecting the
remote connections when SSH_CONNECTION is not set.

* Inherit SSH connection detection inside tmux or other subshells

* Store match and use upper case export for consistency

This will also help with debugging if a user ever runs into a problem
where the user/host is shown when it should not be:

	# typeset -p PROMPT_PURE_SSH_CONNECTION
	export PROMPT_PURE_SSH_CONNECTION='(::1)'
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