* Fixed apps loading.

* Added new app: gitinfo, for retrieving git information.
* Default (and only one for this time) prompt is now using gitinfo
app for showing git status in current directory.
This commit is contained in:
Stanislav Nikitin 2015-12-26 15:33:59 +05:00
parent abfa93f1a4
commit 297f6d82ed
5 changed files with 191 additions and 6 deletions

14
zsh/03-apps.zsh Normal file
View File

@ -0,0 +1,14 @@
# Load enabled apps.
for app in ${ENABLED_APPS[@]}; do
if [ ! -f "${CONFIG_PATH}/zsh/apps/${app}.app.zsh" ]; then
error 0 "Application '${app}' doesn't exist"
else
source "${CONFIG_PATH}/zsh/apps/${app}.app.zsh"
# chpwd injecting.
chpwd_injector=`declare -f ${app}_chpwd`
if [ $chpwd_injector ]; then
${app}_chpwd
fi
fi
#export ${app}="${app}_main"
done

132
zsh/apps/gitinfo.app.zsh Normal file
View File

@ -0,0 +1,132 @@
GITINFO_BRANCH=""
GITINFO_COMMIT_ID=""
GITINFO_COMMIT_SHORTID=""
GITINFO_COMMIT_COUNT=0
GITINFO_NEW_FILES=""
GITINFO_MODIFIED_FILES=""
GITINFO_REMOTES=""
GITINFO_STASHES_COUNT=0
######################################################################
# Main function.
######################################################################
function gitinfo()
{
curdir=`pwd`
if [ ! -d "${curdir}/.git" ]; then
echo "This is not a git repository."
return 1
fi
gitinfo_get_branch
gitinfo_get_changes
gitinfo_get_commit_data
gitinfo_get_remotes
gitinfo_get_stashes
gitinfo_check
if [ $? -eq 0 ]; then
echo "Git repository information:"
echo "======================================================================"
echo "| Branch | $fg[yellow]${GITINFO_BRANCH}$reset_color"
echo "| Commit | $fg[green]${GITINFO_COMMIT_ID}$reset_color"
echo "| Commits count | ${GITINFO_COMMIT_COUNT}"
echo "| Stashes | ${GITINFO_STASHES_COUNT}"
echo "======================================================================"
else
echo "This is not a git repository. But you seeing this message. So it's a bug in 'gitinfo' application. Please, report to developer!"
fi
}
######################################################################
# Adds some of gitinfo's functions into chpwd array.
######################################################################
function gitinfo_chpwd()
{
emulate -L zsh
precmd_functions=(${precmd_functions[@]} "gitinfo_get_branch" "gitinfo_get_commit_data" "gitinfo_get_changes" "gitinfo_get_remotes" "gitinfo_get_stashes")
}
######################################################################
# Checks if variables was set.
# Useful to call if we're using this app in prompt configuration.
######################################################################
function gitinfo_check()
{
curdir=`pwd`
if [ "${#GITINFO_BRANCH}" -eq 0 ]; then
return 1
fi
}
######################################################################
# Gets current directory's branch.
######################################################################
function gitinfo_get_branch()
{
curdir=`pwd`
if [ ! -d "${curdir}/.git" ]; then
GITINFO_BRANCH=""
return 1
fi
GITINFO_BRANCH=`git branch | cut -d " " -f 2`
}
######################################################################
# Gets current changes, if any.
######################################################################
function gitinfo_get_changes()
{
curdir=`pwd`
if [ ! -d "${curdir}/.git" ]; then
GITINFO_NEW_FILES=""
GITINFO_MODIFIED_FILES=""
return 1
fi
GITINFO_NEW_FILES=`LC_ALL=C git status | grep "new" | wc -l`
GITINFO_MODIFIED_FILES=`LC_ALL=C git status | grep "modified" | wc -l`
}
######################################################################
# Gets current directory's commit info, like long hash, short hash,
# commits count.
######################################################################
function gitinfo_get_commit_data()
{
curdir=`pwd`
if [ ! -d "${curdir}/.git" ]; then
GITINFO_COMMIT_SHORTID=""
GITINFO_COMMIT_ID=""
GITINFO_COMMIT_COUNT=""
return 1
fi
GITINFO_COMMIT_SHORTID=`git rev-parse --short HEAD`
GITINFO_COMMIT_ID=`git log | head -n 1 | cut -d " " -f 2`
GITINFO_COMMIT_COUNT=`git log | grep "commit " | wc -l`
}
######################################################################
# Gets remotes
######################################################################
function gitinfo_get_remotes()
{
curdir=`pwd`
if [ ! -d "${curdir}/.git" ]; then
GITINFO_REMOTES=""
return 1
fi
GITINFO_REMOTES=$(git remote -v | awk {' print $2 '} | uniq | wc -l)
}
######################################################################
# Gets current directory's stashes count.
######################################################################
function gitinfo_get_stashes()
{
curdir=`pwd`
if [ ! -d "${curdir}/.git" ]; then
GITINFO_STASHES_COUNT=""
return 1
fi
GITINFO_STASHES_COUNT=$(git stash list 2>/dev/null | wc -l)
}

