1

I tried to display colored characters using ascii escape sequences, but it shows weird character and the option I set. "[0M" ← this is the copy and pasted character from command prompt. I think this should not be printed by system.

I tried to use escape sequences using both python interpreter and C codes running on Windows command prompt. The C compiler is GNU compiler.

here is the python code.

>>> print("12345\33[5D67890")
12345[5D67890

and C code.

#include <stdio.h>

int main()
{
    printf("12345\033[5D67890\n");
}

and results

12345[5D67890

I noticed that the browser or something doesn't show my weird character properly, here is the screenshot of my weird character

my weird character

also I copy and paste my weird character into ascii to keycode converter. It shows that the weird character's keycode is "027" which is the escape sequence keycode in denary number.

rici
  • 234,347
  • 28
  • 237
  • 341
erurami
  • 11
  • 3

1 Answers1

0

I did some local testing and found that the issue is either one of two things. If you end your print in a newline, the cursor is already at the start of the line and can not go back further. The second issue is that if you remove the newline, the output will get buffered unless you manually flush it.

The following example will output a string and then place the cursor back 5 characters:

In [15]: def foo(): 
    ...:     sys.stdout.write("1234567890\33[5D") 
    ...:     sys.stdout.flush() 
    ...:     time.sleep(10) 
    ...:                                                                                                                                   

In [16]: foo()                                                                                                                             
1234567890
#    ^ cursor stays here until sleep finishes
jordanm
  • 33,009
  • 7
  • 61
  • 76