Skip to content

ANSI Control sequences close stdout in Console, preventing any following output in the same script #14941

Closed

Description

@jucor

System details

RStudio Edition : Desktop
RStudio Version : Version 2024.04.2+764 
OS Version      : macOS Sonoma 14.5
R Version       : 4.4.1

also tested on

RStudio Edition : Desktop
RStudio Version : 2024.04.2 Build 764 
OS Version      : Windows Server 2022 Datacenter 21H2
R Version       : 4.4.1

Steps to reproduce the problem

  1. Copy the following code into test_control_various.R
write("I am about to write some ANSI cursor control sequence to stdout in the next line...\n", stdout())
write("Are Control Sequences Working in write (Y/N): N\x1b[1DY\n", stdout())
# Note also that in Terminal, the above line ends in Y, whereas in the Console it ends in N...
write("This is not printed to the console anymore if this script was sourced from the Console.\n", stdout())
write("But does get printed if we ran this file via Rscript in the Terminal!\n", stdout())
write("And this does get sent to a file correctly any way -- so the script does run, but stdout in console is ruined.","myfile.txt" )
  1. From the console, source that R file: source('./test_control_various.R'). Observe that only the first two write calls display anything. All the outputs are missing after the second write, which contains the ANSI cursor-moving control sequence "\x1b[1D". But a file is correctly created by the final call, so the problem is purely a "print-to-stdout" issue, not an execution problem.

  2. For comparison, use Rscript to source that R file in the terminal: Rscript ./test_control_various.R and see that all the outputs are present.

Describe the problem in detail

Using cursor movement ANSI Control sequences seems to close stdout for the rest of the script: no further output from that script is displayed in the console.
The console must reopen it afterwards, as future commands outside of that script work.

The problem does not occur when using Rscript to run the same script in the Terminal.

Describe the behavior you expected

I expect the write calls to still display text in the console after the one call that contains a control sequence.

I know from #9478 (comment) that the Console uses another rendering than the Terminal.

I also see from

* such as cursor movement are ignored.
that the cursor control sequences are ignored (I'll open a feature request for that and link it here), but that should not prevent the follow-up displays.

I suspect this will be of interest in particular to @gtritchie who is the master of all things Console and ANSI :)

Thanks for your help!

  • I have read the guide for submitting good bug reports.
  • I have installed the latest version of RStudio, and confirmed that the issue still persists.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

Activity

  1. added
    new
    on Jul 8, 2024
  2. added and removed
    new
    on Jul 9, 2024
  3. kevinushey commented on Jul 9, 2024

    @kevinushey
    Collaborator

    Somewhat more succinctly, writeLines("N\x1b[1DY") gives:

    > writeLines("N\x1b[1DY")
    Y
    

    in a plain R session, but gives:

    > writeLines("N\x1b[1DY")
    N
    

    I believe RStudio's console only supports ANSI escapes typically used for colors, which implies the escape is normally closed via \x1b[m. Note that the "regular" backspace character is handled correctly, though.

    > writeLines("N\x08Y")
    Y
    
  4. jucor commented on Jul 9, 2024

    @jucor
    Author
  5. added this to the Kousa Dogwood milestone on Jul 9, 2024
  6. ronblum commented on Aug 29, 2024

    @ronblum
    Contributor

    I tested using the example in the QA notes, and got the expected result:

    > writeLines("Hello world!\x1b[10DLL\x1b[4CRL")
    HeLLo woRLd!
    

    However, I also tried this variation, going back 55, which changes the line with the command on it:

    > wriLLLineRL"Hello world!\x1b[55DLL\x1b[4CRL")
    Hello world!
    

    Also, going forward 5 executed the next line in a script, so running the first line

    writeLines("Hello world!\x1b[10DLL\x1b[5CRL")
    1 + 1
    

    results in

    > writeLines("Hello world!\x1b[10DLL\x1b[5CRL")
    HeLLo worRL!
    > 1 + 1
    [1] 2
    

    @kevinushey I see why this happens, but is this the behavior that we want?

  7. kevinushey commented on Aug 29, 2024

    @kevinushey
    Collaborator

    I think this is fine.

  8. jucor commented on Aug 29, 2024

    @jucor
    Author

    OK, how about the fact that subsequent write() aren't displayed after this one?

    write("Are Control Sequences Working in write (Y/N): N\x1b[1DY\n", stdout())
    

    Where you able to reproduce?
    Thanks for your work on this!

  9. kevinushey commented on Aug 29, 2024

    @kevinushey
    Collaborator

    Thanks for catching that; this appears to still be an issue.

    > write("Are Control Sequences Working in write (Y/N): N\x1b[1DY\n", stdout())
    Are Control Sequences Working in write (Y/N): Y
    
    > write("Do you see me?")
    >
    
  10. kevinushey commented on Aug 29, 2024

    @kevinushey
    Collaborator

    Sorry, yes, it is working -- I forgot to include stdout() in my second usage.

    > write("Are Control Sequences Working in write (Y/N): N\x1b[1DY\n", stdout())
    Are Control Sequences Working in write (Y/N): Y
    
    > write("Do you see me?", stdout())
    Do you see me?
    
  11. jucor commented on Aug 29, 2024

    @jucor
    Author

    Terrific, thanks!

  12. ronblum commented on Sep 10, 2024

    @ronblum
    Contributor

    Verified in

    • RStudio Desktop Pro 2024.10.0-daily+131.pro1 on Windows 11
    • RStudio Server 2024.10.0-daily+136 on Ubuntu 22 via Edge on Windows 11

    using the original script and the series of examples throughout this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions