Improve bash/zsh/terminal consistency

This commit is contained in:
Loic Nageleisen 2024-09-11 19:08:14 +02:00
parent bb8b9420d6
commit b780b2ce34
Signed by: lloeki
GPG key ID: D05DAEE6889F94C2
9 changed files with 160 additions and 50 deletions

View file

@ -66,8 +66,17 @@ __debug_invoke_preexec () {
local this_command;
# TODO: hack: doesn't work when command is not added to history
this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`;
[ -n "$COMP_LINE" ] && return # completion
[ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # precmd
# get current command by reading last history item
# contrary to zsh, the command needs to be inserted in the history
# so it may not work reliably depending on HISTCONTROL/HISTIGNORE
# - ignoredups is fine: last command stays the same
# - erasedups is fine: last command is inserted, older duplicates are removed
# - ignorespace is not fine (and therefore so is ignoreboth): never inserts
# - HISTIGNORE is not fine: never inserts
this_command="$(history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g")";
preexec "$this_command"
}
@ -126,7 +135,7 @@ clear_incomplete_line() {
fi
}
function sub_prompt_colors_unsized() {
function sub_zprompt_unsized() {
sed \
-e 's#%F{black}#\\033[30m#g' \
-e 's#%F{red}#\\033[31m#g' \
@ -136,10 +145,11 @@ function sub_prompt_colors_unsized() {
-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'
-e 's#%f#\\033[00m#g' \
-e 's#%{\(\([^%]\)*\)%}#\1#g'
}
function sub_prompt_colors_sized() {
function sub_zprompt_sized() {
sed \
-e 's#%F{black}#\\[\\033[30m\\]#g' \
-e 's#%F{red}#\\[\\033[31m\\]#g' \
@ -149,10 +159,11 @@ function sub_prompt_colors_sized() {
-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'
-e 's#%f#\\[\\033[00m\\]#g' \
-e 's#%{\(\([^%]\)*\)%}#\\[\1\\]#g'
}
function strip_prompt_colors() {
function strip_zprompt() {
sed \
-e 's#%F{black}##g' \
-e 's#%F{red}##g' \
@ -162,15 +173,17 @@ function strip_prompt_colors() {
-e 's#%F{magenta}##g' \
-e 's#%F{cyan}##g' \
-e 's#%F{white}##g' \
-e 's#%f##g'
-e 's#%f##g' \
-e 's#%{\(\([^%]\)*\)%}##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)
local rprompt=$(echo "${RPROMPT}" | sub_zprompt_unsized)
local prompt=$(echo "${PROMPT}" | sub_zprompt_sized)
#local rprompt=$(echo "${RPROMPT}" | strip_zprompt)
#local prompt=$(echo "${PROMPT}" | strip_zprompt)
if [[ -n "${RPROMPT}" ]]; then
PS1="$(printf "\[%*s\r\]%s" "${COLUMNS}" "${rprompt:-}" "${prompt:-}")"
else

View file

@ -1,16 +1,24 @@
# ignore repeated, space-started, and casual commands
export HISTIGNORE="&:[ ]*:l[sl]:[bf]g:exit:cd .."
# export HISTIGNORE="&:[ ]*:l[sl]:[bf]g:exit:cd .."
# ^ disabled until bash/ext preexec is fixed
export HISTIGNORE=""
# enable multiline historization as a single line
shopt -s cmdhist
# enable appending to histfile
# enable appending to histfile on exit
shopt -s histappend
# ignore sequential duplicates
export HISTCONTROL=ignoreboth
export HISTCONTROL=ignoredups
# more!
export HISTSIZE=10000
# more! (live)
export HISTSIZE=100000
# all! (persisted)
export HISTFILESIZE=1000000
# share with zsh
export HISTFILE=~/.history
# vim: ft=bash

View file

@ -132,4 +132,29 @@ set_prompt() {
fi
}
mark_prompt() {
local mark_a='%{\e]133;A\a%}'
local mark_b='%{\e]133;B\a%}'
PROMPT="${mark_a}${PROMPT}${mark_b}"
}
mark_command_exec() {
if [[ $# -eq 0 ]]; then
local mark_c="\e]133;C\a"
printf "${mark_c}" "${command}"
else
local command="${1:-}"
local mark_c="\e]133;C;cmdline=%q\a"
printf "${mark_c}" "${command}"
fi
}
mark_command_exit() {
local rc="${1:-}"
local mark_d="\e]133;D${rc};\a"
printf "${mark_d}"
}
# vim: ft=bash

11
bash/rc
View file

@ -18,7 +18,7 @@ source $DOTFILES_SHELL_DIR/chruby
source $DOTFILES_BASH_DIR/fzf
source $DOTFILES_SHELL_DIR/direnv
source $DOTFILES_SHELL_DIR/kd
source $DOTFILES_BASH_DIR/kitty
#source $DOTFILES_BASH_DIR/kitty
source $DOTFILES_SHELL_DIR/git_prompt_info
GIT_PS1_SHOWDIRTYSTATE=1
@ -28,6 +28,8 @@ GIT_PS1_SHOWUNTRACKEDFILES=1
precmd() {
CMD_RC=$?
mark_command_exit "${CMD_RC}"
# if [[ -n ${CMD_START} ]]; then
# CMD_END="${EPOCHREALTIME}"
# CMD_DURATION=$(bc <<<"${CMD_END} - ${CMD_START}")
@ -38,20 +40,21 @@ precmd() {
_direnv_hook
#clear_incomplete_line
set_prompt
apply_prompt_rprompt
update_terminal_cwd
set_term_title
mark_prompt
apply_prompt_rprompt
}
preexec() {
set_term_title
CMD_START="${EPOCHREALTIME}"
mark_command_exec "$1"
}
chpwd() {
__git_ps1_gitdir
_gopath
_auto-chruby
if _has-chruby; then _auto-chruby; fi
}
chpwd

View file

@ -1,4 +1,4 @@
# Tell the terminal about the working directory at each prompt.
# Tell Apple Terminal about the working directory at each prompt.
if [ "$TERM_PROGRAM" == "Apple_Terminal" ] && [ -z "$INSIDE_EMACS" ]; then
update_terminal_cwd() {
# Identify the directory using a "file:" scheme URL,
@ -14,15 +14,22 @@ else
fi
set_term_title() {
if [[ -n "${SSH_CLIENT}" ]]; then
local title="$USER@${HOSTNAME%%.*}:${PWD/#$HOME/\~}"
else
local title="${PWD/#$HOME/\~}"
fi
update_terminal_cwd
case $TERM in
xterm*|rxvt*|alacritty)
local title="\033]2;$USER@${HOSTNAME%%.*}:${PWD/#$HOME/~}\a"
;;
*)
local title=""
;;
screen*|xterm*|rxvt*|alacritty)
printf "\e]2;%s\a" "${title}"
;;
*)
: # NOOP
;;
esac
printf "$title"
}
# vim: ft=bash