hyper icon indicating copy to clipboard operation
hyper copied to clipboard

Tab titles don't reflect current directory or process running

Open stereokai opened this issue 7 years ago • 47 comments

  • OS version and name: Windows 10 64bit
  • Hyper.app version: 1.0.0
  • Link of a Gist with the contents of your .hyper.js: hyper.js gist
  • Relevent information from devtools: N/A
  • The issue is reproducible in vanilla Hyper.app: Reproducible in vanilla

Issue

Current directory is not displayed. All tab titles display "Shell" regardless of which shell I am using (tried cmd and Bash on Ubuntu on Windows).

stereokai avatar Dec 13 '16 23:12 stereokai

This is true of macOS too.

audunolsen avatar Dec 14 '16 00:12 audunolsen

@audunolsen in version 1.0.0?

I assumed it was Windows only because of this notice: https://github.com/parro-it/hyperterm-title/issues/5

stereokai avatar Dec 14 '16 00:12 stereokai

Yes.

screenshot 2016-12-14 01 12 42

Tab titles doesn't reflect directory or process running.

audunolsen avatar Dec 14 '16 00:12 audunolsen

I stole your comment for a better issue title :)

stereokai avatar Dec 14 '16 00:12 stereokai

Hi, since my plugin hyperterm-title is now embedded in core, tabs title is now managed directly by your shell. There is a special escape sequence command that is meant to set terminal title.

You have to setup a shell prompt that uses this escape sequence in order to avoid "Shell" as tab title.

parro-it avatar Dec 14 '16 17:12 parro-it

Checkout pure prompt for zsh. I'm sure there are similar ones for bash or fish too

parro-it avatar Dec 14 '16 17:12 parro-it

I agree that a better default behaviour is useful... Show the Shell name should be simple to implement The cwd or current foreground process instead was not trivial, previous hyper versions uses a polling procedure that is too resource heavy...

parro-it avatar Dec 14 '16 17:12 parro-it

@parro-it thanks for chiming in! I appreciate your work on this plugin as it is a feature probably most users if not everyone take it for granted.

With that said, I think an ideal solution would be supporting all terminals, in particular now that Hyper is meant to be used on all major operating systems. So perhaps additional methods of retrieving the shell title are needed :)

stereokai avatar Dec 14 '16 17:12 stereokai

@parro-it you were quicker than me!

stereokai avatar Dec 14 '16 17:12 stereokai

@stereokai 😁 any idea on how we Can listen for cwd change or foreground process change without polling?

parro-it avatar Dec 14 '16 17:12 parro-it

For what's it worth, on Windows, Cmder is capable of pulling the cwd, it might be possible to learn how they do it. Although it is only relevant to Windows terminals (ie. CMD and PowerShell) - as it doesn't work with the native Bash on Windows cmder

stereokai avatar Dec 14 '16 18:12 stereokai

I found this issue there: https://github.com/cmderdev/cmder/issues/815. Seems they are using terminal escape codes too to set the title... but I'm not sure. /cc @samvasko @Stanzilla @MartiUK @Jackbennett

parro-it avatar Dec 14 '16 18:12 parro-it

This is my linux terminal, with two tabs running bash:

image

It display current shell user and name of the shell. I had to comment lines on my .bashrc that set the title using escape codes. The window title reflect that of the current tab.

I think this behavior could be acceptable for hyper too...

parro-it avatar Dec 14 '16 18:12 parro-it

@parro-it Excellent find!

Using this, I can confirm Hyper recognizes and picks up the titles from all shells on my machine, that is, PowerShell, cmd, and native Bash on Ubuntu on Windows.

I guess you deserve a huge applause because it's your code that's doing the magic ;)

Can you check if echo -ne '\e]0;HelloCmder\a' is working for you in your Linux shells? Then we could narrow it down to a single, cross-OS method

Edit: Would be better to just include the code from the link here: PowerShell:

$host.ui.RawUI.WindowTitle = "HelloCmder"

cmd:

title HelloCmder

bash:

echo -ne '\e]0;HelloCmder\a'

stereokai avatar Dec 14 '16 19:12 stereokai

Can you check if echo -ne '\e]0;HelloCmder\a' is working for you in your Linux shells? Then we could narrow it down to a single, cross-OS method

