Python curses won't work properly in cmd or powershell - Stack Overflow

I'm trying to display big ascii text with curses, but it only works in the Visual Studio Code term

I'm trying to display big ascii text with curses, but it only works in the Visual Studio Code terminal for some reason.

Visual Studio Code terminal displays the text correctly

But when I try to run the program in powershell it cuts off the start of the text for some reason, and when I run it in cmd, it just displays a square of question marks.

The windows powershell terminal has the letters cut off at the start, replaced by empty space, and the cmd terminal just displays a box of question marks

What's going on and how do I fix it?

main.py:

import os, curses, time
os.system('cls' if os.name == 'nt' else 'clear')

def main(stdscr):
    stdscr.clear()
    HEIGHT = curses.LINES
    WIDTH = curses.COLS

    logo = []
    with open("logo.txt", mode="r", encoding="utf-8") as dataFile:
        logo = dataFile.readlines()
    
    for i in range(len(logo)):
        stdscr.addstr(logo[i])

    stdscr.refresh()
    stdscr.getkey()


curses.wrapper(main)

logo.txt:

                       #                              
                  #   ##                              
  #  ##########   ##  #########     #####       ####  
   # #  # ## ##    #  #            ##   ##     #   ## 
     #  # ##         #            ##     ##   ##      
       ## ##  #  #  ##########   ##       #   ##      
 ##   ##   ####  ##   #  #   #   ##       ##  ###     
   # ##  #         #  #  #   #   ##       ##   ####   
         #          ###########  ##       ##     #### 
     ##########    #  #  #  ##   ##       ##       ## 
   #     ##        #  #  #  ##   ##       #         ##
   #    ####      ## ##  #  ##    ##     ##         ##
  ##   # #  #     #  ##########    ##   ##    ##   ## 
 ##  ##  #   ##  ##  #      #       #####      #####  
 #   #   #       #       ####                         

I tried debugging by addstr-inging different variations of "A" and "AAAAAAAAAAAAAAAAAAAAAAAA" instead the ascii font, which worked normally.

I also tried changing the text to become smaller, in case the text got looped or something in the powershell terminal, but the same problem persisted.

Lastly I tried removing the first character for the same reason. The cut-off point seemed to be the same place on the screen, with less text meaning less of the text could peek past the blacked out area.

I'm trying to display big ascii text with curses, but it only works in the Visual Studio Code terminal for some reason.

Visual Studio Code terminal displays the text correctly

But when I try to run the program in powershell it cuts off the start of the text for some reason, and when I run it in cmd, it just displays a square of question marks.

The windows powershell terminal has the letters cut off at the start, replaced by empty space, and the cmd terminal just displays a box of question marks

What's going on and how do I fix it?

main.py:

import os, curses, time
os.system('cls' if os.name == 'nt' else 'clear')

def main(stdscr):
    stdscr.clear()
    HEIGHT = curses.LINES
    WIDTH = curses.COLS

    logo = []
    with open("logo.txt", mode="r", encoding="utf-8") as dataFile:
        logo = dataFile.readlines()
    
    for i in range(len(logo)):
        stdscr.addstr(logo[i])

    stdscr.refresh()
    stdscr.getkey()


curses.wrapper(main)

logo.txt:

                       #                              
                  #   ##                              
  #  ##########   ##  #########     #####       ####  
   # #  # ## ##    #  #            ##   ##     #   ## 
     #  # ##         #            ##     ##   ##      
       ## ##  #  #  ##########   ##       #   ##      
 ##   ##   ####  ##   #  #   #   ##       ##  ###     
   # ##  #         #  #  #   #   ##       ##   ####   
         #          ###########  ##       ##     #### 
     ##########    #  #  #  ##   ##       ##       ## 
   #     ##        #  #  #  ##   ##       #         ##
   #    ####      ## ##  #  ##    ##     ##         ##
  ##   # #  #     #  ##########    ##   ##    ##   ## 
 ##  ##  #   ##  ##  #      #       #####      #####  
 #   #   #       #       ####                         

