Attribute Bold Doesn't Seem to Work in My Curses

Python curses.flash() work only under certain conditions but theses doesn't seem to be documented

flash in curses uses the flash capability in the terminal description. For example, using infocmp, you may see something like this in the output:

flash=\E[?5h$<100/>\E?5l,

That tells the application to send an escape sequence, wait 100 milliseconds, and send another escape sequence. (Not all "xterm" wanna-be's implement DECSCNM; that defect is usually noted in the terminal database comments);

All screen updates in curses are buffered; flash is an update. You can explicitly flush that buffer to the terminal using refresh, or rely upon functions which read input (which as a side-effect will do a refresh). If your program is prevented from writing to the terminal, you'll see no flashing.

Whether you call getch or getkey, that normally blocks until you press a key. You can use timeout, nodelay, etc., which are mentioned in the documentation.

By the way, time.sleep will interfere with the refresh. Use napms.

For example:

> diff -u foo.orig foo
--- foo.orig 2022-07-20 03:53:05.000000000 -0400
+++ foo 2022-07-20 03:56:18.833794715 -0400
@@ -8,10 +8,14 @@
curses.cbreak()
curses.curs_set(0)
stdscr.keypad(True)
-
+ stdscr.nodelay(1)
+
while True:
curses.flash()
- time.sleep(0.5)
- # ~ stdscr.getkey()
-
+ curses.napms(500)
+ try:
+ stdscr.getkey()
+ except curses.error:
+ pass
+
curses.wrapper(test)

Further reading:

  • Curses Programming with Python
  • beep, flash - curses bell and screen flash routines
  • Reading characters (getch)

OP comments that calling use_default_colors helps with flash. That depends (as does DECSCNM) upon the terminal, but the reason why it would make a difference is because the python curses wrapper calls start_color, which causes ncurses to try to color the terminal with white-on-black. Calling use_default_colors partly cancels that, because there are no explicit colors used in the script, so that you'll see only the terminal's default colors. If the terminal emulator shows DECSCNM only when using the default (uncolored) configuration, then that's what you'd get for a "working" flash.

As far as ncurses is concerned, "flash" is separate from the details of color and refresh (see source). It only happens to rely upon the buffer-flushing done in a refresh.

Curses set bright and dark backgrounds

I still don't seem to have any way to set a bright background color.

This question is old, but in case someone will look for answer to it, you just have to combine curses.A_BOLD with curses.A_REVERSE, for example:

curses.init_pair(1, curses.COLOR_GREEN, curses.color_BLACK);
// Pair with green as a foreground color and black as a background
curses.color_pair(1);
// Pair with light green as a background color and black as a foreground
curses.color_pair(1) | curses.A_BOLD | curses.REVERSE;

... and now you have 2 × more pairs :)

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

You need to use bitwise operators (eg &)

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

etc

How to move pad correctly?

Check what values do you get for up/down arrows and compare them with curses.KEY_UP/DOWN. See My cursor keys do not work.

For example, key-up returns as 3 characters on my terminal:

import curses

with curses_screen() as stdscr:
pad = curses.newpad(100, 100)
pad.addstr(0,0, curses.longname())
for i in range(1, 10):
pad.addstr(i,0, str(i))

coord = 5, 5, 20, 75
pad.refresh(0, 0, *coord)

KEY_UP, KEY_DOWN = 'AB'
y = 0
for c in iter(pad.getkey, 'q'):
if c in '\x1b\x5b': continue # skip escape seq
y -= (c == KEY_UP)
y += (c == KEY_DOWN)
y = min(max(y, 0), 9)
pad.refresh(y, 0, *coord)

Definition of curses_screen().



Related Topics



Leave a reply



Submit