It work, that is exactly the escape code we use to set tab title on hyper :smile:. If you set your prompt to contains the same escape, you can get replace HelloCmder with your CWD, and you get cwd on the title:

image

The tab on the right is running bash without custom prompt, the tab on the left is running zsh with pure

parro-it avatar Dec 14 '16 19:12 parro-it

What do you mean technically by "set your prompt to contain the same escape"? Can you be more clear?

stereokai avatar Dec 14 '16 19:12 stereokai

Try to run this on bash:

PS1="\[\033]0;\w\007\]>"

That command set your prompt to an escape sequence that change the title to the cwd every time you run a command (and add a '>' to the prompt just for convenience)

Here are various code you can use in the prompt: http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/bash-prompt-escape-sequences.html

parro-it avatar Dec 14 '16 19:12 parro-it

@parro-it doesn't seem to work in Bash on Windows, but anyway - it won't be supported in cmd and PowerShell - so what's the use? It is a Linux-only infrastructure. Or maybe you can enlighten me :)

stereokai avatar Dec 14 '16 20:12 stereokai

@parro-it doesn't seem to work in Bash on Windows, but anyway - it won't be supported in cmd and PowerShell - so what's the use? It is a Linux-only infrastructure. Or maybe you can enlighten me :)

It work very well on macOS too... it's very useful to have custom prompt in these two operating system. anyway, I still think that a better default for hyper is needed that does not use ansi escape sequences.

parro-it avatar Dec 14 '16 20:12 parro-it

PS1="\[\033]0;\w\007\]>"

@parro-it This works great for me on macOS, thank you! Do you know if there is an additional way to make it reflect a process like e.g. node? Like if it's possible to prepend the process name to the cwd?

audunolsen avatar Dec 15 '16 00:12 audunolsen

hyper tabs

I managed to find a solution to show the basedir and append bash commands to it!

case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"'
    show_command_in_title_bar()
    {
        case "$BASH_COMMAND" in
            *\033]0*)
                ;;
            *)
                echo -ne "\033]0;${BASH_COMMAND} - ${PWD##*/}\007"
                ;;
        esac
    }
    trap show_command_in_title_bar DEBUG
    ;;
*)
    ;;
esac

I threw the above code in ~.profile, it's based on this.

audunolsen avatar Dec 15 '16 11:12 audunolsen

@audunolsen awesome!

parro-it avatar Dec 15 '16 11:12 parro-it

As I mentioned on #1162 (same bug) the tabs are just displaying "Shell" with Hyper 1.0.0 on Yosemite.

Setting PS1="\[\033]0;\w\007\]>" had no effect.

The bash script above does work.

zpnk avatar Dec 23 '16 18:12 zpnk

@audunolsen My current tab title was blank, and all tabs just showed "Shell". Placed the code you gave in ~/.profile. All tab titles got replaced by the name of current User. How do I get tab titles like the one in your GIF?

screen shot 2017-01-29 at 11 36 59 pm

anirudhmurali avatar Jan 29 '17 18:01 anirudhmurali

Sorry to pile on @audunolsen - I also pasted the code into .profile and no change. Using Bash on Windows10.

cfjedimaster avatar Mar 09 '17 03:03 cfjedimaster

Nevermind - user error. I put this in .bashrc and it worked. Now to figure out how to get it to show the complete path, not just the current directory. :)

cfjedimaster avatar Mar 09 '17 03:03 cfjedimaster

autotitle_plus

I decided to extend audunolsen's solution. I wanted the auto-title he created but I also wanted to have ability to set a static title to a tab when needed.

So I did this in .bashrc


function title {
    export TITLE_OVERRIDDEN=1
    PROMPT_COMMAND=''
    echo -ne "\033]0;"$*"\007"
}

case "$TERM" in
xterm*|rxvt*)
    PROMPT_COMMAND='echo -ne "\033]0;${PWD##*/}\007"'
    show_command_in_title_bar()
    {
        if [[ "$TITLE_OVERRIDDEN" == 1 ]]; then return; fi
        case "$BASH_COMMAND" in
            *\033]0*)
                ;;
            *)
                echo -ne "\033]0;${BASH_COMMAND} - ${PWD##*/}\007"
                ;;
        esac
    }
    trap show_command_in_title_bar DEBUG
    ;;
