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

How to integrate posh-git-sh into oh-my-zsh? #14

Closed
alexya opened this issue Jul 1, 2016 · 10 comments
Closed

How to integrate posh-git-sh into oh-my-zsh? #14

alexya opened this issue Jul 1, 2016 · 10 comments

Comments

@alexya
Copy link

alexya commented Jul 1, 2016

I want to use posh-git-sh in shell 'oh-my-zsh', but I failed. could you please give me some help?

I posted a thread in stackoverflow

lyze added a commit that referenced this issue Jul 5, 2016
Remove printf format string parameter for status string creation.

See #14.
@lyze
Copy link
Owner

lyze commented Jul 5, 2016

The file git-prompt.sh is simply a shell script, so you can source it as usual in .zshrc.

oh-my-zsh comes with themes, usually setting the PROMPT variable. Themes have no knowledge of how posh-git-sh constructs the git status string. So if you set the PROMPT variable, it might get blown away later by a theme (or vice versa).

I examined the git prompt plugin for oh-my-zsh, and it seems that the git prompt plugin sets RPROMPT, presumably to avoid stepping on the PROMPT variable set by the theme.

I refactored a little bit, so in your .zshrc, you can easily get a posh git status indicator.

. posh-git-sh/git-prompt.sh
RPROMPT='$(__posh_git_echo)'

EDIT: Added quotes around RPROMPT value

@lyze lyze closed this as completed Jul 5, 2016
@alexya
Copy link
Author

alexya commented Jul 11, 2016

@lyze
I updated both git-prompt.sh and oh-my-zsh to latest, and the following is my .zshrc file.

export ZSH=/Users/alexya/.oh-my-zsh
ZSH_THEME="crcandy"
plugins=(git-prompt)
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
source $ZSH/oh-my-zsh.sh
. ~/posh-git-sh/git-prompt.sh
RPROMPT=$(__posh_git_echo)

close and reopen my terminal, then go to my project folder which contains code change, I still can't get the posh-git prompt style.

@lyze
Copy link
Owner

lyze commented Jul 11, 2016

This works for me for oh-my-zsh:

export ZSH=export/of/.osh-my-sh
ZSH_THEME='crcandy'
plugins=
export PATH=export/of/PATH
source $ZSH/oh-my-zsh.sh
. ~/posh-git-sh/git-prompt.sh
RPROMPT='$(__posh_git_echo)'

I think the single quotes are necessary so that the RPROMPT line actually gets run.

@alexya
Copy link
Author

alexya commented Jul 12, 2016

@lyze
Yeah, I missed the single quotes, thx. one more question,

alexya@alex-imac27 [09:12:35] [~/Source/hello_world/chef_repo] [staging *]
-> %                                                               [staging ≡ +1 ~0 -0]$

The above is the my terminal, you can see that the original git-prompt, [staging *], still exist. but I want the posh-git prompt to replace the position. I am not sure if it is by design of posh-git-sh, and it can't be approached.
I have set plugins to empty in .zshrc, the same as you.

@lyze
Copy link
Owner

lyze commented Jul 12, 2016

By design, posh-git-sh gives you a status line [staging ≡ +1 ~0 -0], and you can put that information anywhere. The difficulty is integrating into oh-my-zsh, which isn't quite extensible as we'd like. The oh-my-zsh themes use oh-my-zsh's builtin git plugin.

The line

[09:12:35] [~/Source/hello_world/chef_repo] [staging *]

is from crcandy

PROMPT=$'%{$fg_bold[green]%}%n@%m %{$fg[blue]%}%D{[%X]} %{$reset_color%}%{$fg[white]%}[%~]%{$reset_color%} $(git_prompt_info)\
%{$fg[blue]%}->%{$fg_bold[blue]%} %#%{$reset_color%} '

ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[green]%}["
ZSH_THEME_GIT_PROMPT_SUFFIX="]%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY=" %{$fg[red]%}*%{$fg[green]%}"
ZSH_THEME_GIT_PROMPT_CLEAN=""

The theme calls $(git_prompt_info), which is part of oh-my-zsh's git plugin. So the way to go about it seems to be by modifying the PROMPT variable.

Initial thoughts: You would have to either manually patch every theme you'd like to use to use posh-git instead, or monkey patch git_prompt_info to actually call posh-git instead. In either case you'd also have to keep in mind that the ZSH_THEME_GIT_PROMPT_* colors might be different from posh-git-sh's default colors.

@lyze lyze reopened this Jul 12, 2016
@alexya
Copy link
Author

alexya commented Jul 12, 2016

I resolved the issue by replacing $(git_prompt_info) with $(__posh_git_echo) in crcandy.zsh-theme
Thank you very much for your help.

@lyze lyze closed this as completed Jul 12, 2016
@nomasprime
Copy link

Tried for ages and having similar problems trying to integrate with any Prezto theme.

The themes don't put VCS info into PROMPT variable. Instead they use other variables, for example, preprompt, and the info is sourced from vcs_info.

If I try and store __posh_git_echo into a variable, I get something like % % in the output.

So this script only seems to work when used with the PROMPT variable.

@nomasprime
Copy link

Outside the function that deals with setting preprompt print -P "$(__posh_git_echo)" outputs correctly but inside the function is when I get % %.

Maybe this is common problem that people with more zsh experience run into so hopefully someone can point me in the right direction.

@TrueWill
Copy link

This was way more difficult than I expected. I ended up creating a custom theme:

PROMPT="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
PROMPT+=' %{$fg[cyan]%}%c%{$reset_color%}$(__posh_git_echo) '

ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
ZSH_THEME_GIT_PROMPT_CLEAN="%{$fg[blue]%})"

It would be nice if this community and/or the zsh community could automate this.

@eashi
Copy link

eashi commented Oct 12, 2019

I did the following:

  1. created a file under .oh-my-zsh/custom: my-custom-git.zsh
  2. pasted the raw content of https://github.com/lyze/posh-git-sh/blob/master/git-prompt.sh
  3. added a function at the end that overrides the git_prompt_info():
git_prompt_info () {
 __posh_git_echo   
}

That solved it for me, and hopefully should work with all themes that use the built-in git_prompt_info :)

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

No branches or pull requests

5 participants