0

This is a follow-up to a question from from 7 years ago [see Link *]. I'd like a built-in or portable way to enable live-mouse updates AND extended colors, in a python 3 curses application that currently runs on a Windows Cygwin Xterm. I don't want to define a custom $TERM. I have tried different TERM values and different print escape statements to no avail -- my only workaround is to swap the OS ENV TERM values between xterm-1003 (supports live-mouse) and xterm-256color (supports colors), but I want both without creating a new $TERM!

Relevant links:

Python application version: 3.9
Curses module version: 2.2
Windows Cygwin Terminal env defaults:

TERM_PROGRAM=mintty
TERM_PROGRAM_VERSION=3.6.1
TERM=xterm-256color
SHELL=/bin/bash

Code (See the 2nd FIXME for the function I would like to have ALONG with a live-mouse):

import sys
assert sys.version_info > (3,4)
import locale
locale.setlocale(locale.LC_ALL,"")
import os
#os.environ['TERM'] = 'xterm-1003' # For live-tracking mouse movement: https://stackoverflow.com/questions/29020638/which-term-to-use-to-have-both-256-colors-and-mouse-move-events-in-python-curse/70735193#70735193
os.environ['TERM'] = 'xterm-256color' # [Default] For extended colors
#print('\033[?1003h') # FIXME Doesn't do anything, could this work instead of setting TERM??
import curses, _curses

def cleanup():
    curses.endwin()
    curses.flushinp()

curses.initscr()
try:
    assert curses.has_colors()
    #assert curses.has_extended_color_support() # Python 3.10 only
    #assert curses.can_change_color() # FIXME Fails if TERM=xterm-1003
except:
    cleanup()
    raise
curses.flushinp()
curses.start_color()
mm = curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION)
curses.mouseinterval(0) # Prevents delay after L/R mouse-clicks
curses.cbreak()
curses.curs_set(0)

def window(screen):
    screen.timeout(0)
    screen.keypad(True)
    screen.nodelay(True)
    screen.clear()
    screen.bkgd(' ', 0)
    screen.addstr(0, 0, f"0x{curses.ALL_MOUSE_EVENTS:x} 0x{curses.REPORT_MOUSE_POSITION:x}, 0x{mm[0]:x} 0x{mm[1]:x}") # Is OK
    i = 0
    while True:
        i+=1
        screen.addstr(1, 0, f"{i}")
        _ = screen.getch()
        try:
            id, x, y, _, bstate = curses.getmouse()
            screen.addstr(2, 0, f"{y}, {x}, {id}, 0x{bstate:x}")
        except:
            continue
        screen.addch(y, x, "X")
        screen.refresh()
curses.wrapper(window)
MrMattBusby
  • 116
  • 5

0 Answers0