*)
    ;;
esac

Now when I need title it's title My new title

achekulaev avatar Jun 29 '17 20:06 achekulaev

I am still having trouble getting the location in tab title using powershell

the following line of code does the trick in ConEmu and in vanilla powershell but does not work when opening powershell in hyper. What gives?

$host.UI.RawUI.WindowTitle = Get-Location

Setting it to a string (se below) works fine.

$host.UI.RawUI.WindowTitle = "Hello"

stilren avatar Sep 07 '17 09:09 stilren

I'm a bit confused. my tabs are showing the correct command on hyper 1.4.3 on mac. but instead of "ng serve" I'd like something like 'myProject-ui' and the other tab could be 'myProject-service' instead of just 'node index.js' so is it posible to manually set title just the way you want?

luismasg avatar Sep 07 '17 20:09 luismasg

@luismasg @achekulaev's solution seemd perfect for your needs

chabou avatar Sep 08 '17 15:09 chabou

Hello all, here's a small update benefiting all of you using zsh. I recently made the switch from bash which broke my previous solution. Luckily, fixing this for zsh wasn't too difficult.

screenshot 2017-09-27 19 56 20
# ~/.zshrc

# Override auto-title when static titles are desired ($ title My new title)
title() { export TITLE_OVERRIDDEN=1; echo -en "\e]0;$*\a"}
# Turn off static titles ($ autotitle)
autotitle() { export TITLE_OVERRIDDEN=0 }; autotitle
# Condition checking if title is overridden
overridden() { [[ $TITLE_OVERRIDDEN == 1 ]]; }
# Echo asterisk if git state is dirty
gitDirty() { [[ $(git status 2> /dev/null | grep -o '\w\+' | tail -n1) != ("clean"|"") ]] && echo "*" }

