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 | Show 4 more comments2 Answers
Reset to default 0I 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
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 [affectingstdscr.addstr()
] to appropriately render utf-8 characters regardless of environment. (Tested in Ubuntu Terminal) – Hannu Commented Nov 16, 2024 at 16:57import sys;sys.stdout.reconfigure(encoding='utf-8')
does the trick forstdout
in git Bash (terminal) on windows. – Hannu Commented Nov 16, 2024 at 17:03#
, I would have understood:curses
gives some comfort towmove
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:46system
to callcls
, when curses providesclear
for thart. – chrslg Commented Nov 16, 2024 at 17:48