d7825313cca7ec4cfdd0cf64fb9b0119d52a4ab7

Author
Marc Cornellà <marc.cornella@live.com>
Committer
GitHub <noreply@github.com>
Date

Message

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.

Diff

 1diff --git a/lib/termsupport.zsh b/lib/termsupport.zsh
 2index 59a1efb02c3ca15416b80c2c907c1de8b05dee7a..4d6fb8c21d02f5e50ebac570bbe3d65d0ed17013 100644
 3--- a/lib/termsupport.zsh
 4+++ b/lib/termsupport.zsh
 5@@ -68,25 +68,48 @@ function omz_termsupport_preexec {
 6     return
 7   fi
 8 
 9+  # split command into array of arguments
10+  local -a cmdargs
11+  cmdargs=("${(z)2}")
12+  # if running fg, extract the command from the job description
13+  if [[ "${cmdargs[1]}" = fg ]]; then
14+    # get the job id from the first argument passed to the fg command
15+    local job_id jobspec="${cmdargs[2]#%}"
16+    # logic based on jobs arguments:
17+    # http://zsh.sourceforge.net/Doc/Release/Jobs-_0026-Signals.html#Jobs
18+    # https://www.zsh.org/mla/users/2007/msg00704.html
19+    case "$jobspec" in
20+      <->) # %number argument:
21+        # use the same <number> passed as an argument
22+        job_id=${jobspec} ;;
23+      ""|%|+) # empty, %% or %+ argument:
24+        # use the current job, which appears with a + in $jobstates:
25+        # suspended:+:5071=suspended (tty output)
26+        job_id=${(k)jobstates[(r)*:+:*]} ;;
27+      -) # %- argument:
28+        # use the previous job, which appears with a - in $jobstates:
29+        # suspended:-:6493=suspended (signal)
30+        job_id=${(k)jobstates[(r)*:-:*]} ;;
31+      [?]*) # %?string argument:
32+        # use $jobtexts to match for a job whose command *contains* <string>
33+        job_id=${(k)jobtexts[(r)*${(Q)jobspec}*]} ;;
34+      *) # %string argument:
35+        # use $jobtexts to match for a job whose command *starts with* <string>
36+        job_id=${(k)jobtexts[(r)${(Q)jobspec}*]} ;;
37+    esac
38+
39+    # override preexec function arguments with job command
40+    local job_cmd="${jobtexts[$job_id]}"
41+    if [[ -n "$job_cmd" ]]; then
42+      1="$job_cmd"
43+      2="$job_cmd"
44+    fi
45+  fi
46+
47   # cmd name only, or if this is sudo or ssh, the next cmd
48   local CMD=${1[(wr)^(*=*|sudo|ssh|mosh|rake|-*)]:gs/%/%%}
49   local LINE="${2:gs/%/%%}"
50 
51-  # replace fg, possibly with argument, with description from jobs
52-  if [[ "$CMD" = fg ]]; then
53-    local JOB
54-    if [[ ${(z)1} = fg ]]; then # no arguments
55-      JOB="$(jobs %% 2>/dev/null)"
56-    else # arguments
57-      JOB="$(jobs ${${(z)1}[2]} 2>/dev/null)"
58-    fi
59-    if [[ $? -eq 0 ]]; then
60-      JOB="${${(z)JOB}[4,$]}" # trim job number, +, pid, status
61-      title ${JOB:gs/%/%%} ${JOB:gs/%/%%}
62-      return
63-    fi
64-  fi
65-
66   title '$CMD' '%100>...>$LINE%<<'
67 }
68