migueldeicaza / SwiftTerm

Xterm/VT100 Terminal emulator in Swift
MIT License
992 stars 146 forks source link

Inset after line breaks on shells without TTY #326

Closed ewilken closed 1 year ago

ewilken commented 1 year ago

Describe the bug

This might very well be a bug in my code, but in that case, (not being an expert in the domain of how terminals work) I would be really thankful for some hint in a right direction, since I saw you helping out a lot of people in other issues super nicely and concise. Thank you in advance for that!

So I'm using SwiftTerm for my SwiftUI Kubernetes management app to implement the behaviors of kubectl exec and kubectl attach.

For exec, I'm executing a shell command (e.g. /bin/sh) in a container that allocates a TTY and wire stdin and stdout to SwiftTerm's send and feed, which works like a charm. I get a prompt, can type to the stdin, and even resizing works nicely.

For attach though, Kubernetes attaches to the main process running inside a container, which might not necessarily allocate a TTY, but usually accepts stdin and spits out stdout and stderr byte streams. So far so good. When I wire e.g. a pod's stdout to SwiftTerm though, stdout lines ending with a 0x0a byte are rendering the next line after the break inset to where the previous line ended (see screenshot below). I have no explanation for this, but maybe there's an easy one with a little more knowledge about terminal internals. 😅

To Reproduce

Attach SwiftTerm to the stdout of a Kubernetes pod that doesn't allocate a TTY and watch newlines being rendered inset.

Expected behavior

The terminal view rendering new lines left-aligned with no inset.

Screenshots

I tried taking a screenshot while logging the bytes I was feeding into SwiftTerm.

The terminal looks like this:

Bildschirmfoto 2023-09-24 um 12 09 01

while being fed:

feeding stdout chunk: [49, 48, 46, 52, 50, 46, 48, 46, 49, 32, 45, 32, 45, 32, 91, 50, 52, 47, 83, 101, 112, 47, 50, 48, 50, 51, 58, 49, 48, 58, 48, 56, 58, 51, 57, 32, 43, 48, 48, 48, 48, 93, 32, 34, 71, 69, 84, 32, 47, 32, 72, 84, 84, 80, 47, 49, 46, 49, 34, 32, 50, 48, 48, 32, 54, 32, 34, 34, 32, 34, 107, 117, 98, 101, 45, 112, 114, 111, 98, 101, 47, 49, 46, 50, 55, 34, 10]
feeding stdout chunk: [49, 48, 46, 52, 50, 46, 48, 46, 49, 32, 45, 32, 45, 32, 91, 50, 52, 47, 83, 101, 112, 47, 50, 48, 50, 51, 58, 49, 48, 58, 48, 56, 58, 52, 57, 32, 43, 48, 48, 48, 48, 93, 32, 34, 71, 69, 84, 32, 47, 32, 72, 84, 84, 80, 47, 49, 46, 49, 34, 32, 50, 48, 48, 32, 54, 32, 34, 34, 32, 34, 107, 117, 98, 101, 45, 112, 114, 111, 98, 101, 47, 49, 46, 50, 55, 34, 10]
feeding stdout chunk: [49, 48, 46, 52, 50, 46, 48, 46, 49, 32, 45, 32, 45, 32, 91, 50, 52, 47, 83, 101, 112, 47, 50, 48, 50, 51, 58, 49, 48, 58, 48, 56, 58, 53, 51, 32, 43, 48, 48, 48, 48, 93, 32, 34, 71, 69, 84, 32, 47, 104, 101, 97, 108, 116, 104, 122, 32, 72, 84, 84, 80, 47, 49, 46, 49, 34, 32, 50, 48, 48, 32, 49, 51, 32, 34, 34, 32, 34, 100, 97, 115, 104, 98, 111, 97, 114, 100, 47, 118, 50, 46, 48, 46, 52, 34, 10]
feeding stdout chunk: [49, 48, 46, 52, 50, 46, 48, 46, 49, 32, 45, 32, 45, 32, 91, 50, 52, 47, 83, 101, 112, 47, 50, 48, 50, 51, 58, 49, 48, 58, 48, 56, 58, 53, 57, 32, 43, 48, 48, 48, 48, 93, 32, 34, 71, 69, 84, 32, 47, 32, 72, 84, 84, 80, 47, 49, 46, 49, 34, 32, 50, 48, 48, 32, 54, 32, 34, 34, 32, 34, 107, 117, 98, 101, 45, 112, 114, 111, 98, 101, 47, 49, 46, 50, 55, 34, 10]

So every line seems to end with a 0x0a line feed for what it's worth.

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context

I can't thank you enough for this library! Thanks a lot for all the work you've done here and thanks a lot in advance for any hint you might have about my bug!

migueldeicaza commented 1 year ago

There is a setting on the terminal that you can set that will turn new line into new line + cr.

You can change this configuration at startup with the TerminalOptions.convertEol, or at runtime with an escape sequence:

https://vt100.net/docs/vt510-rm/DECRQM.html

(You want code 20: LNM)

ewilken commented 1 year ago

Thank you so much! 😊