diff --git a/zsh/03-apps.zsh b/zsh/03-apps.zsh new file mode 100644 index 0000000..2cc2dbb --- /dev/null +++ b/zsh/03-apps.zsh @@ -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 diff --git a/zsh/apps/gitinfo.app.zsh b/zsh/apps/gitinfo.app.zsh new file mode 100644 index 0000000..ffe8fb5 --- /dev/null +++ b/zsh/apps/gitinfo.app.zsh @@ -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) +} diff --git a/zsh/apps/showcolors.app.zsh b/zsh/apps/showcolors.app.zsh new file mode 100644 index 0000000..3640b29 --- /dev/null +++ b/zsh/apps/showcolors.app.zsh @@ -0,0 +1,4 @@ +function showcolors() +{ + log 0 "Here will be a colors-showing app." +} diff --git a/zsh/defaults/02-apps.conf b/zsh/defaults/02-apps.conf new file mode 100644 index 0000000..409ba7e --- /dev/null +++ b/zsh/defaults/02-apps.conf @@ -0,0 +1,2 @@ +# Apps list to enable. +ENABLED_APPS=("colors") diff --git a/zsh/prompts/pztrn.zsh b/zsh/prompts/pztrn.zsh index b60dc3a..53acfe4 100644 --- a/zsh/prompts/pztrn.zsh +++ b/zsh/prompts/pztrn.zsh @@ -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