How to make arrow symbols in terminal?


Zink

Member
Joined
Mar 8, 2012
Messages
170
I am trying to write a small console app in Python using Curses, and I need to add some arrow key symbols.


I suppose this should be done this way:


win.addch(1, 1, curses.ACS_UARROW, curses.A_ALTCHARSET)


but it doesn't work. For ACS_UARROW I'm getting ^


and for ACS_DARROW I'm getting ┴


Are the arrows available somehow?
 
Funny, I was never even aware of the ACS_UARROW constant before. Because I prefer Unicode! There are many arrow characters available, and the curses documentation tells you how to make it work with Unicode. Your code will look something like this:



Code:
import locale

locale.setlocale(locale.LC_ALL, '')

code = locale.getpreferredencoding()

ARROW_UP = '\u2191'.encode(code)

win.addstr(1, 1, ARROW_UP) # I think you might need addstr instead of addch for this; try it and see!


Let me know how that works for you.
 
Thanks. Unicode was the first thing I tried, but it didn't work (probably because I know very little about unicode and python in general :) ).


Anyway, your code produces slightly unexpected results. It shows: \u2191. I suppose you meant this string instead: u'\x2191', which in turn produces: !91


I also tried u'\x21\x91', but this one produces UnicodeEncodeError: 'ascii' codec can't encode character u'\x91' in position 1: ordinal not in range(128) error.


Maybe this is a default encoding problem, because code is set to: ANSI_X3.4-1968


I also tried calling sys.setdefaultencoding('utf8') before your code (as proposed in some articles), but it didn't change anything (code is still ANSI...).
 
Last edited by a moderator:
Oh no! You don't want to have \x in a unicode string. But you are right, it should be prefixed with a u, so that should be u'\u2191'.encode.


See, unicode lets you specify the exact character you want. Different encodings then have different ways of representing that character as a string of bytes. When you use \u, you're requesting an exact character. When you you call encode on the unicode character, it's converted to the appropriate byte representation. But when you use \x, you specify the bytes to use. All of this is much smoother in Python 3, but that doesn't help here.


However, unicode isn't going to be useful if the Pandora's terminal doesn't support it. I don't have one to test, so I can't tell you. Try running "print u'\u2191", and see if you get an arrow. If not, you might have to go with ugly characters like "<^v>".
 
The default locale on the pandora is ISO8859, maybe that could cause problems?


(Personally, I think it's time to step out from the stone ages and make UTF-8 the default character encoding)
 
Try running "print u'\u2191", and see if you get an arrow. If not, you might have to go with ugly characters like "<^v>".

That raises a UnicodeEncodeError: "'ascii' codec can't encode character u'\u2191' in position 0: ordinal not in range(128)".


So yeah, gotta use ASCII I guess. :) Just use "^" (caret) for up and "v" (lowercase V) for down.
 
Last edited by a moderator:
Try running "print u'\u2191", and see if you get an arrow. If not, you might have to go with ugly characters like "<^v>".

That raises a UnicodeEncodeError: "'ascii' codec can't encode character u'\u2191' in position 0: ordinal not in range(128)".


So yeah, gotta use ASCII I guess. :) Just use "^" (caret) for up and "v" (lowercase V) for down.
Same error for me. I guess I will have to stick to ^ and v.


I also tried printing all characters from normal and alternative charset (curses), and found no arrow anywhere. But strangely, quite many of the characters were inverted "?". What a waste. Is it possible somehow to create own characters and put them there for one session?
 
Back
Top