def display_info(stdscr): display_info_line(stdscr, "COLS: {}".format(curses.COLS)) display_info_line(stdscr, "LINES: {}".format(curses.LINES)) display_info_line(stdscr, "Bold", curses.color_pair(0) | curses.A_BOLD) display_info_line(stdscr, "Blink", curses.A_BLINK) display_info_line(stdscr, "Blink + Standout", curses.A_BLINK | curses.A_STANDOUT) display_info_line(stdscr, "Stand out", curses.A_STANDOUT) display_info_line(stdscr, "Dim", curses.A_DIM) display_info_line(stdscr, "Underline", curses.A_UNDERLINE) display_info_line(stdscr, "Normal", curses.A_NORMAL) display_info_line(stdscr, "Reverse", curses.A_REVERSE) display_info_line(stdscr, "Color", curses.A_COLOR) display_info_line(stdscr, "Chartext", curses.A_CHARTEXT) display_info_line(stdscr, "Low", curses.A_LOW) display_info_line(stdscr, "Left", curses.A_LEFT) display_info_line(stdscr, "Can change color pair: %s" % curses.can_change_color()) display_info_line(stdscr, "Color pairs: %s" % curses.COLOR_PAIRS) display_info_line(stdscr, "# of colors: %d" % curses.COLORS) if curses.can_change_color(): display_info_line(stdscr, "The terminal should show a green gradient below. If it's colorful instead, curses can't change terminal color.") matrix.init_colors() for i in range(matrix.NUMBER_OF_COLOR): color_num = matrix.START_COLOR_NUM + i display_info_line(stdscr, "Pair {} {} {}".format(color_num, curses.color_content(color_num), curses.pair_content(color_num)), curses.color_pair(color_num)) stdscr.getch()
def display_info(stdscr): display_info_line(stdscr, "COLS: {}".format(curses.COLS)) display_info_line(stdscr, "LINES: {}".format(curses.LINES)) display_info_line(stdscr, "Bold", curses.color_pair(0) | curses.A_BOLD) display_info_line(stdscr, "Blink", curses.A_BLINK) display_info_line(stdscr, "Blink + Standout", curses.A_BLINK | curses.A_STANDOUT) display_info_line(stdscr, "Stand out", curses.A_STANDOUT) display_info_line(stdscr, "Dim", curses.A_DIM) display_info_line(stdscr, "Underline", curses.A_UNDERLINE) display_info_line(stdscr, "Normal", curses.A_NORMAL) display_info_line(stdscr, "Reverse", curses.A_REVERSE) display_info_line(stdscr, "Color", curses.A_COLOR) display_info_line(stdscr, "Chartext", curses.A_CHARTEXT) display_info_line(stdscr, "Low", curses.A_LOW) display_info_line(stdscr, "Left", curses.A_LEFT) display_info_line(stdscr, "Can change color pair: %s" % curses.can_change_color()) display_info_line(stdscr, "Color pairs: %s" % curses.COLOR_PAIRS) display_info_line(stdscr, "# of colors: %d" % curses.COLORS) if curses.can_change_color(): display_info_line(stdscr, "The terminal should show a green gradient below. If it's colorful instead, curses can't change terminal color.") matrix.init_colors() for i in range(matrix.NUMBER_OF_COLOR + 1): color_num = matrix.START_COLOR_NUM + i display_info_line(stdscr, "Pair {} {} {}".format(color_num, curses.color_content(color_num), curses.pair_content(color_num)), curses.color_pair(color_num)) stdscr.getch()
def make_colors() -> bool: print(curses.can_change_color()) if curses.can_change_color(): curses.init_color(1, 98, 114, 164) curses.init_color(2, 139, 233, 253) curses.init_color(3, 80, 250, 123) curses.init_color(4, 255, 184, 108) return True return False
def __init__(self, fileName): """ """ self.logger = logging.getLogger('FrontEnd') self.cursor = Cursor.Cursor(fileName) self.stdscr = curses.initscr() curses.def_shell_mode() self.fileName = './tmp/'+fileName curses.noecho() screenHeight, screenWidth = self.stdscr.getmaxyx() self.statusWin = curses.newwin( 1,screenWidth, screenHeight-1,0) self.leftBorder = curses.newwin( screenHeight-0,4, 0,0) self.contentWin = curses.newwin( screenHeight, screenWidth-3, 0,4) self.height, self.width = self.contentWin.getmaxyx() bY,bX = self.contentWin.getbegyx() self.cursorWindow = CursorWindow.CursorWindow(0,0,self.width-1,self.height-1, 0) self.stdscr.clear() self.stdscr.keypad(True) if curses.can_change_color(): # init_color(n, r, g, b) # n=0 is background curses.start_color() curses.init_color(0, 255, 255, 255) self.fh = open(self.fileName, 'rb') self.RedrawBuffer() self.stdscr.move(0,4)
def init_color(self, t): """ Attempts to allocate a custom color. If the color you try to allocate has already been allocated once, it will return the old color If the value passed is not a 3-tuple, this function will return the input parameter. """ try: return self.allocated[t] except KeyError: pass try: if len(t) == 3: if self.colorcounter > curses.COLORS: return 1 if not curses.can_change_color(): return 1 curses.init_color(self.colorcounter, t[0], t[1], t[2]) c = self.colorcounter self.allocated[t] = c self.colorcounter += 1 return c else: return t except (KeyError, IndexError, TypeError) as e: return t
def curses_status( ca, args ): ca.stdscr.addstr( "Baudrate : %s\n" % curses.baudrate() ) ca.stdscr.addstr( "Has color : %s\n" % curses.has_colors() ) ca.stdscr.addstr( "Change color : %s\n" % curses.can_change_color() ) ca.stdscr.addstr( "Insert char : %s\n" % curses.has_ic() ) ca.stdscr.addstr( "Insert line : %s\n" % curses.has_il() ) ca.stdscr.addstr( "Color numbers : 0-%s\n" % curses.COLORS ) ca.stdscr.addstr( "COLOR_WHITE : %s\n" % (curses.color_content(curses.COLOR_WHITE),) ) ca.stdscr.addstr( "COLOR_BLACK : %s\n" % (curses.color_content(curses.COLOR_BLACK),) ) ca.stdscr.addstr( "COLOR_RED : %s\n" % (curses.color_content(curses.COLOR_RED),) ) ca.stdscr.addstr( "COLOR_GREEN : %s\n" % (curses.color_content(curses.COLOR_GREEN),) ) ca.stdscr.addstr( "COLOR_BLUE : %s\n" % (curses.color_content(curses.COLOR_BLUE),) ) ca.stdscr.addstr( "COLOR_YELLOW : %s\n" % (curses.color_content(curses.COLOR_YELLOW),) ) ca.stdscr.addstr( "COLOR_MAGENTA : %s\n" % (curses.color_content(curses.COLOR_MAGENTA),)) ca.stdscr.addstr( "COLOR_CYAN : %s\n" % (curses.color_content(curses.COLOR_CYAN),) ) ca.stdscr.addstr( "Erase char : %s\n" % (curses.erasechar(),) ) ls = list( filter( lambda x: curses.has_key(x), range(255) )) ca.stdscr.addstr( "Unknown keys : %s\n" % ls )
def set_colors(): if curses.can_change_color(): # Light curses.init_color(COLOR_LIGHT, 240, 217, 181) # Dark curses.init_color(COLOR_DARK, 181, 136, 99) # White on light curses.init_pair(WHITE_ON_LIGHT, curses.COLOR_WHITE, COLOR_LIGHT) # White on dark curses.init_pair(WHITE_ON_DARK, curses.COLOR_WHITE, COLOR_DARK) # Black on light curses.init_pair(BLACK_ON_LIGHT, curses.COLOR_BLACK, COLOR_LIGHT) # Black on dark curses.init_pair(BLACK_ON_DARK, curses.COLOR_BLACK, COLOR_DARK) else: # White on light curses.init_pair(WHITE_ON_LIGHT, curses.COLOR_WHITE, curses.COLOR_YELLOW) # White on dark curses.init_pair(WHITE_ON_DARK, curses.COLOR_WHITE, curses.COLOR_GREEN) # Black on light curses.init_pair(BLACK_ON_LIGHT, curses.COLOR_BLACK, curses.COLOR_YELLOW) # Black on dark curses.init_pair(BLACK_ON_DARK, curses.COLOR_BLACK, curses.COLOR_GREEN)
def main(stdscr): stdscr.keypad(True) curses.noecho() curses.cbreak() cube = Cube(defaultState) nextkey = "" curses.curs_set(False) if curses.can_change_color(): curses.init_color(curses.COLOR_RED, 769, 118, 227) curses.init_color(curses.COLOR_WHITE, 1000, 1000, 1000) curses.init_color(curses.COLOR_BLUE, 0, 318, 729) curses.init_color(curses.COLOR_GREEN, 0, 620, 376) curses.init_color(curses.COLOR_YELLOW, 1000, 835, 0) curses.init_color(curses.COLOR_MAGENTA, 1000, 345, 0) # Orange curses.init_pair(100, curses.COLOR_BLACK, curses.COLOR_RED) curses.init_pair(101, curses.COLOR_BLACK, curses.COLOR_WHITE) curses.init_pair(102, curses.COLOR_BLACK, curses.COLOR_YELLOW) curses.init_pair(103, curses.COLOR_BLACK, curses.COLOR_GREEN) curses.init_pair(104, curses.COLOR_BLACK, curses.COLOR_BLUE) curses.init_pair( 105, curses.COLOR_BLACK, curses.COLOR_MAGENTA) # Is magenta if curses.can_change_colors()=false while True: stdscr.clear() if parse_key(nextkey, cube, rotatekeys): return 0 draw(stdscr, cube.state) nextkey = stdscr.getkey()
def initialize_curses(): """ Initializes curses and returns a _curses.window object for the entire screen. :return: (_curses.window) stdscr """ stdscr = curses.initscr() curses.start_color() curses.use_default_colors() curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE) if curses.can_change_color() == 1: curses.init_color(240, 500, 500, 500) curses.init_pair(2, 240, -1) curses.init_color(255, 0, 0, 0) curses.init_pair(3, 255, 240) curses.init_pair(4, curses.COLOR_GREEN, -1) curses.init_pair(5, curses.COLOR_RED, -1) curses.noecho() curses.cbreak() stdscr.keypad(True) curses.curs_set(0) return stdscr
def main(window, speed): if curses.can_change_color(): curses.init_color(curses.COLOR_BLACK, 0, 0, 0) curses.init_color(curses.COLOR_WHITE, 1000, 1000, 1000) curses.init_color(curses.COLOR_YELLOW, 1000, 1000, 0) try: curses.curs_set(0) except Exception: pass # Can't hide cursor in 2019 huh? cells = {} for i in range( 10000 ): # initial population # @todo: make it a portion of height * width cell = random_cell(window) cells[cell[0]] = cell[1] while True: height, width = max_dimensions(window) if len(cells.keys()) >= 0.95 * (height * width): cells.clear() cells = update_cells(cells, window) window.clear() redisplay(cells, window) window.refresh() try: time.sleep((0.2) / (speed / 100)) except ZeroDivisionError: time.sleep(0.2)
def initColors (self): curses.start_color() if self.conf.params_transparency: curses.use_default_colors() bgcolor = -1 else: bgcolor = 0 # Setup colors # TODO, check the term capability before if curses.can_change_color(): for i in range(len(self.conf.color_set)): if not self.conf.color_set[i]: continue else: rgb = self.conf.color_set[i] curses.init_color(i, rgb[0], rgb[1], rgb[2]) curses.init_pair(0, curses.COLOR_BLACK, bgcolor) # 1 black curses.init_pair(1, curses.COLOR_RED, bgcolor) # 2 red curses.init_pair(2, curses.COLOR_GREEN, bgcolor) # 3 green curses.init_pair(3, curses.COLOR_YELLOW, bgcolor) # 4 yellow curses.init_pair(4, curses.COLOR_BLUE, bgcolor) # 5 blue curses.init_pair(5, curses.COLOR_MAGENTA, bgcolor) # 6 magenta curses.init_pair(6, curses.COLOR_CYAN, bgcolor) # 7 cyan curses.init_pair(7, curses.COLOR_WHITE, bgcolor) # 8 white
def mybox(stdscr): stdscr.addstr(0,0,'KUNG FURY') stdscr.addstr(1,1,'KUNG FURY') stdscr.addstr( 1, 2, 'LOLO') stdscr.addstr(3, 4, 'AARFRFRF') stdscr.addstr(5, 6, 'huhu') stdscr.addstr(7, 7, "Current mode: Typing mode", curses.A_REVERSE) stdscr.addch(curses.ACS_ULCORNER) stdscr.addch(curses.ACS_URCORNER) stdscr.addch(curses.ACS_LLCORNER) stdscr.addch(curses.ACS_LRCORNER) stdscr.border() #stdscr.bkgd('.', curses.A_REVERSE) #stdscr.bkgdset('.', curses.A_UNDERLINE) #stdscr.addstr(10, 1, "Pretty text", curses.color_pair(1)) if curses.has_colors(): stdscr.addstr(5, 1, 'HAS COLOR') else: stdscr.addstr(5, 1, 'NO COLOR') if curses.can_change_color(): stdscr.addstr(6, 1, 'can_change_color = true') else: stdscr.addstr(6, 1, 'can_change_color = false') stdscr.addstr(10, 1, "Pretty text lots of space", curses.color_pair(1)) curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE) stdscr.addstr(11, 1, "RED ALERT!", curses.color_pair(1)) stdscr.addstr(12, 1, "RED ALERT!", curses.color_pair(0)) stdscr.refresh() stdscr.getkey()
def __init__(self, y0, x0, y1, x1, filename, alignment): """ Args: y0 (int): top boundary x0 (int): left boundary y1 (int): bottom boundary x1 (int): right boundary filename (str): name of alignment file displayed alignment (Bio.Align.MultipleSeqAlignment): MSA Returns: None """ self.filename = filename self.num_seq = len(alignment) self.align_width = len(alignment[0]) if curses.has_colors(): attr2 = curses.color_pair(2) if not curses.can_change_color(): attr2 |= curses.A_BOLD else: attr2 = curses.A_NORMAL self.pad_width = x1 - x0 + 1 self.pad = curses.newpad(1, self.pad_width + 1) self.pad.addstr(0, 0, " " * (self.pad_width), attr2) status = "Loading {}...".format(filename) self.pad.addstr(0, 0, status[0:self.pad_width], attr2) self.pad.noutrefresh(0, 0, y0, x0, y1, x1)
def colordebugger(self, screen): checkvar1 = curses.has_colors() checkvar2 = curses.can_change_color() COLOR_NOW = curses.COLOR_PAIRS startwhitha0 = 0 a = True while a: screen.clear() self.screen_window(screen, 0, 0, self.height - 2, self.width - 1, curses.color_pair(5) + curses.A_REVERSE) screen.addstr(2, 2, str(checkvar1)) screen.addstr(3, 2, str(checkvar2)) screen.addstr(4, 2, str(curses.COLORS)) screen.addstr(5, 2, str(curses.COLOR_PAIRS)) starting = startwhitha0 for y in range(0, 36): for x in range(0, 32): screen.addstr(y + 10, 5 * x + 15, str(startwhitha0), curses.color_pair(startwhitha0)) startwhitha0 = 1 + startwhitha0 % COLOR_NOW startwhitha0 = starting try: action = screen.get_wch() if action == "\n": a = False elif action == curses.KEY_UP: startwhitha0 = (startwhitha0 - 32) % COLOR_NOW elif action == curses.KEY_DOWN: startwhitha0 = (startwhitha0 + 32) % COLOR_NOW elif action == curses.KEY_RIGHT: startwhitha0 = (startwhitha0 + 256) % COLOR_NOW elif action == curses.KEY_LEFT: startwhitha0 = (startwhitha0 - 256) % COLOR_NOW except KeyboardInterrupt: sys.exit()
def init_palette(): # just execute this once global init_done if init_done: return init_done = True # skip for terminals without colours if not curses.has_colors() or not curses.can_change_color(): return # get the colour depth that we can archieve based # on the number of supported colours global colour_depth colour_depth = get_colour_depth() # init colours for r in range(colour_depth): for g in range(colour_depth): for b in range(colour_depth): colour_number = get_palette_colour_number(r, g, b) cursed_r = int(r / (colour_depth - 1) * 1000) cursed_g = int(g / (colour_depth - 1) * 1000) cursed_b = int(b / (colour_depth - 1) * 1000) curses.init_color(colour_number, cursed_r, cursed_g, cursed_b)
def display_curses_color_info(self): import curses self.io.msg(curses.COLORS, curses.COLOR_PAIRS, curses.can_change_color()) self.io.msg(curses.A_ALTCHARSET, curses.A_BLINK, curses.A_BOLD, curses.A_DIM, curses.A_NORMAL, curses.A_REVERSE, curses.A_STANDOUT, curses.A_UNDERLINE)
def initColors (self): '''Setup all colors stuff, rgb as well.''' curses.start_color() if self.conf.params['transparency']: curses.use_default_colors() bgcolor = -1 else: bgcolor = False # Setup colors rgb if curses.can_change_color(): for i in range(len(self.conf.color_set)): if not self.conf.color_set[i]: continue else: rgb = self.conf.color_set[i] curses.init_color(i, rgb[0], rgb[1], rgb[2]) curses.init_pair(0, curses.COLOR_BLACK, bgcolor) # 0 black curses.init_pair(1, curses.COLOR_RED, bgcolor) # 1 red curses.init_pair(2, curses.COLOR_GREEN, bgcolor) # 2 green curses.init_pair(3, curses.COLOR_YELLOW, bgcolor) # 3 yellow curses.init_pair(4, curses.COLOR_BLUE, bgcolor) # 4 blue curses.init_pair(5, curses.COLOR_MAGENTA, bgcolor) # 5 magenta curses.init_pair(6, curses.COLOR_CYAN, bgcolor) # 6 cyan curses.init_pair(7, curses.COLOR_WHITE, bgcolor) # 7 white
def test_colors(screen): curses.curs_set(0) screen.bkgd(' ', curses.A_STANDOUT) win = curses.newwin(20, 32, 2, 2) win.border() win.bkgd(' ') win.addstr(1, 1, "The quick brown fox jumped ...", curses.color_pair(1)) win.addstr(2, 1, "The quick brown fox jumped ...", curses.color_pair(1) | curses.A_BOLD) win.addstr(3, 1, "The quick brown fox jumped ...", curses.color_pair(2)) win.addstr(4, 1, "The quick brown fox jumped ...", curses.color_pair(2) | curses.A_BOLD) win.addstr(5, 1, "The quick brown fox jumped ...", curses.color_pair(3)) win.addstr(6, 1, "The quick brown fox jumped ...", curses.color_pair(3) | curses.A_BOLD) win.addstr(7, 1, "The quick brown fox jumped ...", curses.color_pair(4)) win.addstr(8, 1, "The quick brown fox jumped ...", curses.color_pair(4) | curses.A_BOLD) win.addstr(9, 1, "The quick brown fox jumped ...", curses.color_pair(5)) win.addstr(10, 1, "The quick brown fox jumped ...", curses.color_pair(5) | curses.A_BOLD) win.addstr(11, 1, "The quick brown fox jumped ...", curses.color_pair(6)) win.addstr(12, 1, "The quick brown fox jumped ...", curses.color_pair(6) | curses.A_BOLD) win.addstr(13, 1, "The quick brown fox jumped ...", curses.color_pair(7)) win.addstr(14, 1, "The quick brown fox jumped ...", curses.color_pair(7) | curses.A_BOLD) win.addstr(15, 1, "The quick brown fox jumped ...", curses.color_pair(8)) win.addstr(16, 1, "The quick brown fox jumped ...", curses.color_pair(8) | curses.A_BOLD) if curses.can_change_color(): win.addstr(17, 1, "Can change color") else: win.addstr(17, 1, "Cannot change color") win.addstr(18, 1, "Press a key") win.refresh() win.getch()
def __enter__(self): """Open ANSI interface.""" VideoPlugin.__enter__(self) self.screen = curses.initscr() curses.noecho() curses.cbreak() curses.nonl() curses.raw() self.orig_size = self.screen.getmaxyx() self.underlay = curses.newwin( self.height + self.border_y*2, self.width + self.border_x*2, 0, 0 ) self.window = curses.newwin(self.height, self.width, self.border_y, self.border_x) self.window.nodelay(True) self.window.keypad(True) self.window.scrollok(False) curses.start_color() # curses mistakenly believes changing palettes works on macOS's Terminal.app self.can_change_palette = (not MACOS) and ( curses.can_change_color() and curses.COLORS >= 16 and curses.COLOR_PAIRS > 128 ) sys.stdout.write(SET_TITLE % self.caption.encode('utf-8', 'replace')) sys.stdout.flush() self._set_default_colours(16) bgcolor = self._curses_colour(7, 0, False) # text and colour buffer self.text = [[[(u' ', bgcolor)]*self.width for _ in range(self.height)]] self.set_border_attr(0) self.screen.clear()
def init_curses(): global screen screen = curses.initscr() screen.clear() screen.refresh() curses.curs_set(0) curses.noecho() curses.cbreak() screen.keypad(1) curses.start_color() if curses.has_colors(): if curses.can_change_color(): set_color(curses.COLOR_GREEN, 1000, 517, 165) set_color(curses.COLOR_BLUE, 909, 533, 35) set_color(curses.COLOR_CYAN, 611, 529, 419) set_color(curses.COLOR_WHITE, 1000, 905, 541) set_color(curses.COLOR_RED, 1000, 1000, 1000) curses.init_pair(1, curses.COLOR_GREEN, 0) curses.init_pair(2, curses.COLOR_BLUE, 0) curses.init_pair(3, curses.COLOR_CYAN, 0) curses.init_pair(4, curses.COLOR_WHITE, 0) curses.init_pair(5, curses.COLOR_RED, 0)
def main(window, speed): if curses.can_change_color(): curses.init_color(curses.COLOR_BLACK, 0, 0, 0) curses.init_color(curses.COLOR_WHITE, 1000, 1000, 1000) curses.init_color(curses.COLOR_YELLOW, 1000, 1000, 0) curses.init_pair(1, curses.COLOR_YELLOW, 0) try: curses.curs_set(0) except Exception: pass # Can't hide cursor in 2019 huh? window.border() twice_flakes = {} #twiceflakes is an empty dict right? while True: height, width = max_dimensions(window) if len(twice_flakes.keys()) >= 0.95 * ( height * width ): #What does this part do? Wait isn't this part clearing everything print(twice_flakes.key()) twice_flakes.clear( ) #Nope it still doesn't work. It still clears the screen twice_flakes = update_twiceflakes( twice_flakes, window) #Maybe it's something to do with clearing the screen twiceflake = twiceflake_char(window) print(twiceflake) twice_flakes[(twiceflake[0], twiceflake[1])] = twiceflake[ 2] #What does this part of the code do? Why is twiceflake member str name assigned? window.clear() draw_moon(window) redisplay(twice_flakes, window) window.refresh() try: time.sleep((0.2) / (speed / 100)) except ZeroDivisionError: time.sleep(0.2)
def main(): scr = None try: scr = curses.initscr() mgr = Manager(scr) if curses.has_colors() and curses.can_change_color(): curses.start_color() pair_number = curses.pair_number( curses.color_pair(curses.COLOR_BLUE)) curses.init_pair(pair_number, curses.COLOR_WHITE, curses.COLOR_BLUE) curses.color_content(curses.COLOR_RED) scr.bkgdset(curses.COLOR_BLUE) curses.def_prog_mode() curses.noecho() curses.cbreak() curses.curs_set(0) scr.keypad(1) mgr.run() clean(scr=scr) except KeyboardInterrupt: clean(scr=scr) except: clean(scr=scr)
def __init__(self, *args, **kwargs): super(CustomFormatter, self).__init__(*args, **kwargs) # Do we have color? If the "color" parameter is present and set to # False, it will override all other settings. if kwargs.get('color') == False: self._have_color = False if curses is not None: self._have_color = curses.can_change_color() elif colorama is not None: self._have_color = True else: self._have_color = False # Debug level --> ANSI escape code mapping self._colors = { logging.DEBUG: '\x1b[34m', # Blue logging.INFO: '\x1b[32m', # Green logging.WARNING: '\x1b[33m', # Yellow logging.ERROR: '\x1b[31m', # Red } self._nocolor = '\x1b[39m' # Set the converter to log everything in GMT time. self.converter = time.gmtime
def __init__(self, **options): Formatter.__init__(self, **options) #XXX distinct colors must fit, actually if curses.can_change_color() and len(self.style) <= curses.COLORS: # cache of registered RGB colors colors = [] def init_color(rgb): r, g, b = int(rgb[:2], 16), int(rgb[2:4], 16), int(rgb[4:], 16) curses.init_color(len(colors) + 1, r * 1000 / 255, g * 1000 / 255, b * 1000 / 255) colors.append(rgb) pairs = [] self.pairs = {} self.bolds = set() for token, style in self.style: #XXX bg fg = style['color'] or 'ffffff' bg = style['bgcolor'] if fg not in colors: init_color(fg) if style['bold']: self.bolds.add(token) pair = (fg, bg) sys.stderr.write("%r gets %r\n" % (token, pair)) if pair not in pairs: curses.init_pair(len(pairs) + 1, colors.index(fg)+1, -1) pairs.append(pair) self.pairs[token] = pairs.index(pair)
def set_color(self, name, r, g, b): if not curses.can_change_color(): raise colors.ColorException('Can not set colors.') color_name_exists = name in self._core.get_colors() if not color_name_exists and not len( self._color_index_map.values()) < 32: raise colors.ColorException( 'Maximum number of colors (32) is reached.') cr = int(r * 1000.0 // 255.0) cg = int(g * 1000.0 // 255.0) cb = int(b * 1000.0 // 255.0) color_index = self._color_index_map.get( name, len(self._color_index_map.values())) curses.init_color(color_index, cr, cg, cb) self._color_index_map[name] = color_index if color_name_exists: return # if it is a new name, we add new pair definitions for bg_entry in self._core.get_backgrounds(): self._init_pair( color_index, BG_INDEX_MAP[bg_entry], self._core.get_background_color(bg_entry), self._core.get_background_color(bg_entry, compat=True))
def main(win): curses.start_color() curses.use_default_colors() curses.init_color(curses.COLOR_GREEN, 797, 0, 0) curses.init_pair(1,curses.COLOR_GREEN,curses.COLOR_BLACK) win.addstr(str(curses.can_change_color()), curses.color_pair(1)) win.refresh()
def init_color(self,t): """ Attempts to allocate a custom color. If the color you try to allocate has already been allocated once, it will return the old color If the value passed is not a 3-tuple, this function will return the input parameter. """ try: return self.allocated[t] except KeyError: pass try: if len(t) == 3: if self.colorcounter > curses.COLORS: return 1 if not curses.can_change_color(): return 1 curses.init_color(self.colorcounter,t[0],t[1],t[2]) c = self.colorcounter self.allocated[t] = c self.colorcounter += 1 return c else: return t except (KeyError, IndexError, TypeError) as e: return t
def update(self, y0, x0, y1, x1, offset_y, disp_height): """ Update how the status bar is drawn. Args: y0 (int): top boundary x0 (int): left boundary y1 (int): bottom boundary x1 (int): right boundary offset_y (int): index of the top sequence displayed in the sequence area. disp_height (int): number of lines displayed in the sequence area. Returns: None """ if curses.has_colors(): attr2 = curses.color_pair(2) if not curses.can_change_color(): attr2 |= curses.A_BOLD else: attr2 = curses.A_NORMAL if self.pad_width < x1 - x0 + 1: self.pad_width = x1 - x0 + 1 self.pad = curses.newpad(1, self.pad_width + 1) self.pad.addstr(0, 0, " " * (x1 - x0 + 1), attr2) if self.num_seq > disp_height: viewmax = offset_y + disp_height else: viewmax = self.num_seq status = "Viewing sequences: {}-{}/{}, Alignment length: {} [{}]" status = status.format(offset_y + 1, viewmax, self.num_seq, self.align_width, self.filename) self.pad.addstr(0, 0, status[0:self.pad_width], attr2) self.pad.noutrefresh(0, 0, y0, x0, y1, x1)
def main(window, speed): if curses.can_change_color(): curses.init_color(curses.COLOR_BLACK, 0, 0, 0) curses.init_color(curses.COLOR_WHITE, 1000, 1000, 1000) curses.init_color(curses.COLOR_YELLOW, 1000, 1000, 0) curses.init_pair(1, curses.COLOR_YELLOW, 0) try: curses.curs_set(0) except Exception: pass # Can't hide cursor in 2019 huh? snowflakes = {} while True: height, width = max_dimensions(window) if len(snowflakes.keys()) >= 0.95 * (height * width): snowflakes.clear() snowflakes = update_snowflakes(snowflakes, window) snowflake = snowflake_char(window) snowflakes[(snowflake[0], snowflake[1])] = snowflake[2] window.clear() draw_moon(window) redisplay(snowflakes, window) window.refresh() try: time.sleep((0.2) / (speed / 100)) except ZeroDivisionError: time.sleep(0.2)
def init_colors(self): if curses.has_colors() and curses.can_change_color(): curses.init_color(self.COLOR_BLACK, 0, 0, 0) curses.init_color(self.COLOR_WHITE, 1000, 1000, 1000) curses.init_color(self.COLOR_BLUE, 0, 0, 1000) curses.init_color(self.COLOR_RED, 1000, 0, 0) curses.init_color(self.COLOR_GREEN, 0, 1000, 0) for i in range(0, self.GRAYS): curses.init_color(self.GRAY_BASE + i, i * 1000 // (self.GRAYS - 1), i * 1000 // (self.GRAYS - 1), i * 1000 // (self.GRAYS - 1)) curses.init_pair(self.GRAY_BASE + i, self.GRAY_BASE + i, self.COLOR_BLACK) else: self.COLOR_BLACK = curses.COLOR_BLACK self.COLOR_WHITE = curses.COLOR_WHITE self.COLOR_BLUE = curses.COLOR_BLUE self.COLOR_RED = curses.COLOR_RED self.COLOR_GREEN = curses.COLOR_GREEN for i in range(0, self.GRAYS): curses.init_pair(self.GRAY_BASE + i, self.COLOR_WHITE, self.COLOR_BLACK) curses.init_pair(self.BLACK, self.COLOR_BLACK, self.COLOR_BLACK) curses.init_pair(self.WHITE, self.COLOR_WHITE, self.COLOR_BLACK) curses.init_pair(self.BLUE, self.COLOR_BLUE, self.COLOR_BLACK) curses.init_pair(self.RED, self.COLOR_RED, self.COLOR_BLACK) curses.init_pair(self.GREEN, self.COLOR_GREEN, self.COLOR_BLACK)
def __enter__(self): """Open ANSI interface.""" VideoPlugin.__enter__(self) try: self.screen = curses.initscr() curses.noecho() curses.cbreak() curses.nonl() curses.raw() self.orig_size = self.screen.getmaxyx() self.window = curses.newwin(self.height + self.border_y * 2, self.width + self.border_x * 2, 0, 0) self.window.nodelay(True) self.window.keypad(True) self.window.scrollok(False) curses.start_color() # curses mistakenly believes changing palettes works on macOS's Terminal.app self.can_change_palette = (not MACOS) and ( curses.can_change_color() and curses.COLORS >= 16 and curses.COLOR_PAIRS > 128) self._set_default_colours(16) bgcolor = self._curses_colour(7, 0, False) self.set_border_attr(0) self._resize(self.height, self.width) self.screen.clear() except Exception as e: # if setup fails, don't leave the terminal raw self._close() raise
def init_colours_nt_256_light_reverse(): """ Initialise nucleotide colour scheme for 256 colour terminals with colour-changing capabilities. White text on a coloured background. """ if not curses.can_change_color(): return # nucleotide colours: brightred = 51 yellow = 52 blue = 53 orange = 54 midblue = 55 cyan = 56 lightgrey = 57 green = 58 darkgrey = 59 pink = 60 paleblue = 61 flesh = 62 # ui colours: orange1 = 63 bg = 64 init_rgb255(curses.COLOR_BLACK, 0, 0, 0) init_rgb255(curses.COLOR_WHITE, 255, 255, 255) init_rgb255(bg, 255, 255, 255) init_rgb255(brightred, 200, 0, 0) init_rgb255(yellow, 210, 210, 0) init_rgb255(blue, 10, 80, 245) init_rgb255(orange, 180, 110, 0) init_rgb255(midblue, 10, 10, 160) init_rgb255(cyan, 0, 210, 210) init_rgb255(lightgrey, 195, 195, 195) init_rgb255(green, 0, 140, 0) init_rgb255(orange1, 208, 49, 1) curses.init_pair(1, curses.COLOR_BLACK, bg) # colour pair 2 for status bar curses.init_pair(2, curses.COLOR_WHITE, orange1) # colour pair 3 for gap % track curses.init_pair(3, orange1, bg) # colour pair 4 for sequence id labels curses.init_pair(4, curses.COLOR_WHITE, darkgrey) # colour pair 11 for gaps and misc characters curses.init_pair(11, lightgrey, bg) for key in aa_dict.keys(): curses.init_pair(aa_dict[key], lightgrey, bg) for key in ['a', 'A']: curses.init_pair(aa_dict[key], bg, green) for key in ['c', 'C']: curses.init_pair(aa_dict[key], bg, orange) for key in ['t', 'T', 'u', 'U']: curses.init_pair(aa_dict[key], bg, midblue) for key in ['g', 'G']: curses.init_pair(aa_dict[key], bg, brightred)
async def start(screen): curses.noecho() curses.cbreak() screen.keypad(True) curses.start_color() curses.use_default_colors() curses.curs_set(0) if curses.can_change_color(): curses.init_color(COLOR_DARKBLACK, 0, 0, 0) curses.init_color(COLOR_SUPERWHITE, 1000, 1000, 1000) curses.init_pair(PAIR_ACTIVE_TAB, COLOR_SUPERWHITE, COLOR_DARKBLACK) curses.init_pair(PAIR_TABBAR_BG, COLOR_DARKBLACK, COLOR_SUPERWHITE) else: curses.init_pair(PAIR_ACTIVE_TAB, curses.COLOR_WHITE, curses.COLOR_BLACK) curses.init_pair(PAIR_TABBAR_BG, curses.COLOR_BLACK, curses.COLOR_WHITE) curses.init_pair(PAIR_INACTIVE_TAB, curses.COLOR_WHITE, curses.COLOR_BLACK) curses.init_pair(PAIR_ACTIVE_ACCOUNT_SEL, curses.COLOR_BLACK, curses.COLOR_WHITE) curses.init_pair(PAIR_INACTIVE_ACCOUNT_SEL, curses.COLOR_WHITE, -1) curses.init_pair(PAIR_POSITIVE_VALUE, curses.COLOR_GREEN, -1) curses.init_pair(PAIR_NEGATIVE_VALUE, curses.COLOR_RED, -1) websockets_path = "ws://localhost:8888" async with api.WebSocket(websockets_path) as ws: app = Application(screen, ws) await app.start()
def init(): height_term, width_term = get_terminal_size() height_min = COL_HEIGHT * HEIGHT + 2 + 9 width_min = COL_WIDTH * WIDTH + 2 + 5 if height_term < height_min or width_term < width_min: # resize the terminal to fit the minimum size to display the connect4 before exit stdout.write("\x1b[8;{h};{w}t".format(h=max(height_min, height_term), w=max(width_min, width_term))) exit('\033[91m' + 'The terminal was too small, you can now restart ' + '\033[1m' + 'Connect4' + '\033[0m') stdscr = curses.initscr() height,width = stdscr.getmaxyx() if height < height_min or width < width_min: # abort the program if the terminal can't be resized curses.endwin() exit('Please resize your terminal [%d%s%d] (minimum required %d%s%d)' %(width, 'x', height, width_min, 'x', height_min)) curses.noecho() curses.cbreak() curses.curs_set(0) stdscr.keypad(1) #define the different colors if curses.can_change_color(): defineColors() #return stdscr, width stdscr.clear() stdscr.border(0) return stdscr, width, height
def curses_init(): global scr scr = curses.initscr() curses.noecho() curses.cbreak() curses.start_color() curses.use_default_colors() if curses.can_change_color(): curses.init_color(curses.COLOR_BLACK, 0, 0, 0) curses.init_color(curses.COLOR_WHITE, 255, 255, 255) curses.init_color(curses.COLOR_GREEN, 0, 255, 0) curses.init_color(curses.COLOR_YELLOW, 255, 255, 0) curses.init_color(curses.COLOR_RED, 255, 0, 0) curses.init_color(curses.COLOR_MAGENTA, 255, 0, 255) curses.init_pair(1, curses.COLOR_WHITE, -1) curses.init_pair(2, curses.COLOR_GREEN, -1) curses.init_pair(3, curses.COLOR_YELLOW, -1) curses.init_pair(4, curses.COLOR_RED, -1) curses.init_pair(5, curses.COLOR_MAGENTA, -1) curses.init_pair(6, curses.COLOR_BLACK, curses.COLOR_WHITE) curses.init_pair(7, curses.COLOR_BLACK, curses.COLOR_GREEN) curses.init_pair(8, curses.COLOR_BLACK, curses.COLOR_YELLOW) curses.init_pair(9, curses.COLOR_BLACK, curses.COLOR_RED) curses.init_pair(10, curses.COLOR_YELLOW, -1) cleanup.install(curses_deinit)
def main(win): curses.start_color() curses.use_default_colors() curses.init_color(curses.COLOR_GREEN, 797, 0, 0) curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK) win.addstr(str(curses.can_change_color()), curses.color_pair(1)) win.refresh()
def init_rgb_colors(self): if curses.can_change_color(): for i in range(len(self.conf.color_set)): if not self.conf.color_set[i]: continue else: rgb = self.conf.color_set[i] curses.init_color(i, rgb[0], rgb[1], rgb[2])
def selectPalette(self): if curses.can_change_color(): # TODO: Put the palette back the way it was colorLevel = 650 # Your powershell window is going to look pink after you exit... d8) curses.init_color(curses.COLOR_BLACK, 0, 0, 0) curses.init_color(curses.COLOR_BLUE, 0, 0, colorLevel) curses.init_color(curses.COLOR_GREEN, 0, colorLevel, 0) curses.init_color(curses.COLOR_RED, colorLevel, 0, 0) curses.init_color(curses.COLOR_YELLOW, colorLevel, colorLevel, 0) curses.init_color(curses.COLOR_MAGENTA, colorLevel, 0, colorLevel) curses.init_color(curses.COLOR_CYAN, 0, colorLevel, colorLevel) curses.init_color(curses.COLOR_WHITE, colorLevel, colorLevel, colorLevel) # The color ordering is different between a CMD / Powershell window and everything else... if not PLATFORM_IS_WINDOWS: colorMap = (curses.COLOR_BLACK, curses.COLOR_CYAN, curses.COLOR_GREEN, curses.COLOR_RED, curses.COLOR_YELLOW, curses.COLOR_MAGENTA, curses.COLOR_BLUE, curses.COLOR_WHITE) else: colorMap = (curses.COLOR_BLACK, curses.COLOR_YELLOW, curses.COLOR_GREEN, curses.COLOR_CYAN, curses.COLOR_RED, curses.COLOR_MAGENTA, curses.COLOR_BLUE, curses.COLOR_WHITE) self.attributeMap = array.array('L', [0] * 256) if curses.has_colors(): self.screenMode = Display.smCO80 i = 0 for fore in reversed(colorMap): for back in colorMap: if i: curses.init_pair(i, fore, back) i += 1 for i in range(256): back = (i >> 4) & 0x07 bold = i & 0x08 fore = i & 0x07 self.attributeMap[i] = curses.color_pair((7 - colorMap[fore]) * 8 + colorMap[back]) if bold: self.attributeMap[i] |= curses.A_BOLD else: self.screenMode = Display.smMono self.attributeMap[0x07] = curses.A_NORMAL self.attributeMap[0x0f] = curses.A_BOLD self.attributeMap[0x70] = curses.A_REVERSE
def main(stdscr): # Clear screen stdscr.clear() # Debug Bar stdscr.addstr( 0, 0, "COLOR SUPPORT: " + str(curses.has_colors()) + " FULL COLOR SUPPORT: " + str(curses.can_change_color()) + " SUPPORTED COLORS: " + str(curses.COLORS) + " SUPPORTED COLOR PAIRS: " + str(curses.COLOR_PAIRS), curses.color_pair(5), ) # Define color defaults based on output level (WARN,INFO,ERROR,DEBUG) curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_BLACK) # WARN curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK) # INFO curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK) # ERROR curses.init_pair(4, curses.COLOR_CYAN, curses.COLOR_BLACK) # DEBUG curses.init_pair(5, curses.COLOR_CYAN, curses.COLOR_WHITE) # DEBUG-BAR # Load rulesetManifest rulesetManifest = io.open("rulesets/manifest.json", "r") rulesetManifest = json.load(rulesetManifest) # User prompt on what to do stdscr.addstr(2, 2, "Select a game:") # List possible games for slot in rulesetManifest["availableGames"]: stdscr.addstr( int(slot.replace("slot", "")) + 3, 3, slot.replace("slot", "") + ". " + rulesetManifest["availableGames"][slot]["callName"], ) # Ask player for ruleset to use and retrieve it from json rules = getRules(stdscr, rulesetManifest) # Clear screen and add debug info for game stdscr.clear() stdscr.addstr( 1, 2, rules["gameInfo"]["title"] + " Version " + str(rules["gameInfo"]["version"]) + " Branch " + rules["gameInfo"]["branch"], curses.color_pair(4), ) stdscr.addstr(2, 2, "Rules by " + rules["gameInfo"]["author"], curses.color_pair(4)) # EXAMPLE: engine.renderEngine.writetoBuffer("Hello World!",3,2,"WARN") stdscr.refresh() stdscr.getkey()
def __init__(self, **kwargs): """ Initialise the text interface. """ video.VideoPlugin.__init__(self) self.curses_init = False if not curses: raise video.InitFailed() # set the ESC-key delay to 25 ms unless otherwise set # set_escdelay seems to be unavailable on python curses. if not os.environ.has_key('ESCDELAY'): os.environ['ESCDELAY'] = '25' self.curses_init = True self.screen = curses.initscr() self.orig_size = self.screen.getmaxyx() curses.noecho() curses.cbreak() curses.nonl() curses.raw() curses.start_color() self.screen.clear() self.height, self.width = 25, 80 # border width percentage border_width = kwargs.get('border_width', 0) self.border_y = int(round((self.height * border_width)/200.)) self.border_x = int(round((self.width * border_width)/200.)) self.underlay = curses.newwin( self.height + self.border_y*2, self.width + self.border_x*2, 0, 0) self.window = curses.newwin(self.height, self.width, self.border_y, self.border_x) self.window.nodelay(True) self.window.keypad(True) self.window.scrollok(False) self.can_change_palette = (curses.can_change_color() and curses.COLORS >= 16 and curses.COLOR_PAIRS > 128) self.caption = kwargs.get('caption', '') sys.stdout.write(ansi.esc_set_title % self.caption) sys.stdout.flush() self._set_default_colours(16) # cursor is visible self.cursor_visible = True # 1 is line ('visible'), 2 is block ('highly visible'), 3 is invisible self.cursor_shape = 1 # current cursor position self.cursor_row = 1 self.cursor_col = 1 # last colour used self.last_colour = None # text and colour buffer self.num_pages = 1 self.vpagenum, self.apagenum = 0, 0 bgcolor = self._curses_colour(7, 0, False) self.text = [[[(u' ', bgcolor)]*self.width for _ in range(self.height)]] self.f12_active = False self.set_border_attr(0) # set codepage try: self.codepage = kwargs['codepage'] except KeyError: logging.error('No codepage supplied to text-based interface.') raise video.InitFailed()
def edit(self, state, name, fg, bg, *args): ''' Adds given item to the theme Parameters: state (str): Theme state name (str): Identifier keyed to curses attribute fg (3-tuple<float>): Foreground color as normalized rgb values bg (3-tuple<float>): Background color as normalized rgb values *args: Formatting attributes in {'BLINK', 'BOLD', 'REVERSE', 'UNDERLINE'} (Optional) Preconditions: Curses library shall be intialized. ''' # Return early if given state is invalid. if state not in self._data: return # Process color input. color_attr = 0; if curses.has_colors() and curses.can_change_color(): # Translate give colors to curses color items. colors = self._colors fg = tuple(math.floor(i * 1000) for i in fg) bg = tuple(math.floor(i * 1000) for i in bg) for rgb in fg, bg: if rgb not in colors: color_idx = len(colors) + 16 # 16-color terminal palette curses.init_color(color_idx, *rgb) colors[rgb] = color_idx # Associate foreground and background as a curses color pair object. color_pairs = self._color_pairs pair = (fg, bg) if pair not in color_pairs: color_pair_idx = len(color_pairs) + 8 # 16-color terminal palette curses.init_pair(color_pair_idx, colors[fg], colors[bg]) color_pairs[pair] = color_pair_idx # Get color attribute. color_attr = curses.color_pair(color_pairs[pair]) # Combine formatting attributes into a single curses attribute. format_attr = 0 if 'BLINK' in args: format_attr |= curses.A_BLINK if 'BOLD' in args: format_attr |= curses.A_BOLD if 'REVERSE' in args: format_attr |= curses.A_REVERSE if 'UNDERLINE' in args: format_attr |= curses.A_UNDERLINE # Insert combined color and formatting attributes into this theme. self._data[state][name] = color_attr | format_attr
def setup_colors(self): """Initialize color support and define colors.""" curses.start_color() curses.use_default_colors() fg = -1 # Default foreground color (could also be set to curses.COLOR_WHITE) bg = -1 # Default background color (could also be set to curses.COLOR_BLACK) # This gets colors working in TTY's as well as terminal emulators #curses.init_pair(10, -1, -1) # Default (white on black) # Colors for xterm (not xterm-256color) # Dark Colors curses.init_pair(0, curses.COLOR_BLACK, bg) # 0 Black curses.init_pair(1, curses.COLOR_RED, bg) # 1 Red curses.init_pair(2, curses.COLOR_GREEN, bg) # 2 Green curses.init_pair(3, curses.COLOR_YELLOW, bg) # 3 Yellow curses.init_pair(4, curses.COLOR_BLUE, bg) # 4 Blue curses.init_pair(5, curses.COLOR_MAGENTA, bg) # 5 Magenta curses.init_pair(6, curses.COLOR_CYAN, bg) # 6 Cyan curses.init_pair(7, fg, bg) # White on Black curses.init_pair( 8, fg, curses.COLOR_BLACK) # White on Black (Line number color) # Light Colors (only work on xterm-256color) #curses.init_pair(1, 9, bg) # Red #curses.init_pair(2, 10, bg) # Green #curses.init_pair(3, 11, bg) # Yellow #curses.init_pair(4, 12, bg) # Blue #curses.init_pair(5, 13, bg) # Magenta #curses.init_pair(6, 14, bg) # Cyan # Nicer shades of same colors (if supported) if curses.can_change_color(): try: # TODO: Define RGB forthese to avoid getting # different results in different terminals # xterm-256color chart http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html curses.init_pair(0, 242, bg) # 0 Black curses.init_pair(1, 204, bg) # 1 Red curses.init_pair(2, 119, bg) # 2 Green curses.init_pair(3, 221, bg) # 3 Yellow curses.init_pair(4, 69, bg) # 4 Blue curses.init_pair(5, 171, bg) # 5 Magenta curses.init_pair(6, 81, bg) # 6 Cyan curses.init_pair(7, 15, bg) # 7 White #curses.init_pair(8, 8, bg) # 8 Gray (Line number color) curses.init_pair( 8, 8, curses.COLOR_BLACK) # 8 Gray on Black (Line number color) except: self.app.logger.log( "Enhanced colors failed to load. You could try 'export TERM=xterm-256color'." ) else: self.app.logger.log( "Enhanced colors not supported. You could try 'export TERM=xterm-256color'.", LOG_INFO)
def start_colors(self): if not curses.can_change_color(): return curses.start_color() for color_index in self.colors.keys(): pair_index = self.colors[color_index] if pair_index != 0: curses.init_pair(pair_index, color_index, curses.COLOR_BLACK)
def get_assigner(): curses.start_color() if curses.has_colors(): curses.use_default_colors() if curses.can_change_color(): return DynamicColorAssigner() else: return StaticColorAssigner() return NoColorAssigner()
def setup_colors(self): """Initialize color support and define colors.""" curses.start_color() try: curses.use_default_colors() except: self.app.logger.log("Failed to load curses default colors. You could try 'export TERM=xterm-256color'.") return False fg = -1 # Default foreground color (could also be set to curses.COLOR_WHITE) bg = -1 # Default background color (could also be set to curses.COLOR_BLACK) # This gets colors working in TTY's as well as terminal emulators #curses.init_pair(10, -1, -1) # Default (white on black) # Colors for xterm (not xterm-256color) # Dark Colors curses.init_pair(0, curses.COLOR_BLACK, bg) # 0 Black curses.init_pair(1, curses.COLOR_RED, bg) # 1 Red curses.init_pair(2, curses.COLOR_GREEN, bg) # 2 Green curses.init_pair(3, curses.COLOR_YELLOW, bg) # 3 Yellow curses.init_pair(4, curses.COLOR_BLUE, bg) # 4 Blue curses.init_pair(5, curses.COLOR_MAGENTA, bg) # 5 Magenta curses.init_pair(6, curses.COLOR_CYAN, bg) # 6 Cyan curses.init_pair(7, fg, bg) # White on Black curses.init_pair(8, fg, curses.COLOR_BLACK) # White on Black (Line number color) # Light Colors (only work on xterm-256color) #curses.init_pair(1, 9, bg) # Red #curses.init_pair(2, 10, bg) # Green #curses.init_pair(3, 11, bg) # Yellow #curses.init_pair(4, 12, bg) # Blue #curses.init_pair(5, 13, bg) # Magenta #curses.init_pair(6, 14, bg) # Cyan # Nicer shades of same colors (if supported) if curses.can_change_color(): try: # TODO: Define RGB forthese to avoid getting # different results in different terminals # xterm-256color chart http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html curses.init_pair(0, 242, bg) # 0 Black curses.init_pair(1, 204, bg) # 1 Red curses.init_pair(2, 119, bg) # 2 Green curses.init_pair(3, 221, bg) # 3 Yellow curses.init_pair(4, 69, bg) # 4 Blue curses.init_pair(5, 171, bg) # 5 Magenta curses.init_pair(6, 81, bg) # 6 Cyan curses.init_pair(7, 15, bg) # 7 White #curses.init_pair(8, 8, bg) # 8 Gray (Line number color) curses.init_pair(8, 8, curses.COLOR_BLACK) # 8 Gray on Black (Line number color) except: self.app.logger.log("Enhanced colors failed to load. You could try 'export TERM=xterm-256color'.") else: self.app.logger.log("Enhanced colors not supported. You could try 'export TERM=xterm-256color'.", LOG_INFO)
def colors(stdscr): colors = {getattr(curses, name): name for name in dir(curses) if name.startswith("COLOR_")} if not curses.can_change_color(): curses.addstr("Sorry, your terminal doesn't support color awesomeness") stdscr.refresh() get_q() return stdscr.refresh()
def canchange(self): canchange = (curses.can_change_color() and (self.colors == -1 or self.colors >= 256) and curses.COLORS > 16) if not canchange: return False try: self._makecolor(curses.COLORS-1,(0,0,0)) return True log('CHANGE SUCCESSFUL!', curses.COLORS-1) except: log('CANCHANGE FALSE!') return False
def init_colors(): curses.start_color() global USE_GRADIENT USE_GRADIENT = curses.can_change_color() # use xterm-256 if this is false if USE_GRADIENT: curses.init_color(curses.COLOR_WHITE, 1000, 1000, 1000) for i in range(NUMBER_OF_COLOR + 1): green_value = (1000 - COLOR_STEP * NUMBER_OF_COLOR) + COLOR_STEP * i curses.init_color(START_COLOR_NUM + i, 0, green_value, 0) curses.init_pair(START_COLOR_NUM + i, START_COLOR_NUM + i, curses.COLOR_BLACK) else: curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
def init_colors(self): if curses.has_colors() and curses.can_change_color(): curses.init_color(self.COLOR_BLACK, 0, 0, 0) curses.init_color(self.COLOR_WHITE, 1000, 1000, 1000) curses.init_color(self.COLOR_BLUE, 0, 0, 1000) curses.init_color(self.COLOR_RED, 1000, 0, 0) curses.init_color(self.COLOR_GREEN, 0, 1000, 0) # this will remove flicker, but gives boring colors ''' self.COLOR_BLACK = curses.COLOR_BLACK self.COLOR_WHITE = curses.COLOR_WHITE self.COLOR_BLUE = curses.COLOR_BLUE self.COLOR_RED = curses.COLOR_RED self.COLOR_GREEN = curses.COLOR_GREEN ''' for i in xrange(0, self.GRAYS): curses.init_color( self.GRAY_BASE + i, i * 1000 / (self.GRAYS - 1), i * 1000 / (self.GRAYS - 1), i * 1000 / (self.GRAYS - 1) ) curses.init_pair( self.GRAY_BASE + i, self.GRAY_BASE + i, self.COLOR_BLACK ) else: self.COLOR_BLACK = curses.COLOR_BLACK self.COLOR_WHITE = curses.COLOR_WHITE self.COLOR_BLUE = curses.COLOR_BLUE self.COLOR_RED = curses.COLOR_RED self.COLOR_GREEN = curses.COLOR_GREEN for i in xrange(0, self.GRAYS): curses.init_pair( self.GRAY_BASE + i, self.COLOR_WHITE, self.COLOR_BLACK ) curses.init_pair(self.BLACK, self.COLOR_BLACK, self.COLOR_BLACK) curses.init_pair(self.WHITE, self.COLOR_WHITE, self.COLOR_BLACK) curses.init_pair(self.BLUE, self.COLOR_BLUE, self.COLOR_BLACK) curses.init_pair(self.RED, self.COLOR_RED, self.COLOR_BLACK) curses.init_pair(self.GREEN, self.COLOR_GREEN, self.COLOR_BLACK)
def start_colors(): curses.start_color() curses.init_pair(10, curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(20, curses.COLOR_RED, curses.COLOR_BLACK) if curses.can_change_color(): curses.init_color(COLOR_ORANGE, 600, 400, 250) curses.init_pair(30, COLOR_ORANGE, curses.COLOR_BLACK) curses.init_color(COLOR_GREY, 400, 400, 400) curses.init_pair(40, COLOR_GREY, curses.COLOR_BLACK) else: # Use default curses colors curses.init_pair(30, curses.COLOR_YELLOW, curses.COLOR_BLACK) curses.init_pair(40, curses.COLOR_BLUE, curses.COLOR_BLACK)
def main(stdscr): stdscr.clear() curses.use_default_colors() #curses.init_color(1,400,0,0) if curses.has_colors(): for i in xrange(1, curses.COLORS): curses.init_pair(i, i, curses.COLOR_BLACK) stdscr.addstr(str(curses.color_content(i))+"COLOR %d! " % i, curses.color_pair(i)) stdscr.addstr(str(curses.can_change_color())+"BOLD! ", curses.color_pair(i) | curses.A_BOLD) stdscr.addstr("STANDOUT! ", curses.color_pair(i) | curses.A_STANDOUT) stdscr.addstr("UNDERLINE! ", curses.color_pair(i) | curses.A_UNDERLINE) stdscr.addstr("BLINK! ", curses.color_pair(i) | curses.A_BLINK) stdscr.addstr("DIM! ", curses.color_pair(i) | curses.A_DIM) stdscr.addstr("REVERSE! ", curses.color_pair(i) | curses.A_REVERSE) stdscr.refresh() stdscr.getch()
def addString(y, x, string, string_color=color.BLACK, bold=False): if x == position.CENTER: x = width/2 - len(string)/2 options = 0 if curses.can_change_color(): # tokens special cases color if string == 'X': options = curses.color_pair(color.RED) if not bold else curses.color_pair(color.RED_H) | curses.A_BOLD elif string == 'O': options = curses.color_pair(color.YELLOW) if not bold else curses.color_pair(color.YELLOW_H) | curses.A_BOLD else: options = curses.color_pair(string_color) if bold: options |= curses.A_BOLD stdscr.addstr(y, x, string, options) stdscr.refresh()
def init(): """ Initialise the text interface. """ global screen, default_colors, can_change_palette if not curses: # fail silently, we're going to try ANSI return False # find a supported UTF-8 locale, with a preference for C, en-us, default languages = (['C', 'en-US', locale.getdefaultlocale()[0]] + [a for a in locale.locale_alias.values() if '.' in a and a.split('.')[1] == 'UTF-8']) for lang in languages: try: locale.setlocale(locale.LC_ALL,(lang, 'utf-8')) break except locale.Error: pass if locale.getlocale()[1] != 'UTF-8': logging.warning('No supported UTF-8 locale found.') return False # set the ESC-key delay to 25 ms unless otherwise set # set_escdelay seems to be unavailable on python curses. if not os.environ.has_key('ESCDELAY'): os.environ['ESCDELAY'] = '25' screen = curses.initscr() curses.noecho() curses.cbreak() curses.nonl() curses.raw() curses.start_color() screen.clear() # init_screen_mode() can_change_palette = (curses.can_change_color() and curses.COLORS >= 16 and curses.COLOR_PAIRS > 128) sys.stdout.write(ansi.esc_set_title % caption) sys.stdout.flush() if can_change_palette: default_colors = range(16, 32) else: # curses colours mapped onto EGA default_colors = ( curses.COLOR_BLACK, curses.COLOR_BLUE, curses.COLOR_GREEN, curses.COLOR_CYAN, curses.COLOR_RED, curses.COLOR_MAGENTA, curses.COLOR_YELLOW, curses.COLOR_WHITE, curses.COLOR_BLACK, curses.COLOR_BLUE, curses.COLOR_GREEN, curses.COLOR_CYAN, curses.COLOR_RED, curses.COLOR_MAGENTA, curses.COLOR_YELLOW, curses.COLOR_WHITE) return True
def main(screen): screen.clear() screen.keypad(True) curses.curs_set(False) width = curses.COLS height = curses.LINES if(height < 20 or width < 50): raise RuntimeError("This terminal is too damn small!") if not (curses.has_colors()): raise RuntimeError("This terminal does not support colors!") if not (curses.can_change_color()): raise RuntimeError("This terminal does not support changing color definitions!") conf = configs.nice_conf menu.confmenu(conf, screen) screen.nodelay(True) screen.clear() screen.refresh() mainwin = curses.newwin(height-7, width, 0, 0) statuswin = curses.newwin(7, width, height-7, 0) while(1): world = World(mainwin, conf) activeplayer = 0 n_turns = 1 for p in itertools.cycle(world.players): if(p.isdead): continue world.wind = randint(max(-conf['wind_max'], world.wind-conf['wind_change']), min( conf['wind_max'], world.wind+conf['wind_change'])) p.isactive = True p.active_shots = 0 while ((p.isactive or p.active_shots > 0) and not len([p for p in world.players if not p.isdead]) <= 1 ): gamestep(screen, mainwin, statuswin, p, world, conf, n_turns) if (len([p for p in world.players if not p.isdead]) == 1): gameover(screen, [p for p in world.players if not p.isdead][0]) break if (len([p for p in world.players if not p.isdead]) == 0): gameover(screen, None) break n_turns += 1
def initialize(): curses.curs_set(False) curses.use_default_colors() curses.init_pair(CP_DEFAULT, -1, -1) succeeded = False # init_color may fail even if this returns True if curses.can_change_color(): try: active_fg = curses.COLOR_WHITE active_bg = OFFSET_CP + CP_ACTIVE highlighted_fg = curses.COLOR_WHITE highlighted_bg = OFFSET_CP + CP_HIGHLIGHTED on_fg = OFFSET_CP + CP_ON off_fg = OFFSET_CP + CP_OFF enabled_fg = OFFSET_CP + CP_ENABLED static_fg = OFFSET_CP + CP_STATIC curses.init_color(active_bg, 250, 400, 800) curses.init_color(highlighted_bg, 350, 350, 350) curses.init_color(on_fg, 0, 800, 350) curses.init_color(off_fg, 800, 0, 350) curses.init_color(enabled_fg, 0, 750, 1000) curses.init_color(static_fg, 500, 500, 1000) succeeded = True except curses.error: pass if not succeeded: active_fg = curses.COLOR_WHITE active_bg = curses.COLOR_BLUE highlighted_fg = curses.COLOR_BLACK highlighted_bg = curses.COLOR_WHITE on_fg = curses.COLOR_GREEN off_fg = curses.COLOR_RED enabled_fg = curses.COLOR_CYAN static_fg = curses.COLOR_MAGENTA curses.init_pair(CP_ACTIVE, active_fg, active_bg) curses.init_pair(CP_HIGHLIGHTED, highlighted_fg, highlighted_bg) curses.init_pair(CP_ON, on_fg, -1) curses.init_pair(CP_OFF, off_fg, -1) curses.init_pair(CP_ENABLED, enabled_fg, -1) curses.init_pair(CP_STATIC, static_fg, -1)
def main(): stdscr = curses.initscr() # MANDATORY! curses.start_color() # MANDATORY! curses.use_default_colors() # optional. stdscr.addstr("can_change_color(): " + str(curses.can_change_color()) + '\n') stdscr.addstr("COLOR_PAIRS: " + str(curses.COLOR_PAIRS) + '\n\n') #################################### stdscr.addstr("color 67 before:\n") curses.init_pair(20, 67, 0) stdscr.addstr(str(curses.color_content(67)) + '\n', curses.color_pair(20)) stdscr.addstr("color 67 after:\n") curses.init_color(67, 0, 255, 0) curses.init_pair(21, 67, 0) stdscr.addstr(str(curses.color_content(67)) + '\n\n', curses.color_pair(21)) #################################### stdscr.addstr("color 101 before:\n") curses.init_pair(22, 101, 0) stdscr.addstr(str(curses.color_content(101)) + '\n', curses.color_pair(22)) stdscr.addstr("color 101 after:\n") curses.init_color(101, 128, 128, 128) curses.init_pair(23, 101, 0) stdscr.addstr(str(curses.color_content(101)) + '\n\n', curses.color_pair(23)) #################################### stdscr.addstr("pair 40 before:\n") stdscr.addstr(str(curses.pair_content(40)) + '\n', curses.color_pair(40)) stdscr.addstr("pair 40 after:\n") curses.init_pair(40, 101, 67) stdscr.addstr(str(curses.pair_content(40)) + '\n\n', curses.color_pair(40)) stdscr.getch() curses.endwin() # optional, but recommended.
def DISPLAY_basic(self): res = [] res.append("This is a line of text.") res.append("This is a test of the curse display function.") res.append("This screen is {} by {},".format( self.maxx, self.maxy)) res.append("The maximum viewing area is {} by {}.".format( self.maxx - (2 * self.xmargin), self.maxy - (2 * self.ymargin))) res.append("It is now {}.".format(time.asctime())) res.append("Does the window have colors? {}".format( str(curses.has_colors()))) res.append("Can the window change colors? {}".format( str(curses.can_change_color()))) res.append("The window has {} colors and {} color pairs".format( str(curses.COLORS), str(curses.COLOR_PAIRS))) res.append("self.__dir__() has {} entries".format(len( self.__dir__()))) res.append("") return res, []