# Show cwd when shell prompts for input.
precmd() {
   if overridden; then return; fi
   pwd=$(pwd) # Store full path as variable
   cwd=${pwd##*/} # Extract current working dir only
   print -Pn "\e]0;$cwd$(gitDirty)\a" # Replace with $pwd to show full path
}

# Prepend command (w/o arguments) to cwd while waiting for command to complete.
preexec() {
   if overridden; then return; fi
   printf "\033]0;%s\a" "${1%% *} | $cwd$(gitDirty)" # Omit construct from $1 to show args
}

I've commented each step, making it easy to modify the script to your likings (full path vs. basename only, show command with or without args, git dirty state). I've also incorporated @achekulaev's static titles, letting you override auto-titles with your own static titles ($ title My new title), remove the static title with $ autotitle.

audunolsen avatar Sep 27 '17 18:09 audunolsen

For windows bash users, I upgraded to Fall Creators update using the Windows update assistant, and this resolved itself for me 😄

darkdreamingdan avatar Nov 01 '17 13:11 darkdreamingdan

Titles are broken again on Windows (using cmd) when "Use legacy console" is not enabled. Switching to the legacy console works fine, but breaks WSL bash. Clink (and Cmder) had a similar, possibly related issue: https://github.com/mridgers/clink/pull/464

Something about the new conhost. https://github.com/Stanzilla/clink/commit/4217e6defd3914e338c3597c168f88ae54111e51

nathonius avatar Jan 08 '18 19:01 nathonius

I'm running into an issue while using @audunolsen 's zsh script. My titles come out as %15<..<, am I missing something simple?

mackmmiller avatar Jan 09 '18 14:01 mackmmiller

@mackmmiller: I'm using zsh version 5.3 (x86_64-apple-darwin17.0). I don't use zsh in conjunction with any other framework, e.g. oh-my-zsh, if you are, then maybe there's some clash there.

audunolsen avatar Jan 10 '18 00:01 audunolsen

Using zsh with ohmyzsh and hyper-tabs-enhanced and @audunolsen 's solution worked for me. It's even getting the process icons right which is pretty cool. Took me a good hour to get to this point though. It might me worthy to note that ohmyzsh has an implementation of title() itself, which got me really confused at first: https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/termsupport.zsh

ugrupp avatar Jan 19 '18 15:01 ugrupp

After working with @audunolsen 's solution a bit, I realized there's one problem. Using precmd() and preexec() directly causes conflicts with other zsh plugins which are also using these hooks. In my case, the git plugin wouldn't show the repo status anymore since its hooks were overridden by our tab title functions.

I managed to solve it, thanks to a similar solution posted in https://github.com/direnv/direnv/issues/24. The idea is to attach our handlers to the array of existing handlers, rather than directly defining precmd() and preexec(). So, extending @audunolsen 's solution:

# ~/.zshrc

# Override auto-title when static titles are desired ($ title My new title)
title() { export TITLE_OVERRIDDEN=1; echo -en "\e]0;$*\a"}
# Turn off static titles ($ autotitle)
autotitle() { export TITLE_OVERRIDDEN=0 }; autotitle
# Condition checking if title is overridden
overridden() { [[ $TITLE_OVERRIDDEN == 1 ]]; }
# Echo asterisk if git state is dirty
gitDirty() { [[ $(git status 2> /dev/null | grep -o '\w\+' | tail -n1) != ("clean"|"") ]] && echo "*" }

# Show cwd when shell prompts for input.
tabtitle_precmd() {
   if overridden; then return; fi
   pwd=$(pwd) # Store full path as variable
   cwd=${pwd##*/} # Extract current working dir only
   print -Pn "\e]0;$cwd$(gitDirty)\a" # Replace with $pwd to show full path
}
[[ -z $precmd_functions ]] && precmd_functions=()
precmd_functions=($precmd_functions tabtitle_precmd)

# Prepend command (w/o arguments) to cwd while waiting for command to complete.
tabtitle_preexec() {
   if overridden; then return; fi
   printf "\033]0;%s\a" "${1%% *} | $cwd$(gitDirty)" # Omit construct from $1 to show args
}
[[ -z $preexec_functions ]] && preexec_functions=()
preexec_functions=($preexec_functions tabtitle_preexec)

ugrupp avatar Jan 19 '18 16:01 ugrupp

I'm getting the same issue as @mackmmiller

alexh avatar Feb 18 '18 04:02 alexh

@audunolsen thanks for your solution but I have a problem with it. $_ pretty much always contains show_command_in_title_bar.

nikitakatchik avatar Mar 13 '18 13:03 nikitakatchik

Seems like this helps.

nikitakatchik avatar Mar 13 '18 13:03 nikitakatchik

screen shot 2018-03-14 at 9 18 22 pm

I just did a clean install of Hyper (and added plugin 'hyper-tab-icons') and the tab titles are not changing. Nor are the icons, like they are supposed to with hyper-tab-icons.

Do I need to do something in order for the title/icons to show?

marshalhayes avatar Mar 15 '18 02:03 marshalhayes

@ugrupp I imported your code suggested at https://github.com/zeit/hyper/issues/1188#issuecomment-359015977 but I am having issues since the title is all messed up and it looks like this:

Do you happen to know how I may fix it?

ghost avatar Apr 27 '18 11:04 ghost

@ugrupp Fortunately I found a quick fix. Since I am using oh-my-zsh I had to uncomment the line DISABLE_AUTO_TITLE="true" in my ~/.zshrc to make it work again.

ghost avatar Apr 27 '18 11:04 ghost

really late to the party, but #3015 fixes this issue. Should work on all shells too.

El-Dringo-Brannde avatar May 16 '18 18:05 El-Dringo-Brannde

putting it in my ~/.zshrc or ~/.bashrc worked for me

function title() { echo -e "\033]0;$(basename $PWD)" }

williammu6 avatar Feb 02 '21 17:02 williammu6

$host.UI.RawUI.WindowTitle = Get-Location

@stilren hopefully this is still somehow not too late. It's $(Get-Location) instead of Get-Location.

NotesOfReality avatar May 21 '22 22:05 NotesOfReality

To all the windows powershell (10 or less?I don't use 11) users, the most effective workaround atm seems to be leveraging $Host.UI.RawUI.WindowTitle by a PS profile.

For instance, $host.UI.RawUI.WindowTitle = $(Get-Location) would set the PS window title bar (which is inherited by the tabs extensions of hyper terminal) to the working directory at the time the command is run. If you want the tab/window title to update put the line in the prompt function.

NotesOfReality avatar May 21 '22 22:05 NotesOfReality