Search code examples
pythonpython-3.xattributescurses

Python - Curses : how to use the inch method to get the attribute of a character


I am learning python and curses. I am at a point where I want to be able to tell if a specific character is either A_BOLD, A_DIM or A_REVERSE etc... So I could eventually change its attribute accordingly (using for example window.chgat(attr)).

But I do not know how to retrieve this information.

According to the documentation:

window.inch([y, x])¶

Return the character at the given position in the window. The bottom 8 bits are the character proper, and upper bits are the attributes.

I understand that the information about the character attribute is incorporated within the result from inch and as a matter of fact, printing the character obtained displays it with its attributes as well.

But Im not fluent enough in computer speak to understand how to use this. How do I get and interpret those upper bit?... What should I do, say, to check if the character is printed in bold or not?


Solution

  • You need to use bitwise operators (eg &)

    attrs = window.inch([y, x])
    ch = chr(attrs & 0xFF)
    isbold = bool(attrs & curses.A_BOLD)
    

    etc