admin管理员组文章数量:1026925
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.
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 [affectingstdscr.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 forstdout
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 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: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 callcls
, when curses providesclear
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
2 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.
本文标签: Python curses won39t work properly in cmd or powershellStack Overflow
版权声明:本文标题:Python curses won't work properly in cmd or powershell - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745653547a2161462.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