1
0
Fork 0
mirror of synced 2024-06-01 06:41:12 -04:00

Adapted zimfw code style guide and enhanced git-info coloring

This commit is contained in:
PatTheMav 2018-01-05 21:29:10 +01:00
parent a4215d9d14
commit 1919ee6c7d

View file

@ -8,6 +8,8 @@ function {
# Global settings # Global settings
MNML_OK_COLOR="${MNML_OK_COLOR:-2}" MNML_OK_COLOR="${MNML_OK_COLOR:-2}"
MNML_ERR_COLOR="${MNML_ERR_COLOR:-1}" MNML_ERR_COLOR="${MNML_ERR_COLOR:-1}"
# ADDED FOR ZIMFW
MNML_DIV_COLOR="${MNML_DIV_COLOR:-5}"
MNML_USER_CHAR="${MNML_USER_CHAR:-λ}" MNML_USER_CHAR="${MNML_USER_CHAR:-λ}"
MNML_INSERT_CHAR="${MNML_INSERT_CHAR:-}" MNML_INSERT_CHAR="${MNML_INSERT_CHAR:-}"
@ -22,57 +24,57 @@ function {
# Components # Components
function mnml_status { function mnml_status {
local okc="$MNML_OK_COLOR" local okc="${MNML_OK_COLOR}"
local errc="$MNML_ERR_COLOR" local errc="${MNML_ERR_COLOR}"
local uchar="$MNML_USER_CHAR" local uchar="${MNML_USER_CHAR}"
local job_ansi="0" local job_ansi="0"
if [ -n "$(jobs | sed -n '$=')" ]; then if [ -n "$(jobs | sed -n '$=')" ]; then
job_ansi="4" job_ansi="4"
fi fi
local err_ansi="$MNML_OK_COLOR" local err_ansi="${MNML_OK_COLOR}"
if [ "$MNML_LAST_ERR" != "0" ]; then if [ "${MNML_LAST_ERR}" != "0" ]; then
err_ansi="$MNML_ERR_COLOR" err_ansi="${MNML_ERR_COLOR}"
fi fi
echo -n "%{\e[$job_ansi;3${err_ansi}m%}%(!.#.$uchar)%{\e[0m%}" echo -n "%{\e[$job_ansi;3${err_ansi}m%}%(!.#.$uchar)%{\e[0m%}"
} }
function mnml_keymap { function mnml_keymap {
local kmstat="$MNML_INSERT_CHAR" local kmstat="${MNML_INSERT_CHAR}"
[ "$KEYMAP" = 'vicmd' ] && kmstat="$MNML_NORMAL_CHAR" [ "$KEYMAP" = 'vicmd' ] && kmstat="${MNML_NORMAL_CHAR}"
echo -n "$kmstat" echo -n "${kmstat}"
} }
function mnml_cwd { function mnml_cwd {
local segments="${1:-2}" local segments="${1:-2}"
local seg_len="${2:-0}" local seg_len="${2:-0}"
local _w="%{\e[0m%}" local _w="%{\e[0m%}"
local _g="%{\e[38;5;244m%}" local _g="%{\e[38;5;244m%}"
if [ "$segments" -le 0 ]; then if [ "${segments}" -le 0 ]; then
segments=1 segments=1
fi
if [ "${seg_len}" -gt 0 ] && [ "${seg_len}" -lt 4 ]; then
seg_len=4
fi
local seg_hlen=$((seg_len / 2 - 1))
local cwd="%${segments}~"
cwd="${(%)cwd}"
cwd=("${(@s:/:)cwd}")
local pi=""
for i in {1..${#cwd}}; do
pi="$cwd[$i]"
if [ "${seg_len}" -gt 0 ] && [ "${#pi}" -gt "${seg_len}" ]; then
cwd[$i]="${pi:0:$seg_hlen}$_w..$_g${pi: -$seg_hlen}"
fi fi
if [ "$seg_len" -gt 0 ] && [ "$seg_len" -lt 4 ]; then done
seg_len=4
fi
local seg_hlen=$((seg_len / 2 - 1))
local cwd="%${segments}~" echo -n "$_g${(j:/:)cwd//\//$_w/$_g}$_w"
cwd="${(%)cwd}"
cwd=("${(@s:/:)cwd}")
local pi=""
for i in {1..${#cwd}}; do
pi="$cwd[$i]"
if [ "$seg_len" -gt 0 ] && [ "${#pi}" -gt "$seg_len" ]; then
cwd[$i]="${pi:0:$seg_hlen}$_w..$_g${pi: -$seg_hlen}"
fi
done
echo -n "$_g${(j:/:)cwd//\//$_w/$_g}$_w"
} }
function mnml_git { function mnml_git {
@ -83,139 +85,139 @@ function mnml_git {
} }
function mnml_uhp { function mnml_uhp {
local _w="%{\e[0m%}" local _w="%{\e[0m%}"
local _g="%{\e[38;5;244m%}" local _g="%{\e[38;5;244m%}"
local cwd="%~" local cwd="%~"
cwd="${(%)cwd}" cwd="${(%)cwd}"
echo -n "$_g%n$_w@$_g%m$_w:$_g${cwd//\//$_w/$_g}$_w" echo -n "$_g%n$_w@$_g%m$_w:$_g${cwd//\//$_w/$_g}$_w"
} }
function mnml_ssh { function mnml_ssh {
if [ -n "$SSH_CLIENT" ] || [ -n "$SSH_TTY" ]; then if [ -n "${SSH_CLIENT}" ] || [ -n "${SSH_TTY}" ]; then
echo -n "$(hostname -s)" echo -n "$(hostname -s)"
fi fi
} }
function mnml_pyenv { function mnml_pyenv {
if [ -n "$VIRTUAL_ENV" ]; then if [ -n "${VIRTUAL_ENV}" ]; then
_venv="$(basename $VIRTUAL_ENV)" _venv="$(basename ${VIRTUAL_ENV})"
echo -n "${_venv%%.*}" echo -n "${_venv%%.*}"
fi fi
} }
function mnml_err { function mnml_err {
local _w="%{\e[0m%}" local _w="%{\e[0m%}"
local _err="%{\e[3${MNML_ERR_COLOR}m%}" local _err="%{\e[3${MNML_ERR_COLOR}m%}"
if [ "${MNML_LAST_ERR:-0}" != "0" ]; then if [ "${MNML_LAST_ERR:-0}" != "0" ]; then
echo -n "$_err$MNML_LAST_ERR$_w" echo -n "${_err}${MNML_LAST_ERR}${_w}"
fi fi
} }
function mnml_jobs { function mnml_jobs {
local _w="%{\e[0m%}" local _w="%{\e[0m%}"
local _g="%{\e[38;5;244m%}" local _g="%{\e[38;5;244m%}"
local job_n="$(jobs | sed -n '$=')" local job_n="$(jobs | sed -n '$=')"
if [ "$job_n" -gt 0 ]; then if [ "${job_n}" -gt 0 ]; then
echo -n "$_g$job_n$_w&" echo -n "${_g}${job_n}${_w}&"
fi fi
} }
function mnml_files { function mnml_files {
local _w="%{\e[0m%}" local _w="%{\e[0m%}"
local _g="%{\e[38;5;244m%}" local _g="%{\e[38;5;244m%}"
local a_files="$(ls -1A | sed -n '$=')" local a_files="$(ls -1A | sed -n '$=')"
local v_files="$(ls -1 | sed -n '$=')" local v_files="$(ls -1 | sed -n '$=')"
local h_files="$((a_files - v_files))" local h_files="$((a_files - v_files))"
local output="${_w}[$_g${v_files:-0}" local output="${_w}[${_g}${v_files:-0}"
if [ "${h_files:-0}" -gt 0 ]; then if [ "${h_files:-0}" -gt 0 ]; then
output="$output $_w($_g$h_files$_w)" output="$output $_w(${_g}$h_files${_w})"
fi fi
output="$output${_w}]" output="${output}${_w}]"
echo -n "$output" echo -n "${output}"
} }
# Magic enter functions # Magic enter functions
function mnml_me_dirs { function mnml_me_dirs {
local _w="\e[0m" local _w="\e[0m"
local _g="\e[38;5;244m" local _g="\e[38;5;244m"
if [ "$(dirs -p | sed -n '$=')" -gt 1 ]; then if [ "$(dirs -p | sed -n '$=')" -gt 1 ]; then
local stack="$(dirs)" local stack="$(dirs)"
echo "$_g${stack//\//$_w/$_g}$_w" echo "${_g}${stack//\//${_w}/${_g}}${_w}"
fi fi
} }
function mnml_me_ls { function mnml_me_ls {
if [ "$(uname)" = "Darwin" ] && ! ls --version &> /dev/null; then if [ "$(uname)" = "Darwin" ] && ! ls --version &> /dev/null; then
COLUMNS=$COLUMNS CLICOLOR_FORCE=1 ls -C -G -F COLUMNS=${COLUMNS} CLICOLOR_FORCE=1 ls -C -G -F
else else
ls -C -F --color="always" -w $COLUMNS ls -C -F --color="always" -w ${COLUMNS}
fi fi
} }
function mnml_me_git { function mnml_me_git {
git -c color.status=always status -sb 2> /dev/null git -c color.status=always status -sb 2> /dev/null
} }
# Wrappers & utils # Wrappers & utils
# join outpus of components # join outpus of components
function mnml_wrap { function mnml_wrap {
local arr=() local arr=()
local cmd_out="" local cmd_out=""
for cmd in ${(P)1}; do for cmd in ${(P)1}; do
cmd_out="$(eval "$cmd")" cmd_out="$(eval "$cmd")"
if [ -n "$cmd_out" ]; then if [ -n "${cmd_out}" ]; then
arr+="$cmd_out" arr+="${cmd_out}"
fi fi
done done
echo -n "${(j: :)arr}" echo -n "${(j: :)arr}"
} }
# expand string as prompt would do # expand string as prompt would do
function mnml_iline { function mnml_iline {
echo "${(%)1}" echo "${(%)1}"
} }
# display magic enter # display magic enter
function mnml_me { function mnml_me {
local output=() local output=()
local cmd_output="" local cmd_output=""
for cmd in $MNML_MAGICENTER; do for cmd in ${MNML_MAGICENTER}; do
cmd_out="$(eval "$cmd")" cmd_out="$(eval "$cmd")"
if [ -n "$cmd_out" ]; then if [ -n "${cmd_out}" ]; then
output+="$cmd_out" output+="${cmd_out}"
fi fi
done done
echo -n "${(j:\n:)output}" | less -XFR echo -n "${(j:\n:)output}" | less -XFR
} }
# capture exit status and reset prompt # capture exit status and reset prompt
function mnml_line_init { function mnml_line_init {
MNML_LAST_ERR="$?" # I need to capture this ASAP MNML_LAST_ERR="$?" # I need to capture this ASAP
zle reset-prompt zle reset-prompt
} }
# redraw prompt on keymap select # redraw prompt on keymap select
function mnml_keymap_select { function mnml_keymap_select {
zle reset-prompt zle reset-prompt
} }
# draw infoline if no command is given # draw infoline if no command is given
function mnml_buffer_empty { function mnml_buffer_empty {
if [ -z "$BUFFER" ]; then if [ -z "${BUFFER}" ]; then
mnml_iline "$(mnml_wrap MNML_INFOLN)" mnml_iline "$(mnml_wrap MNML_INFOLN)"
mnml_me mnml_me
zle redisplay zle redisplay
else else
zle accept-line zle accept-line
fi fi
} }
prompt_minimal2_precmd() { prompt_minimal2_precmd() {
@ -238,9 +240,9 @@ prompt_minimal2_setup() {
zstyle ':zim:git-info:branch' format '%b' zstyle ':zim:git-info:branch' format '%b'
zstyle ':zim:git-info:commit' format '%c' zstyle ':zim:git-info:commit' format '%c'
zstyle ':zim:git-info:dirty' format '%{\e[0;3${MNML_ERR_COLOR}m%}' zstyle ':zim:git-info:dirty' format '%{\e[0;3${MNML_ERR_COLOR}m%}'
zstyle ':zim:git-info:diverged' format '%{\e[0;31m%}' zstyle ':zim:git-info:diverged' format '%{\e[0;3${MNML_DIV_COLOR}m%}'
zstyle ':zim:git-info:behind' format '%F{11}' zstyle ':zim:git-info:behind' format '%{\e[0;3${MNML_DIV_COLOR}m%}↓ '
zstyle ':zim:git-info:ahead' format '%f' zstyle ':zim:git-info:ahead' format '%{\e[0;3${MNML_DIV_COLOR}m%}↑ '
zstyle ':zim:git-info:keys' format \ zstyle ':zim:git-info:keys' format \
'prompt' '%b%c' \ 'prompt' '%b%c' \
'color' '$(coalesce "%D" "%V" "%B" "%A" "%{\e[0;3${MNML_OK_COLOR}m%}")' 'color' '$(coalesce "%D" "%V" "%B" "%A" "%{\e[0;3${MNML_OK_COLOR}m%}")'