termstandard / colors

Color standards for terminal emulators
The Unlicense
1.1k stars 44 forks source link

Bugfix: Example causes standard input hang #28

Closed DanielDewberry closed 2 years ago

DanielDewberry commented 2 years ago

This example prints the escape sequence to standard out, then launches xxdwhich waits for input on standard input.

$ (echo -e '\e[48:2:1:2:3m\eP$qm\e\\' ; xxd)

Change the ; to a pipe send the escape sequence into xxd

$ (echo -e '\e[48:2:1:2:3m\eP$qm\e\\' | xxd)

Example output:

$ (echo -e '\e[48:2:1:2:3m\eP$qm\e\\' | xxd)
00000000: 1b5b 3438 3a32 3a31 3a32 3a33 6d1b 5024  .[48:2:1:2:3m.P$
00000010: 716d 1b5c 0a                             qm.\.
kurahaupo commented 2 years ago

@DanielDewberry

Sorry but this PR completely missed the point.

This patch would cause xxd to print the bytes that echo outputs. That would be pointless, as it wouldn't tell you anything about the status of the terminal.

The objective is to send a control sequence to the terminal, which is a request that causes the terminal to reply with a control sequence (as if it had been typed) and then for xxd to read those bytes and display them in a printable form to make them easier to understand.

You then need to press ctrl-D to finish the input to xxd, one or twice until you get the shell prompt back. (You will probably need to press it twice, but if one suffices to get your shell prompt back, you don't want to press it again, because that will log you out.) If the terminal does not respond to the enquiry sequence, nothing will be printed, and you still need to press ctrl-D until your shell prompt returns.

A more sophisticated version would be to use the read built-in command, with a time limit:

IFS= read -s -p $'\e[48:2:1:2:3m\eP$qm\e\\' -N50 -t1 reply
printf %q\\n "$reply"
printf %s "$reply" | xxd

That's

DanielDewberry commented 2 years ago

@kurahaupo Thank you for your thoughtful reply. I accept what you have said and recognise my error.
The inclusion of this line 00000000: 1b50 3124 7234 383a 323a 313a 323a 336d .P1$r48:2:1:2:3m is what confused me as I had executed the command ((echo -e '\e[48:2:1:2:3m\eP$qm\e\\' ; xxd)) in Konsole which waited for input on stdin and yielded no further information from xxd upon sending the the CTRL-D signal.

(echo -e '\e[48:2:1:2:3m\eP$qm\e\\' ; xxd)
2:1:2:3m

I then ran it piped into xxd which (misleadingly resembled the stated behaviour) and then ran the command without xxd which yielded the same output, without the hang.

(echo -e '\e[48:2:1:2:3m\eP$qm\e\\' )
2:1:2:3m

It was not until moments ago that I loaded https://onlinegdb.com and executed the command with xxd that i received similar output to the stated behaviour upon pressing CTRL-D

^[P1$r0m^[\0000000: 1b50 3124 7230 6d1b 5c                   .P1$r0m.\

Thank you for your time and thorough explanation