Fix job count reported by set prompt=%j #110
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I noticed that if I have %j in my $prompt it wouldn't update until I run a (non builtin) command.
Without this patch, on master HEAD:
Notice how after coming out of the first `ed', it still prints 0 (!). After the second it prints 1 (?).
sleep gets it to 2. killing a job is still 2. Going back to the last remaining ed and exiting... still 2.
The jobs builtin always reports correctly.
Running a ls >& /dev/null in between would update the job count reported by prompt=%j
The `jobs' builtin always reports the correct amount of jobs.
I couldn't quite wrap my head around the existing code (like why it started at -1, but -1 was printed as 0, etc), but the
jobs' builtin seemed to always report the correct count. So I cut-pasted code from
dojobs' into the `%j' handler in tc.prompt.c so that it now reports the number of jobs in a way I actually expect.I don't know how to automate tests for this, I couldn't even effectively break into gdb because ^Z was involved and it ended up getting processed by the shell controlling the terminal and I gave up trying to figure it out with `kill(1)' blabla. Anyway...
I tested by hand:
Notice how after coming out of `ed' after ^Z, the prompt is 1; same after the second one. Same after sleep.
Killing a job does not update it, but I didn't want to mess with how it usually works and get in the way of the jobs builtin or how the shell eventually reports jobs that went away etc.
If anyone has a better implementation, I'd be happy to test it/use it, but the existing code doesn't quite seem to work right.
Why did I end up with %j in $prompt? I got bored of the endless ^D There are jobs running fg fg fg ^D There are jobs running fg fg ^D There are jobs running fg fg fg fg when trying to shut down everything that was running in tmux for reboot. And dealing with floating vim swap files that got left around isn't what I'd consider fun :-)