From c1446b4750a31506daba7fc7d41957dd515e8022 Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Sat, 6 Jul 2019 12:10:30 -0400 Subject: [PATCH 1/4] Automatic title: Replace fg with description from jobs --- lib/termsupport.zsh | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index aa14f3f07..a74ad9922 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -72,7 +72,19 @@ function omz_termsupport_preexec { local CMD=${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%} local LINE="${2:gs/%/%%}" - title '$CMD' '%100>...>$LINE%<<' + if [[ "$CMD" = fg ]]; then + # replace fg, possibly with argument, with description from jobs + local JOB + if [[ ${(z)1} = fg ]]; then # no arguments + JOB="$(jobs %%)" + else # arguments + JOB="$(jobs ${${(z)1}[2]})" + fi + JOB="${${(z)JOB}[4,$]}" # trim job number, +, pid, status + title ${JOB:gs/%/%%} ${JOB:gs/%/%%} + else + title '$CMD' '%100>...>$LINE%<<' + fi } precmd_functions+=(omz_termsupport_precmd) From 902e3172c9b991ca0995a0dcd658caa29b918d8e Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Sun, 7 Jul 2019 10:48:36 -0400 Subject: [PATCH 2/4] Avoid error messages when there is no job --- lib/termsupport.zsh | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index a74ad9922..59a1efb02 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -72,19 +72,22 @@ function omz_termsupport_preexec { local CMD=${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%} local LINE="${2:gs/%/%%}" + # replace fg, possibly with argument, with description from jobs if [[ "$CMD" = fg ]]; then - # replace fg, possibly with argument, with description from jobs local JOB if [[ ${(z)1} = fg ]]; then # no arguments - JOB="$(jobs %%)" + JOB="$(jobs %% 2>/dev/null)" else # arguments - JOB="$(jobs ${${(z)1}[2]})" + JOB="$(jobs ${${(z)1}[2]} 2>/dev/null)" + fi + if [[ $? -eq 0 ]]; then + JOB="${${(z)JOB}[4,$]}" # trim job number, +, pid, status + title ${JOB:gs/%/%%} ${JOB:gs/%/%%} + return fi - JOB="${${(z)JOB}[4,$]}" # trim job number, +, pid, status - title ${JOB:gs/%/%%} ${JOB:gs/%/%%} - else - title '$CMD' '%100>...>$LINE%<<' fi + + title '$CMD' '%100>...>$LINE%<<' } precmd_functions+=(omz_termsupport_precmd) From d7825313cca7ec4cfdd0cf64fb9b0119d52a4ab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Tue, 3 Mar 2020 20:10:43 +0100 Subject: [PATCH 3/4] 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/termsupport.zsh | 53 ++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index 59a1efb02..4d6fb8c21 100644 --- a/lib/termsupport.zsh +++ b/lib/termsupport.zsh @@ -68,25 +68,48 @@ function omz_termsupport_preexec { return fi + # 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 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* + job_id=${(k)jobtexts[(r)*${(Q)jobspec}*]} ;; + *) # %string argument: + # use $jobtexts to match for a job whose command *starts with* + job_id=${(k)jobtexts[(r)${(Q)jobspec}*]} ;; + esac + + # override preexec function arguments with job command + local job_cmd="${jobtexts[$job_id]}" + if [[ -n "$job_cmd" ]]; then + 1="$job_cmd" + 2="$job_cmd" + fi + fi + # cmd name only, or if this is sudo or ssh, the next cmd local CMD=${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%} local LINE="${2:gs/%/%%}" - # replace fg, possibly with argument, with description from jobs - if [[ "$CMD" = fg ]]; then - local JOB - if [[ ${(z)1} = fg ]]; then # no arguments - JOB="$(jobs %% 2>/dev/null)" - else # arguments - JOB="$(jobs ${${(z)1}[2]} 2>/dev/null)" - fi - if [[ $? -eq 0 ]]; then - JOB="${${(z)JOB}[4,$]}" # trim job number, +, pid, status - title ${JOB:gs/%/%%} ${JOB:gs/%/%%} - return - fi - fi - title '$CMD' '%100>...>$LINE%<<' } From 02d12538090a8ed74ccb15ef976fca46adcde37b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Tue, 3 Mar 2020 20:17:01 +0100 Subject: [PATCH 4/4] lib: clean up termsupport.zsh --- lib/termsupport.zsh | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh index 4d6fb8c21..d67223caa 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,24 +50,17 @@ 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 - fi - # split command into array of arguments local -a cmdargs cmdargs=("${(z)2}") @@ -99,10 +92,9 @@ function omz_termsupport_preexec { esac # override preexec function arguments with job command - local job_cmd="${jobtexts[$job_id]}" - if [[ -n "$job_cmd" ]]; then - 1="$job_cmd" - 2="$job_cmd" + if [[ -n "${jobtexts[$job_id]}" ]]; then + 1="${jobtexts[$job_id]}" + 2="${jobtexts[$job_id]}" fi fi