romkatv / gitstatus

Git status for Bash and Zsh prompt
GNU General Public License v3.0
1.68k stars 106 forks source link

Add gitstatus to window title with powerlevel10k #353

Closed jameswilson closed 1 year ago

jameswilson commented 2 years ago

Thanks @romkatv for your hard work on gitstatus. Reading the project page was eye opening and amazing! Bravo.

I use omz and p10k, and would like to add the current branch info to the window title.

I currently have the following in my ~/.zshrc:

source ~/.oh-my-zsh/custom/themes/powerlevel10k/gitstatus/gitstatus.prompt.zsh
autoload -Uz add-zsh-hook
function set-title {
  local localgit=''
  if gitstatus_query MY && [[ $VCS_STATUS_RESULT == ok-sync ]]; then
    local localgit=${${VCS_STATUS_LOCAL_BRANCH:-@${VCS_STATUS_COMMIT}}//\%/%%}
    (( VCS_STATUS_NUM_STAGED    )) && localgit+='+'
    (( VCS_STATUS_NUM_UNSTAGED  )) && localgit+='!'
    (( VCS_STATUS_NUM_UNTRACKED )) && localgit+='?'
  fi
  print -Pn "\e]0;%~ $localgit\a";
}
add-zsh-hook precmd set-title

ZSH_THEME_TERM_TAB_TITLE_IDLE="%~"
ZSH_THEME_TERM_TITLE_IDLE="%~"

Is there a better way to add gitstatus output into the terminal / iterm2 / xterm window title with p10k? If not, then I'd like to offer this recipe as documentation for how this can be done with zsh + omz + p10k. It probably wouldn't be too complicated to add similar recipes for vanilla zsh without omz+p10k and for bash as well.

It would be really cool to also have the fancy-pants short directory name abbreviations in the window title too if pwd is nested too deep instead of the plain old %~ value. I'm not sure of the best way to go about doing this the "p10k way".

romkatv commented 2 years ago

Is there a better way to add gitstatus output into the terminal / iterm2 / xterm window title with p10k?

It's best to add this kind of code to my_git_formatter in ~/.p10k.zsh.

I'd like to offer this recipe as documentation

You are the first person to ask for this. If more people express the desire to have git status in their terminal title, I'll add this to FAQ.

jameswilson commented 1 year ago

Okay thanks. I saw that function but I was nervous to edit ~/.p10k.zsh because it looked like it is a kind of autogenerated file. I suppose once you generate it once, it becomes editable and I could add it to dotfiles git repo.

jameswilson commented 1 year ago

Fyi, the reason to have the branch in terminal title is because i have a time tracking app that can read the window title of the active app, and it is really helpful to see which feature branch I'm working on be exposed there when I go back through timelogs that get auto recorded.

romkatv commented 1 year ago

Okay thanks. I saw that function but I was nervous to edit ~/.p10k.zsh because it looked like it is a kind of autogenerated file. I suppose once you generate it once, it becomes editable and I could add it to dotfiles git repo.

p10k configure generates this file but after that you can edit it to your liking. It has a ton of comments to help with this. Whether you edit this file or not, you are meant to save it. Powerlevel10k guarantees backward compatibility with all parameters, so as long as you have this file your prompt will stay the same even if you update powerlevel10k.