summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Cornellà <marc.cornella@live.com>2020-03-03 20:21:29 +0100
committerGitHub <noreply@github.com>2020-03-03 20:21:29 +0100
commitb9d5ee79130172aaeff05b2b49ba6507573957f6 (patch)
treef6bae25578db7a70ca8bfbae416d22ff185ca873
parent831089a3757ed08170c7e7687eb3e0377f286d82 (diff)
parent02d12538090a8ed74ccb15ef976fca46adcde37b (diff)
downloadzsh-b9d5ee79130172aaeff05b2b49ba6507573957f6.tar.gz
zsh-b9d5ee79130172aaeff05b2b49ba6507573957f6.tar.bz2
zsh-b9d5ee79130172aaeff05b2b49ba6507573957f6.zip
lib: automatic title: replace fg with description from jobs (#7982)
* Automatic title: Replace fg with description from jobs * Avoid error messages when there is no job * Use $jobstates and $jobtexts to look for jobs `jobs %string` doesn't work correctly when run inside `$()`. `$jobstates` and `$jobtexts` is available in the current shell process, so even though we need to replicate a bit more logic, every type of `fg` invocation works correctly. * lib: clean up termsupport.zsh Co-authored-by: Marc Cornellà <marc.cornella@live.com>
-rw-r--r--lib/termsupport.zsh54
1 files changed, 42 insertions, 12 deletions
diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
index 19fd57b9d..654927a1c 100644
--- a/lib/termsupport.zsh
+++ b/lib/termsupport.zsh
@@ -32,10 +32,10 @@ function title {
# Try to use terminfo to set the title
# If the feature is available set title
if [[ -n "$terminfo[fsl]" ]] && [[ -n "$terminfo[tsl]" ]]; then
- echoti tsl
- print -Pn "$1"
- echoti fsl
- fi
+ echoti tsl
+ print -Pn "$1"
+ echoti fsl
+ fi
fi
;;
esac
@@ -50,22 +50,52 @@ fi
# Runs before showing the prompt
function omz_termsupport_precmd {
- emulate -L zsh
-
- if [[ "$DISABLE_AUTO_TITLE" == true ]]; then
- return
- fi
-
+ [[ "$DISABLE_AUTO_TITLE" == true ]] && return
title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
}
# Runs before executing the command
function omz_termsupport_preexec {
+ [[ "$DISABLE_AUTO_TITLE" == true ]] && return
+
emulate -L zsh
setopt extended_glob
- if [[ "$DISABLE_AUTO_TITLE" == true ]]; then
- return
+ # split command into array of arguments
+ local -a cmdargs
+ cmdargs=("${(z)2}")
+ # if running fg, extract the command from the job description
+ if [[ "${cmdargs[1]}" = fg ]]; then
+ # get the job id from the first argument passed to the fg command
+ local job_id jobspec="${cmdargs[2]#%}"
+ # logic based on jobs arguments:
+ # http://zsh.sourceforge.net/Doc/Release/Jobs-_0026-Signals.html#Jobs
+ # https://www.zsh.org/mla/users/2007/msg00704.html
+ case "$jobspec" in
+ <->) # %number argument:
+ # use the same <number> passed as an argument
+ job_id=${jobspec} ;;
+ ""|%|+) # empty, %% or %+ argument:
+ # use the current job, which appears with a + in $jobstates:
+ # suspended:+:5071=suspended (tty output)
+ job_id=${(k)jobstates[(r)*:+:*]} ;;
+ -) # %- argument:
+ # use the previous job, which appears with a - in $jobstates:
+ # suspended:-:6493=suspended (signal)
+ job_id=${(k)jobstates[(r)*:-:*]} ;;
+ [?]*) # %?string argument:
+ # use $jobtexts to match for a job whose command *contains* <string>
+ job_id=${(k)jobtexts[(r)*${(Q)jobspec}*]} ;;
+ *) # %string argument:
+ # use $jobtexts to match for a job whose command *starts with* <string>
+ job_id=${(k)jobtexts[(r)${(Q)jobspec}*]} ;;
+ esac
+
+ # override preexec function arguments with job command
+ if [[ -n "${jobtexts[$job_id]}" ]]; then
+ 1="${jobtexts[$job_id]}"
+ 2="${jobtexts[$job_id]}"
+ fi
fi
# cmd name only, or if this is sudo or ssh, the next cmd