Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix job count reported by set prompt=%j #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alzwded
Copy link
Contributor

@alzwded alzwded commented Jul 18, 2024

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:

    tcsh$ set prompt=%j\
    0 ed
    ^Z
    Suspended
    0 ed
    ^Z
    Suspended
    1 sleep 5 &
    [3] 317772
    2
    [3]    Done                          sleep 5
    2 kill -9 %1
    2
    [1]    Killed                        ed
    2 fg
    ed
    Q
    2

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:

tcsh$ set prompt='%j '
0 ed
^Z
Suspended
1 ed
^Z
Suspended
2 sleep 5 &
[3] 12345
3
[3]	Done		sleep 5
2 kill -9 %1
2
[1]	Killed
1
fg
ed
Q
0

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 :-)

I noticed that if I have %j in my $prompt it wouldn't update until I run a (forked) command.

Without this patch, on master HEAD:

        tcsh$ set prompt=%j\
        0 ed
        ^Z
        Suspended
        0 ed
        ^Z
        Suspended
        1 sleep 5 &
        [3] 317772
        2
        [3]    Done                          sleep 5
        2 kill -9 %1
        2
        [1]    Killed                        ed
        2 fg
        ed
        Q
        2

Running a ls >& /dev/null in between would update the job count.

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:

	tcsh$ set prompt='%j '
	0 ed
	^Z
	Suspended
	1 ed
	^Z
	Suspended
	2 sleep 5 &
	[3] 12345
	3
	[3]	Done		sleep 5
	2 kill -9 %1
	2
	[1]	Killed
	1
	fg
	ed
	Q
	0

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.
@alzwded
Copy link
Contributor Author

alzwded commented Jul 20, 2024

Also opened a ticket on bugs.astrong.com per the README.

@alzwded
Copy link
Contributor Author

alzwded commented Aug 15, 2024

Ping?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant