Unverified Commit b9d5ee79 authored by Marc Cornellà's avatar Marc Cornellà Committed by GitHub
Browse files

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: default avatarMarc Cornellà <marc.cornella@live.com>
parents 831089a3 02d12538
...@@ -50,22 +50,52 @@ fi ...@@ -50,22 +50,52 @@ fi
# Runs before showing the prompt # Runs before showing the prompt
function omz_termsupport_precmd { function omz_termsupport_precmd {
emulate -L zsh [[ "$DISABLE_AUTO_TITLE" == true ]] && return
if [[ "$DISABLE_AUTO_TITLE" == true ]]; then
return
fi
title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE title $ZSH_THEME_TERM_TAB_TITLE_IDLE $ZSH_THEME_TERM_TITLE_IDLE
} }
# Runs before executing the command # Runs before executing the command
function omz_termsupport_preexec { function omz_termsupport_preexec {
[[ "$DISABLE_AUTO_TITLE" == true ]] && return
emulate -L zsh emulate -L zsh
setopt extended_glob setopt extended_glob
if [[ "$DISABLE_AUTO_TITLE" == true ]]; then # split command into array of arguments
return 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 fi
# cmd name only, or if this is sudo or ssh, the next cmd # cmd name only, or if this is sudo or ssh, the next cmd
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment