8 ms·
> Dare I say and ask: How many spaces is a tab? I just checked (because I had a hazy memory of this doing something weird): tab characters printed raw to a PTY
by derefr 5y ago
> Dare I say and ask: How many spaces is a tab?
I just checked (because I had a hazy memory of this doing something weird): tab characters printed raw to a PTY and looked at in a terminal emulator/tmux/etc., aren't any fixed number of spaces wide.
Rather, a tab, at least when rendered onto a PTY, advances the cursor to the next tabstop — i.e. to the next virtual column that is a multiple of 8.
Which has the dreadful implication that, to handle column prediction when there could ever be tabs in the input, you actually need to fully model the rendering behavior of a PTY.
And don't get me started on predicting the width of text printed raw containing ANSI escape codes!
The fact that libncurses works at all, while dealing with all of this, is a never-ending source of amazement to me.
- moonchild 5y ago> you actually need to fully model the rendering behavior of a PTY I think 'input contains control sequences' can be filed away along with 'input contains vertical tab' as 'you're on your own, kid'. Just handling newlines and tabs is relatively trivial. I think it's even a k&r exercise. > libncurses works at all, while dealing with all of this ncurses filters out escape sequences. You can, too (which is probably a nicer approach than attempting to model them).
- derefr 5y agoIt filters out escape sequences on input, yes. But ncurses does generate its own escape sequences (obviously, that's how it does what it does); and so it still has to have a full internal PTY <-> what-a-terminal-emulator-would-do mapping model for what it itself has emitted to the screen, in order to be able to do "damage region" repainting correctly without e.g. suddenly making some text lose color because it jumped to a "display cell" that actually corresponds in the PTY stream to both the text and some SGR escape codes.
- moonchild 5y ago> suddenly making some text lose color because it jumped to a "display cell" that actually corresponds in the PTY stream to both the text and some SGR escape codes Not how the terminal works. Try this: printf " \e[34mhiii\r\e[0mxx\n" This: - Prints a space - Switches to blue - Prints hiii - Moves to the left of the screen - Turns off all attributes - Prints 'xx' (such that the cell where the colour started has definitely been overwritten) This should result in a default-coloured 'xx' followed by blue 'iii'. You do not need a complete model of the terminal. All you need is calculate dirty regions -> redraw them line-by-line in the naive manner. SGR escapes aren't associated with specific cells; you can think of them as commands to the terminal. 'Set your default foreground colour to ...' They could as easily go along a separate channel (barring synchronization overhead, etc.).
- adolph 5y agoOk, now do vertical tab
- wahern 5y ago> Rather, a tab, at least when rendered onto a PTY, advances the cursor to the next tabstop — i.e. to the next virtual column that is a multiple of 8. Multiple of 8 is just convention, though. AFAIU, it's up to the display software--editor, terminal emulator, etc--where to place tab stops. The kernel TTY and PTY layers don't know anything about tab stops, AFAIU. In the case of terminals there are control sequences available for programs to query and update the tab stops; but all the logic is still at the ends. Contrast that with line buffering and certain escape sequences, where character sequences are captured and interpreted by the terminal device driver. Also note that it's tab stops, plural. The default tab stops are multiples of 8, but you can tell a terminal (similar to other display software, like MS Word) to place tab stops at discrete positions that aren't multiples of anything, e.g. 10, 12, 18, 27, etc.
- thechao 5y agoWhen we were taught to type, we used long-stroke hammer typewriters. The tab stop was a bit of metal with notches & clips. You set the tab stop with a ruler, and each time you pressed tab it went to next stop. If your tab stop was worn out, the carriage would go right past the stop!
- ksherlock 5y agoThe Unix kernel has tab knowledge - see tcsetattr(), specifically the c_oflag OXTABS bit: If OXTABS is set, tabs are expanded to the appropriate number of spaces (assuming 8 column tab stops). (XTABS if you're not using a real unix.)