2014-09-29 49 views
2

我試圖給我的curses輸出添加顏色。然而,挑戰在於文本是通過一個單獨的長字體打印的,即self.all_results。有沒有辦法給字符串的單個部分添加顏色。Curses中的顏色

def main(self,stdscr): 
    x,y = 0,0 # size of the window 
    xx,yy = 50,200 # where to place window - up,across 
    pad = curses.newpad(150,150) # nlines, ncols 
    pad_pos = 0 
    exit = False 

    pad.addstr(4,0,str(self.all_results)) 

    while not exit: 
     sleep(0.2) 
     if self.timer != None: 
      if time() - start > self.timer: 
       self.stop = True 
       break 

     pad.addstr(0,0,str(self.format_results())) 
     pad.refresh(pad_pos,0, x,y, xx,yy) 

     cmd = stdscr.getch() 
     stdscr.nodelay(1) 

     if cmd != -1: 
      pad.refresh(pad_pos,0, x,y, xx,yy) 
      if len(self.format_results().split('\n')) > 100: 
       if cmd == curses.KEY_DOWN: 
        if pad_pos < len(self.format_results())+1: 
         pad_pos += 1 
        try: 
         pad.refresh(pad_pos,0, x,y, xx,yy) 
        except curses.error: 
         pass 
       elif cmd == curses.KEY_UP: 
        if pad_pos != 0: 
         pad_pos -= 1 
        try: 
         pad.refresh(pad_pos,0, x,y, xx,yy) 
        except curses.error: 
         pass 
+1

如何添加顏色字符串的一部分,取決於如何選擇串色的哪一部分。它是通過偏移到字符串中嗎?由字符串的內容?通過位置在屏幕上? – 2014-09-29 14:08:45

+0

字符串很像...'probe1 | NY |錯誤\ nprobe1 | NY | OK' 我想ERROR以紅色和OK以綠色。, – felix001 2014-09-29 14:10:18

+0

當然。將字符串拆分成您想要着色的部分,並且不需要着色。調用'pad.attron'和'pad.attroff'來調用'pad.addstr'來獲得想要着色的部分。 – 2014-09-29 14:13:29

回答

1

我會用re最多分割字符串,然後使用的addstrx,y形式,specifiying用於每個部分的顏色。

#!/usr/bin/env python 
import curses 
from curses.wrapper import wrapper 
import re 


def addstr_colorized(win, y, x, s): 
    colors = {'OK': curses.COLOR_GREEN, 'ERROR': curses.COLOR_RED} 
    win.move(y, x) 
    pattern = r'({0:s})'.format(
     '|'.join(r'\b{0:s}\b'.format(word) for word in colors.keys())) 
    s = re.split(pattern, s) 
    for s in s: 
     win.addstr(s, curses.color_pair(colors.get(s, 0))) 


def main(stdscr): 
    curses.init_pair(curses.COLOR_RED, 
        curses.COLOR_RED, 
        curses.COLOR_BLACK) 
    curses.init_pair(curses.COLOR_GREEN, 
        curses.COLOR_GREEN, 
        curses.COLOR_BLACK) 

    addstr_colorized(stdscr, 
        4, 
        0, 
        "This line is OK.\nBut there is an ERROR in this line\n") 
    stdscr.refresh() 
    stdscr.getch() 


wrapper(main) 

enter image description here

0

這是我用來實現它(不要太漂亮,但它是罰款,我在做什麼)。

您只需某處即定義顏色對:

curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_GREEN); 

然後,只需調用包含一個消息功能colourN [STR]其中N是顏色對Num和str是你想要的字符串的一部分有色。

例如:

addStrColour(stdscr, 0, "This is an example message, colour1[This is coloured using colour pair 1!!!] and now we have normal text again"); 

這是函數:

def addstrColour(stdscr, pos, message): 
    #Split messages based on colour components 
    newMes = re.split("(colour\d\[.*?\])", message); 

    totalOut = 0; 

    for line in newMes: 
    m = re.match("colour(\d{1})\[(.*)\]", line); 
    if m: 
     colourPairNum = int((m.groups()[0])); 

     stdscr.addstr(pos, totalOut, m.groups()[1], curses.color_pair(colourPairNum)); 
     totalOut += len(m.groups()[1]); 
    else: 
     stdscr.addstr(pos, totalOut, line); 
     totalOut += len(line);