I tried debugging by addstr-inging different variations of "A" and "AAAAAAAAAAAAAAAAAAAAAAAA" instead the ascii font, which worked normally.

I also tried changing the text to become smaller, in case the text got looped or something in the powershell terminal, but the same problem persisted.

Lastly I tried removing the first character for the same reason. The cut-off point seemed to be the same place on the screen, with less text meaning less of the text could peek past the blacked out area.

Share Improve this question asked Nov 16, 2024 at 15:54 LucyLucy 11 bronze badge 9
  • You have a problem with utf-8 encoded characters and compatibility. By swapping -f and -t in this: $ iconv -f iso-8859-1 -o utf-8 logo.txt I can go from displaying the above, to a block of ? - back and forth. My understanding of the problem, based on this:: You need to set curses [affecting stdscr.addstr() ] to appropriately render utf-8 characters regardless of environment. (Tested in Ubuntu Terminal) – Hannu Commented Nov 16, 2024 at 16:57
  • 1 import sys;sys.stdout.reconfigure(encoding='utf-8') does the trick for stdout in git Bash (terminal) on windows. – Hannu Commented Nov 16, 2024 at 17:03
  • 1 I am not sure I understand why it has to be so difficult. Maybe you have your reason. Why use curses, if you are using it only to print on stdout very plainly (character after character, and a newline between lines)? If your logo what in the form of the list of coordinates of the #, I would have understood: curses gives some comfort to wmove position on the screen. Or if you used some fancy color (and didn't want to use almost universal but non-portable ANSI \033[... stuff). – chrslg Commented Nov 16, 2024 at 17:46
  • You don't even use curses for the only thing you could have used it for (you use some OS specific ugly system to call cls, when curses provides clear for thart. – chrslg Commented Nov 16, 2024 at 17:48
  • 1 @chrslg I use os to clear because otherwise you can scroll up the terminal to previous code, and I use curses because I'm going to make a whole terminal program. The text being displayed is just me testing as well as a fake boot screen for the terminal program. – Lucy Commented Nov 17, 2024 at 10:03
 |  Show 4 more comments

2 Answers 2

Reset to default 0

I fixed the powershell issue where half the text got cropped by addstr-inging each separately with coordinates, although this caused a new issue of powershell fetting how it displays and instead showing question marks. This also meant that the empty space characters got removed, which means that cmd can display only the question marks for the s.

Both cmd and powershell now display the letters in question marks

Since it seems neither powershell or cmd are able to display the s, I will have to generate text that uses another symbol instead.

One way to BYPASS the problem is to convert the text info characters that are generally printable: this is fairly simple as UTF-8 has most (all?) of the ISO-8859-1 characters in the first 256 codepoints.

$ sed < logo.txt -re 's/\xe3\x80\x80/./g' -e 's/\xef\xbc\x83/#/g' ; echo 
.......................#..............................
..................#...##..............................
..#..##########...##..#########.....#####.......####..
...#.#..#.##.##....#..#............##...##.....#...##.
.....#..#.##.........#............##.....##...##......
.......##.##..#..#..##########...##.......#...##......
.##...##...####..##...#..#...#...##.......##..###.....
...#.##..#.........#..#..#...#...##.......##...####...
.........#..........###########..##.......##.....####.
.....##########....#..#..#..##...##.......##.......##.
...#.....##........#..#..#..##...##.......#.........##
...#....####......##.##..#..##....##.....##.........##
..##...#.#..#.....#..##########....##...##....##...##.
.##..##..#...##..##..#......#.......#####......#####..
.#...#...#.......#.......####.........................

... here at S.O. it looks a bit squished (condensed) as the characters in the monospace font are narrower.
It might need some adaptation to be as nice.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745653548a4638408.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信