0

I am making a program involving ANSI escape codes, and they were all working fine on replit until I switched back to IDLE (3.9 on both). But it doesn't work:
IDLE
it should have looked like this:
replit

I have seen several posts before that complain that the IDLE doesn't support these escape sequences because it isn't a terminal, so I tried to do it directly from the but the beastly symbol still appeared, this time as a boxed question mark:
cmd

I know that it won't work straight from the IDLE, so I wonder if you can import a software like into python?

Powershell works though... powershell pic

P.S. please don't tell me to import colorama or something! I really want this to be the way. I also don't have immediate access to iPython (even though I would like to) so it's not really an option for me... unless I have to :D

EDIT: the code I put across the python programs:

import sys, os

os.system("")

CSI = f"{chr(0x1B)}["

print(f"""{CSI}3m{CSI}1m{CSI}4m{CSI}31m
look at this""")

sys.stdout.flush() 

# I put the sys.stdout.flush() and os.system("") to try and fix the problem...
Arale
  • 39
  • 1
  • 8
  • IDLE doesn't make a very good terminal. My biggest grief is that a very long output line will bog it down so it becomes almost unusable. – Mark Ransom Mar 14 '21 at 04:43
  • @MarkRansom yes I know I hate it when I do help( ) of something long like "CLASSMETHODS" or something :P – Arale Mar 14 '21 at 12:17

3 Answers3

1

The IDLE shell is not a terminal emulator and not intended to be production environment. The decision so far is that is should show program developers what their program's output, without interpretation. This may change in the future but no final decision yet.

If you are on Windows, I believe its console can be put into ANSI mode, but Python does not do that for you. I don't know if program code can do so.

As near as I can tell, there is not wrapper program that turns mintty into an importable python module. It would not make much sense. Rather, you would want to open mintty or a mintty-based terminal-emulator, such as git bash, and open python within that terminal instead of CommandPrompt.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • Starting with Python 3.6 the console I/O bypasses the usual byte I/O so that full Unicode can be supported. If escape sequences worked before then I doubt they work after. – Mark Ransom Mar 14 '21 at 04:47
  • @Arale I don't know anything about colorama, it's possible that they found a way to make it work but it wouldn't be the same code. – Mark Ransom Mar 14 '21 at 18:16
1

ANSI code is a broad term. You have to specify which code page you are using. For example, my Windows is in Chinese Simplified. Therefore if I want to escape from UTF-8 default in Python, I would put # coding : cp936 on the first or second line of a script. Then it can read and write text files with the simplified Chinese coding.

George Y
  • 525
  • 3
  • 14
  • I was talking about terminal colours and configurations with `\x1b, \033, or chr(0x1B)` – Arale Mar 14 '21 at 14:17
  • And hopefully any others (full list on wiki link) – Arale Mar 14 '21 at 14:18
  • So far as I know, no ANSI code by Microsoft definition includes any color theme, otherwise you cannot change font color inside Microsoft Office. If you want color theme on syntax, try another IDE like VS code. – George Y Mar 15 '21 at 03:00
0

Second Question:

Could I make a red/green/etc. font for every character and put it as print('...', file=...)? It should be possible because colored emojis exist.

It should work, but I would like to know how I could (if it's possible) automate this with some program that makes a file containing those characters and displays them with the previous statement.

Cheers!

Arale
  • 39
  • 1
  • 8