View File

@ -0,0 +1,4 @@
function showcolors()
{
log 0 "Here will be a colors-showing app."
}

View File

@ -0,0 +1,2 @@
# Apps list to enable.
ENABLED_APPS=("colors")

View File

@ -1,6 +1,11 @@
# pztrn prompt theme
autoload -U add-zsh-hook
# Common vars array.
declare -A vars
# Prompt parts.
declare p_date p_tty p_plat p_userpwd p_shlvlhist p_rc p_end p_win p_path p_gitinfo
prompt_pztrn_help () {
cat <<'EOF'
@ -12,10 +17,8 @@ EOF
}
prompt_pztrn_setup () {
local -A vars
local p_date p_tty p_plat p_userpwd p_shlvlhist p_rc p_end p_win p_path
emulate -L zsh
precmd_functions=(${precmd_functions[@]} "prompt_pztrn_gitinfo" "prompt_pztrn_createprompt")
# Session-dependend colorizing.
# Local will be black, remote - yellow.
@ -51,9 +54,11 @@ prompt_pztrn_setup () {
p_path="$vars['brackets_start']$vars['default_color'] %d $vars['brackets_end']"
PROMPT="$p_date$p_tty$p_plat$p_userpwd$p_shlvlhist$p_rc
# Initial prompt creation. On every cd it will be recreated with
# prompt_pztrn_createprompt function.
PROMPT="$p_date$p_tty$p_plat$p_userpwd$p_shlvlhist$p_rc$p_gitinfo
$vars['console']$p_path $p_end"
RPROMPT="$p_date$p_tty$p_plat$p_userpwd$p_shlvlhist$p_rc
RPROMPT="$p_date$p_tty$p_plat$p_userpwd$p_shlvlhist$p_rc$p_gitinfo
$vars['console']$p_path $p_end"
PS2='%(4_.\.)%3_> %E'
@ -63,4 +68,32 @@ $vars['console']$p_path $p_end"
}
prompt_pztrn_gitinfo()
{
# Git repository information.
# Depends on 'gitinfo' application.
gitinfo_present=`declare -f gitinfo_check`
if [ $gitinfo_present ]; then
gitinfo_check
#echo $?
if [[ $? -eq 0 ]]; then
p_gitinfo_branch="$vars['brackets_start']$vars['usercolor']${GITINFO_BRANCH}$reset_color$vars['brackets_end']"
p_gitinfo_commit="$vars['brackets_start']%{$fg[magenta]%}${GITINFO_COMMIT_SHORTID}$reset_color (%{$fg[cyan]%}${GITINFO_COMMIT_COUNT}$reset_color)$vars['brackets_end']"
p_gitinfo_remotes="$vars['brackets_start']%{$fg[green]%}${GITINFO_REMOTES} remote(s)$reset_color$vars['brackets_end']"
p_gitinfo_files="$vars['brackets_start']%{$fg[green]%}${GITINFO_NEW_FILES}$reset_color new, %{$fg[yellow]%}${GITINFO_MODIFIED_FILES}$reset_color modified$vars['brackets_end']"
p_gitinfo=$'\n'"$p_gitinfo_branch$p_gitinfo_commit$p_gitinfo_files$p_gitinfo_remotes"
else
p_gitinfo=""
fi
fi
}
prompt_pztrn_createprompt()
{
PROMPT="$p_date$p_tty$p_plat$p_userpwd$p_shlvlhist$p_rc$p_gitinfo
$vars['console']$p_path $p_end"
RPROMPT="$p_date$p_tty$p_plat$p_userpwd$p_shlvlhist$p_rc$p_gitinfo
$vars['console']$p_path $p_end"
}
prompt_pztrn_setup