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

@ -1,13 +1,29 @@
# ignore sequential duplicates
setopt hist_ignore_dups
# ignore space-started
setopt hist_ignore_space
setopt hist_reduce_blanks
# append on exit
setopt append_history
# perform history expansion
setopt hist_verify
# do not load from persistence on every invocation
unsetopt share_history
HISTSIZE=100000
SAVEHIST=100000
# more! (live)
export HISTSIZE=100000
# all! (persisted)
export SAVEHIST=1000000
# share with bash
export HISTFILE=~/.history
# ignore repeated, space-started, and casual commands
HISTORY_IGNORE="(^ +|ls|bg|fg|pwd|exit|cd ..)"
# vim: ft=zsh

View file

@ -184,6 +184,7 @@ set_prompt() {
[[ -n "${IN_NIX_SHELL}" ]] && buffer="${buffer} %F{yellow}nix"
buffer="${buffer}%f> "
PROMPT="${buffer}"
local rbuffer=""
@ -203,4 +204,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=zsh

5
zsh/rc
View file

@ -14,7 +14,6 @@ source $DOTFILES_ZSH_DIR/prompt
source $DOTFILES_ZSH_DIR/fzf
source $DOTFILES_SHELL_DIR/go
source $DOTFILES_SHELL_DIR/direnv
source $DOTFILES_ZSH_DIR/kitty
set -o ignoreeof
unsetopt BEEP
@ -30,6 +29,8 @@ zmodload zsh/datetime
precmd() {
CMD_RC=$?
mark_command_exit "${CMD_RC}"
if [[ -n ${CMD_START} ]]; then
CMD_END="${EPOCHREALTIME}"
CMD_DURATION=$(( ${CMD_END} - ${CMD_START} ))
@ -41,11 +42,13 @@ precmd() {
psvar=()
set_prompt
set_term_title
mark_prompt
}
preexec() {
set_term_title
CMD_START="${EPOCHREALTIME}"
mark_command_exec "$1"
}
chpwd() {

View file

@ -1,28 +1,37 @@
# 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,
# including the host name to disambiguate local vs.
# remote connections. Percent-escape spaces.
local SEARCH=' '
local REPLACE='%20'
local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
printf '\e]7;%s\a' "$PWD_URL"
}
else
update_terminal_cwd() { :; }
fi
# Set terminal title
set_term_title() {
[[ -o interactive ]] || return
# Bubble information up to the terminal
case $TERM_PROGRAM in
Apple_Terminal)
local SEARCH=' '
local REPLACE='%20'
local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}"
printf '\e]7;%s\a' "$PWD_URL"
;;
*)
# NOOP
;;
esac
if [[ -n "${SSH_CLIENT}" ]]; then
local title="$USER@${HOSTNAME%%.*}:${PWD/#$HOME/~}"
else
local title="${PWD/#$HOME/~}"
fi
update_terminal_cwd
case $TERM in
screen*)
screen*|xterm*|rxvt*|alacritty)
#print -Pn "\ek%n@%m: %~\e\\" #breaks tmux
print -Pn "\e]2;%n@%m: %~\a"
;;
xterm*|*rxvt*)
print -Pn "\e]2;%n@%m: %~\a"
printf "\e]2;%s\a" "${title}"
;;
*)
# NOOP
: # NOOP
;;
esac
}