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

Incorrect behavior of ANSI '\033[K' at the end of a line. #15802

Closed
latlaj opened this issue Aug 7, 2023 · 6 comments
Closed

Incorrect behavior of ANSI '\033[K' at the end of a line. #15802

latlaj opened this issue Aug 7, 2023 · 6 comments
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Issue-Question For questions or discussion Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Resolution-Answered Related to questions that have been answered

Comments

@latlaj
Copy link

latlaj commented Aug 7, 2023

Windows Terminal version

1.17.11461.0

Windows build number

10.0.22621.0

Other Software

OpenSSH_for_Windows_8.6p1, LibreSSL 3.4.3
grep (GNU grep) 3.0

Steps to reproduce

First ssh to linux.
Creat a file 'test.txt' like this:

abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdef
grep 'abcd' test.txt --color=always

If Colored String at the end of the line, the last character will be lost. If Colored String at the begin of the line, the last character of the previous line will be lost.

grep 'abcd' test.txt --color=always > test.color

download the test.color file, it looks like:

\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef\033[01;31m\033[Kabcd\033[m\033[Kef

It seems like \033[K clean the last character. How can I avoid it?

Expected Behavior

No response

Actual Behavior

char-lost

@latlaj latlaj added Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Aug 7, 2023
@zadjii-msft
Copy link
Member

what

@lhecker any ideas if is this the same thing you were also just seeing with ssh?

@lhecker
Copy link
Member

lhecker commented Aug 7, 2023

No, that one only happens with OpenConsole.

@j4james
Copy link
Collaborator

j4james commented Aug 7, 2023

There are two issue to be aware of here:

  1. When you write out a line of text that's exactly the width of the screen, the cursor doesn't immediately wrap onto the next line, but instead sets a flag and leaves the cursor in the last column. If more text is received while that flag is set, the cursor will move to the next line before adding the new content to the buffer. But if the terminal receives an EL command at that point, it's going to erase the character in the last column, because that's still where the cursor is positioned.

  2. There are a number of operations that are expected to reset that "end of line" flag, and EL is one of them. So after you've executed that EL command, any additional text you output at that point is expected to start in the last column rather than on the next line.

What we're seeing in the screen capture above is the effect of part 1, but not part 2. So that's why one of the characters is being blanked out rather than overwritten. We only implemented part 2 in PR #14936, which I don't think is included in version 1.17.

Either way you're going to end up with a missing character if you execute EL at the end of a line, but that's expected behavior. This is technically a bug in grep, because it really shouldn't be using EL like that. You can read more about it in the XTerm FAQ.

@j4james
Copy link
Collaborator

j4james commented Aug 7, 2023

I should also note that there was a workaround for this issue that was mentioned in the grep bug report. If you do something like this:

GREP_COLORS="ne" grep 'abcd' test.txt --color=always

Then grep won't inject those EL sequences everywhere, so you shouldn't lose any characters.

@latlaj
Copy link
Author

latlaj commented Aug 8, 2023

@j4james Thanks a lot for finding the answer for me.

@zadjii-msft
Copy link
Member

Well, I think that answers that. Thanks for the detailed writeup!

@zadjii-msft zadjii-msft closed this as not planned Won't fix, can't repro, duplicate, stale Aug 8, 2023
@zadjii-msft zadjii-msft added Issue-Question For questions or discussion Resolution-Answered Related to questions that have been answered labels Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug It either shouldn't be doing this or needs an investigation. Issue-Question For questions or discussion Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Resolution-Answered Related to questions that have been answered
Projects
None yet
Development

No branches or pull requests

4 participants