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

parse signal values in status codes #1969

Closed
anarcat opened this issue Dec 19, 2018 · 0 comments · Fixed by #2026
Closed

parse signal values in status codes #1969

anarcat opened this issue Dec 19, 2018 · 0 comments · Fixed by #2026

Comments

@anarcat
Copy link

anarcat commented Dec 19, 2018

After realizing I was in the process of rewriting something similar to powerline in bash, I figured I would give this project a try again.

One of the things my fancy bash prompt does (thanks to help from some friends ;), is to display properly status code issued from signals received from the shell. For example:

anarcat@curie:~(master)$ true
anarcat@curie:~(master)$ ( exit 1 )
[1]anarcat@curie:~(master)$ ( sleep 10 )
Complété
[SIGTERM]anarcat@curie:~(master)$ 

First prompt exited cleanly: no error. Status code 1 get displayed as is, but the sleep call (which i interrupted in another window), gets shown with the proper signal (instead of, say, "status code 143").

This is how the above looks in powerline:

 anarcat   master  ~  true
 anarcat   master  ~  (exit 1)
 anarcat   master  ~  1  (sleep 10)
Complété
 anarcat   master  ~  143  

forgive the private unicode... But basically, what would be nice would be to parse status codes above 128 for the signal value. here's the bash we're using for that:

__exit_codes_to_SIGs () {
    local -n sig_status=$1 ; shift
    local exit_codes=( "$@" )
    local i=0
    for exit_code in "${exit_codes[@]}" ; do
	if [ "$exit_code" -gt 128 ] ; then
	    local signal=$((exit_code-128))
	    signal="SIG$(kill -l "$signal" 2> /dev/null)" ||
		signal=""
	    if [ -n "$signal" ] ; then
		sig_status[$i]="${magenta}${signal}${reset}"
	    else
		sig_status[$i]="${red}${exit_code}${reset}"
	    fi
	elif [ "$exit_code" -ne 0 ] ; then
	    sig_status[$i]="${red}${exit_code}${reset}"
	fi
	i=$((i+1))
    done
}

It's a nasty piece of work with arrays and everything, but it works fairly well. It does, incidentally, make control-c look much better than in powerline:

anarcat@curie:~(master)$ ^C
[SIGINT]anarcat@curie:~(master)$ 

In powerline:

 anarcat   master  ~  ^C
 anarcat   master  ~  130  

Thanks!

@PH111P PH111P self-assigned this Sep 26, 2019
@PH111P PH111P mentioned this issue Sep 27, 2019
PH111P added a commit that referenced this issue Sep 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants