zsh-users / fizsh

Friendly Interactive ZSHell.
Other
299 stars 18 forks source link

Adding current Github repo status #12

Closed gatlinnewhouse closed 7 years ago

gatlinnewhouse commented 7 years ago

Tried adding something like this:


function fish_prompt
    set -l git_branch (git branch ^/dev/null | sed -n '/\* /s///p')
    echo -n (whoami)'@'(hostname)':'(prompt_pwd)'{'"$git_branch"'} $ '
end
guidovansteen commented 7 years ago

Could you elaborate on this? Don't you confuse this project (which is called fiZsh) with another project (called fish)?

gatlinnewhouse commented 7 years ago

No. I just tried to use the same method I would use in fish with fizsh. I just want the current GitHub repo status to appear in the $PS1 when I cd into a repo directory.

guidovansteen commented 7 years ago

In that case you could modify the file called 'fizsh-prompt.zsh'. I would turn the current Github repo status into a string, and mould it in some way into the environment variable $_fizsh_prompt.

guidovansteen commented 7 years ago

I just tried to use the same method I would use in fish with fizsh

fish and fizsh are different. Fizsh is zsh, fish is a different shell.

gatlinnewhouse commented 7 years ago

Okay, I got the current github repo status working. But I cannot get my PS1 to show colors in zsh at all.

here is my .zshrc:

#!/usr/bin/env zsh
# -------------------------------------------------------------------------------------------------
# Copyright (c) 2011 Guido van Steen
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
#  * Redistributions of source code must retain the above copyright notice, this list of conditions
#    and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright notice, this list of
#    conditions and the following disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of the FIZSH nor the names of its contributors may be used to endorse or
#    promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -------------------------------------------------------------------------------------------------
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim: ft=zsh sw=2 ts=2 et
#
# /etc/fizsh/fizshrc

# This fizshrc script intends to make zsh behave similar to fish when it comes to fish's
# syntax highlighting and fish's matlab-like history search. This script also emulates
# fish's prompt.
#
# When fizsh is run for the first time this script is copied to "$HOME/.fizsh/.zshrc".
# "$HOME/.fizsh/" is fizsh's "$ZDOTDIR" directory. This file is therefore sourced automatically
# when fizsh is run.
#
# The script was tested on Linux. It may need some modifications to work on
# other systems.

################################################
#
# Set the environment variables
#
# copy "$_fizsh_F_LOGIN_FLAG_SET" to a local environment variable called "$_fizsh_F_LOCAL_LOGIN_FLAG_SET" 
#
_fizsh_F_LOCAL_LOGIN_FLAG_SET=$_fizsh_F_LOGIN_FLAG_SET
#
# unset the global environment variable "_fizsh_F_LOGIN_FLAG_SET", so that it can be used again in subshells started from here: 
# 
unset _fizsh_F_LOGIN_FLAG_SET
#
# if $SHLVL is equal to 2 this is an intrinsic login shell (i.e. started by "login") 
#
if [[ $SHLVL -eq 2 ]]; then; # fizsh is running as an intrinsic login shell 
  0="-fizsh" 
fi
#
if [[ $_fizsh_F_LOCAL_LOGIN_FLAG_SET -eq 1 ]]; then; # fizsh should be running as an login shell, because it has been called with the "--login" or "-l" option 
  0="-fizsh" 
fi 
#
if [[ $SHLVL -ne 2 && $_fizsh_F_LOCAL_LOGIN_FLAG_SET -ne 1 ]]; then; # fizsh is and should be running as a non-login shell. 
  0="fizsh" 
fi 
#
SHELL=$(which fizsh)

################################################
#
# History
#
HISTFILE=$_fizsh_F_DOT_DIR/.fizsh_history
HISTSIZE=100000
SAVEHIST=100000

################################################
#
# Append to the history file instead of overwriting it and do it immediately
# when a command is executed.
#
setopt append_history
setopt inc_append_history

################################################
#
# Avoid duplicate entries in the history file
#
setopt hist_ignore_all_dups

################################################
#
# Reduce whitespace in history
#
setopt hist_reduce_blanks

################################################
#
# Reduce whitespace in history
#
setopt hist_ignore_space

################################################
#
# Allow interactive comments
#
setopt interactive_comments

################################################
#
# When entering a nonexistent command name automatically try to find a similar one.
#
setopt correct

################################################
#
# Get rid of beeps
#
setopt no_beep

################################################
#
# Set the prompt
#
setopt prompt_subst

################################################
#
# PS1='$(fizsh-prompt)'
# for some reason turning this into a sourcable function does not work
# see below as well
#
source $_fizsh_F_DOT_DIR/git-prompt.zsh
function precmd() {
    PSVAR=`git_prompt_precmd`
}

autoload -U colors && colors
# Colors
eval red='$FG[203]'
eval green='$FG[184]'
eval yellow='$FG[221]'
eval blue='$FG[081]'
eval grey='$FG[145]'
local _current_dir="%{$green%}%1~%{$reset_color%} "

PS1="%{$red%}%n%{$reset_color%}%{$grey%}@%{$yellow%}%m ${_current_dir}%{$blue%}%v%{$reset_color%} $ "

################################################
#
# Set the terminal title
#
[[ "xterm" =~ $TERM ]] && precmd () {print -Pn "\e]0;$0: %n @ %M: %~\a"}
[[ "screen" =~ $TERM ]] && precmd () {print -Pn "\e]0;$0: %n @ %M [screened]: %~\a"}

################################################
#
# Initiate completion system
#
autoload -U compinit
if [[ $_fizsh_F_COMPINIT_STARTED -ne 1 ]]; then
  compinit -u
  _fizsh_F_COMPINIT_STARTED=1
fi

zmodload zsh/complist

################################################
#
# Enable color support of ls
#
if [[ "$TERM" != "dumb" ]]; then
  if [[ -x $(which dircolors) ]]; then
    eval $(dircolors -b)
    alias 'ls=ls --color=auto'
  fi
fi

################################################
#
# Use colored output
#
alias grep="grep --color=auto"

################################################
#
# Source zsh-syntax-highlighting.zsh, zsh-history-substring-search.zsh,
# fizsh-miscellaneous.zsh and fizshrc
#
source $_fizsh_F_DOT_DIR/zsh-syntax-highlighting.zsh
source $_fizsh_F_DOT_DIR/zsh-history-substring-search.zsh
source $_fizsh_F_DOT_DIR/fizsh-miscellaneous.zsh

################################################
#
# source $_fizsh_F_DOT_DIR/fizsh-prompt # would be nice as well. However,
# for some reason sourcing the prompt as a function from a file
# does not work properly: after a while the prompt gets out of sync
# the same thing seems to happen when we use the precmd function
# to echo the prompt. A bug in ZSH!? Anyway, for this reason 
# source $_fizsh_F_DOT_DIR/fizsh-prompt.zsh has been commented out.
#
#source $_fizsh_F_DOT_DIR/fizsh-prompt.zsh
source $_fizsh_F_DOT_DIR/.fizshrc

################################################
#
# Give $ZDOTDIR its original value again so that we can call zsh
# without implicitly calling fizsh.
#
[[ $+_fizsh_F_OLD_ZDOTDIR -eq 1 ]] && export ZDOTDIR=$_fizsh_F_OLD_ZDOTDIR
# if _fizsh_F_OLD_ZDOTDIR was exported, we use it to restore the value of ZDOTDIR
[[ $+_fizsh_F_OLD_ZDOTDIR -eq 0 ]] && unset ZDOTDIR 
# if _fizsh_F_OLD_ZDOTDIR was not exported, ZDOTDIR did not exist before fizsh was called. So we unset it. 
true 
# We also make sure that the last command of this script succeeds, so that fizsh always starts up with "$?" 
# equal to 0. 

The PS1 shows up fine in terminal besides the colors not showing up.

I tried adding autoload -U colors && colors to files like fizsh, .fizshrc, and fizsh-prompt.zsh (although I think I deprecated fizsh-prompt.zsh).

@guidovansteen

guidovansteen commented 7 years ago

Why do you modify .zshrc? Changes in the prompt are meant to be handled by fizsh-prompt.zsh. Moreover, it is hard to help if I can't see your git-prompt.zsh file.

P.S. In the past I noticed some unexpected behaviour when I sourced fizsh-prompt.zsh from .zshrc. I never bothered to look into this issue more closely, because it was solved by assigning PS1 directly. (see .zshrc).

gatlinnewhouse commented 7 years ago

Here is the git-prompt.zsh:

function in_git_repos() {
    test "`git rev-parse --is-inside-work-tree 2>/dev/null`" = "true"
}

function git_current_branch() {
    if in_git_repos; then
        ref=$(git symbolic-ref HEAD 2> /dev/null) || \
        ref=$(git rev-parse --short HEAD 2> /dev/null) || return
        echo ${ref#refs/heads/}
    fi
}

function git_status_is_clean() {
    if in_git_repos; then
        local lines=$(git status --porcelain | egrep -v '^\?\? ' | wc -l)
        test $lines = 0
    fi
}

function git_unknown_files() {
    if in_git_repos; then
        local lines=$(git status --porcelain | egrep '^\?\? ' | wc -l)
        test $lines = 0
    fi
}

function git_stash_is_clean() {
    if in_git_repos; then
        local lines=$(git stash list | wc -l)
        test $lines = 0
    fi
}

function git_no_branches() {
    if in_git_repos; then
        local lines=$(git branch | wc -l)
        test $lines = 1
    fi
}

function git_single_remote() {
    if in_git_repos; then
        local lines=$(git remote | wc -l)
        test $lines -le 1
    fi
}

function git_no_remote() {
    if in_git_repos; then
        local lines=$(git remote | wc -l)
        test $lines = 0
    fi
}

function git_branch_is_pushed() {
    if in_git_repos; then
        if git branch -r | grep "origin/master" >/dev/null 2>&1; then
            git_no_remote || git diff-tree --quiet origin/master heads/master
        fi
    fi
}

#-----------------------------------------------------------------------------

function git_prompt_precmd() {
    local GITINFO=""
    if [ ! -z `git_current_branch` ]; then
        GITINFO=" [`git_current_branch`"
        if ! git_status_is_clean; then
            GITINFO="$GITINFO*"
        fi
        if ! git_unknown_files; then
            GITINFO="${GITINFO}?"
        fi
        if ! git_branch_is_pushed; then
            GITINFO="$GITINFO➚"
        fi
        if ! git_stash_is_clean; then
            GITINFO="$GITINFO☰"
        fi
        if ! git_no_branches; then
            GITINFO="$GITINFO⌥"
        fi
        if ! git_single_remote; then
            GITINFO="$GITINFO®"
        fi
        GITINFO="$GITINFO]"
    fi
    echo $GITINFO
}

Again the git repo status (via this script) is working totally fine.

That's why I modified .zshrc, to assign a PS1 directly. Unless you mean assigning a PS1 in .fizsh

guidovansteen commented 7 years ago

In the original file .zshrc PS1 is assigned directly by PS1='$("$_fizsh_F_DOT_DIR"/fizsh-prompt.zsh)'. As mentioned in the original .zshrc, in the past I observed problems when I sourced fizsh-prompt.zsh. The same happened when I used the precmd function in order to echo the prompt. So you may try to avoid sourcing git-prompt.zsh and using the precmd function.

gatlinnewhouse commented 7 years ago

I tried merging my git-prompt.zsh with my .zshrc but I only ended up breaking fizsh's ability to display repo information.

I am skeptical that sourcing git-prompt.zsh would cause the lack of color in my PS1 that I inputted in my .zshrc here:

autoload -U colors && colors
# Colors
eval red='$FG[203]'
eval green='$FG[184]'
eval yellow='$FG[221]'
eval blue='$FG[081]'
eval grey='$FG[145]'
local _current_dir="%{$green%}%1~%{$reset_color%} "

PS1="%{$red%}%n%{$reset_color%}%{$grey%}@%{$yellow%}%m ${_current_dir}%{$blue%}%v%{$reset_color%} $ "
gatlinnewhouse commented 7 years ago

I think I may switch to Oh-My-Zsh since it also has matlab like history searches via the substring search module

guidovansteen commented 7 years ago

It is a free world. zsh-history-substring-search is included in both Oh-My-Zsh and fizsh. (I am one of the authors of zsh-history-substring-search).