dotfiles/bash/ext

111 lines
2.9 KiB
Bash

# Make Bash more like Zsh
# Hooks
# derived from http://glyf.livejournal.com/63106.html
# TODO: support precmd_functions and chpwd_functions arrays
# default: NOOPs
function preexec { :; }
function precmd { :; }
function chpwd { :; }
__cd_invoke_chpwd() {
builtin cd "$@"
chpwd
}
__preexec_invoke_exec () {
local this_command;
[ -n "$COMP_LINE" ] && return # completion
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # precmd
this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`;
preexec "$this_command"
}
# set up the hooks
PROMPT_COMMAND="precmd"
trap '__preexec_invoke_exec' DEBUG
cd() { __cd_invoke_chpwd "$@"; }
# clears a line that was not terminated by a LF fixing the dangling prompt
# issue by marking it with a reversed %, like zsh
clear_incomplete_line() {
local row
local col
# ask for cursor position then read answer
# TODO: fix read: mashing keyboard makes syntax errors
stty -echo
echo -en "\033[6n"
IFS=';' read -r -d R -a pos
stty echo
# extract tput-compatible answer
row=$(( ${pos[0]:2} - 1 ))
col=$(( ${pos[1]} - 1 ))
# not on first column? do clean up: fill with spaces and rag left
if [[ $col != 0 ]]; then
printf "\e[7m%%\e[m"
printf "%*s\r" $(( COLUMNS - $col ))
fi
}
function sub_prompt_colors_unsized() {
sed \
-e 's#%F{black}#\\033[30m#g' \
-e 's#%F{red}#\\033[31m#g' \
-e 's#%F{green}#\\033[32m#g' \
-e 's#%F{yellow}#\\033[33m#g' \
-e 's#%F{blue}#\\033[34m#g' \
-e 's#%F{magenta}#\\033[35m#g' \
-e 's#%F{cyan}#\\033[36m#g' \
-e 's#%F{white}#\\033[37m#g' \
-e 's#%f#\\033[00m#g'
}
function sub_prompt_colors_sized() {
sed \
-e 's#%F{black}#\\[\\033[30m\\]#g' \
-e 's#%F{red}#\\[\\033[31m\\]#g' \
-e 's#%F{green}#\\[\\033[32m\\]#g' \
-e 's#%F{yellow}#\\[\\033[33m\\]#g' \
-e 's#%F{blue}#\\[\\033[34m\\]#g' \
-e 's#%F{magenta}#\\[\\033[35m\\]#g' \
-e 's#%F{cyan}#\\[\\033[36m\\]#g' \
-e 's#%F{white}#\\[\\033[37m\\]#g' \
-e 's#%f#\\[\\033[00m\\]#g'
}
function strip_prompt_colors() {
sed \
-e 's#%F{black}##g' \
-e 's#%F{red}##g' \
-e 's#%F{green}##g' \
-e 's#%F{yellow}##g' \
-e 's#%F{blue}##g' \
-e 's#%F{magenta}##g' \
-e 's#%F{cyan}##g' \
-e 's#%F{white}##g' \
-e 's#%f##g'
}
# right prompt support and PROMPT/RPROMPT vars
function apply_prompt_rprompt() {
local rprompt=$(echo "${RPROMPT}" | sub_prompt_colors_unsized)
local prompt=$(echo "${PROMPT}" | sub_prompt_colors_sized)
#local rprompt=$(echo "${RPROMPT}" | strip_prompt_colors)
#local prompt=$(echo "${PROMPT}" | strip_prompt_colors)
if [[ -n "${RPROMPT}" ]]; then
PS1="$(printf "\[%*s\r\]%s" "${COLUMNS}" "${rprompt:-}" "${prompt:-}")"
else
PS1="${prompt:-}"
fi
}
# vim: ft=bash