def __init__(self, file):
self.file = file
self.scr = curses.initscr()
self.scr.border()
self.scr_height, self.scr_width = self.scr.getmaxyx()
self.text_win = curses.newwin(self.scr_height - 1, self.scr_width, 1, 0)
self.file_text = file.content
if self.file_text != None:
self.text_win.addstr(self.file_text)
curses.noecho()
#curses.start_color()
#curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN)
if self.file.exists:
self.start_editor()
else:
curses.endwin()
gc.error('An error occurred while editing this file.')
python类init_pair()的实例源码
def __init__(self, username):
os.environ.setdefault('ESCDELAY', '25') # shorten esc delay
self.username = username
# set up IRC
self.channel = "##HTP"
# set up curses
self.scr = curses.initscr()
self.disconnect = False
curses.start_color()
self.scr_height, self.scr_width = self.scr.getmaxyx()
self.chatbar = curses.newwin(5, self.scr_height - 1, 5, 10)
self.msg_text = ''
self.logfile = '../data/irc.txt'
self.log_text = []
# curses color config
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN)
# start the client
try:
curses.wrapper(self.start_loop())
except Exception as e:
self.scr.addstr(2, 0, str(e), curses.A_REVERSE)
# client game loop
def colors_init():
"""
Initializes all color pairs possible into the dictionary CURSES_COLORPAIRS.
Thus, getting an attribute for a black foreground and a green background
would look like:
>>> curses_color.colors_init()
>>> green_black_attr = curses_color.CURSES_COLORPAIRS['black-green']
>>> stdscr.addstr(0, 0, "test", curses.color_pair(green_black_attr))
"""
if len(CURSES_COLORPAIRS): return
assert curses.has_colors(
), "Curses wasn't configured to support colors. Call curses.start_color()"
start_number = 120
for fg in BASE_CURSES_COLORS.keys():
for bg in BASE_CURSES_COLORS.keys():
pair_num = len(CURSES_COLORPAIRS) + start_number
curses.init_pair(pair_num, BASE_CURSES_COLORS[fg],
BASE_CURSES_COLORS[bg])
pair_name = "{}-{}".format(fg, bg)
CURSES_COLORPAIRS[pair_name] = pair_num
def __init__(self, stdscreen):
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
global screen
screen = stdscreen
self.screen = stdscreen
curses.curs_set(0)
main_menu_items = get_records()
main_menu_items += [{'field': 'Insert new station information record', 'function': selection_main_menu}]
main_menu = Menu(cnn, main_menu_items, self.screen, 'Station information new/edit/delete - %s.%s' % (stn['NetworkCode'], stn['StationCode']))
main_menu.display()
def initcolors():
""" initialize color pallet """
curses.start_color()
if not curses.has_colors():
raise RuntimeError("Sorry. Terminal does not support colors")
# setup colors on black background
for i in range(1,9):
curses.init_pair(i,i,BLACK)
CPS[i] = curses.color_pair(i)
# have to individually set up special cases
curses.init_pair(BUTTON,WHITE,GRAY) # white on gray for buttons
CPS[BUTTON] = curses.color_pair(BUTTON)
curses.init_pair(HLITE,BLACK,GREEN) # black on Green for highlight aps,stas
CPS[HLITE] = curses.color_pair(HLITE)
curses.init_pair(ERR,WHITE,RED) # white on red
CPS[ERR] = curses.color_pair(ERR)
curses.init_pair(WARN,BLACK,YELLOW) # white on yellow
CPS[WARN] = curses.color_pair(WARN)
curses.init_pair(NOTE,WHITE,GREEN) # white on red
CPS[NOTE] = curses.color_pair(NOTE)
def __init__(self):
self.selected_index_stack = [0]
self.returnString = ""
self.play_in_room = None
self.dir = DirBrowse()
self.selected_index = 0
self.selected_column = 0
self.window = curses.initscr()
curses.start_color()
curses.noecho()
curses.cbreak()
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_RED)
curses.init_pair(5, curses.COLOR_YELLOW, curses.COLOR_BLUE)
self.window.keypad(1)
self.draw_ui()
def main(stdscr):
curses.start_color()
curses.use_default_colors()
for i in range(0, curses.COLORS):
curses.init_pair(i + 1, i, -1)
try:
for i in range(0, 255):
stdscr.addstr(str(i), curses.color_pair(i))
stdscr.addstr(" ")
except curses.ERR:
# End of screen reached
pass
stdscr.getch()
def colorize(self):
curses.use_default_colors()
curses.init_pair(1, 7, -1)
curses.init_pair(2, -1, 7)
curses.init_pair(3, -1, 0)
curses.init_pair(4, 2, -1)
curses.init_pair(5, 3, -1)
curses.init_pair(6, 3, -1)
curses.init_pair(7, 6, -1)
curses.init_pair(8, 5, -1)
self.color_palette = {}
self.color_palette["Player"] = 0
self.color_palette["NPC"] = 1
self.color_palette["dark_wall"] = 2
self.color_palette["dark_floor"] = 3
self.color_palette["goblinoid"] = 4
self.color_palette["troll"] = 5
self.color_palette['canine'] = 6
self.color_palette['elf'] = 7
self.color_palette['dwarf'] = 8
def __init__(self):
self._colors = {
'white': curses.color_pair(self.PAIRS['white']),
'black': curses.color_pair(self.PAIRS['white']) | curses.A_REVERSE,
'red': curses.color_pair(self.PAIRS['red']),
'blue': curses.color_pair(self.PAIRS['blue']),
'green': curses.color_pair(self.PAIRS['green']),
'green_reverse': (curses.color_pair(self.PAIRS['green'])
| curses.A_REVERSE),
'cyan': curses.color_pair(self.PAIRS['cyan']),
'cyan_reverse': (curses.color_pair(self.PAIRS['cyan'])
| curses.A_REVERSE),
'yellow': curses.color_pair(self.PAIRS['yellow']),
'yellow_reverse': (curses.color_pair(self.PAIRS['yellow'])
| curses.A_REVERSE),
'magenta': curses.color_pair(self.PAIRS['magenta']),
'magenta_reverse': (curses.color_pair(self.PAIRS['magenta'])
| curses.A_REVERSE),
}
curses.start_color()
curses.use_default_colors()
for definition, (color, background) in self.DEFINITION.items():
curses.init_pair(definition, color, background)
def __getitem__(self, pair):
fg, bg = pair
assert isinstance(fg, int) and -1 <= fg < 256
assert isinstance(bg, int) and -1 <= bg < 256
if pair in self.pair_numbers:
# If pair is stored, retrieve it.
return curses.color_pair(self.pair_numbers[pair])
elif self.counter < MAX_PAIRS:
# If we still haven't filled our queue, add the new pair.
curses.init_pair(self.counter, fg, bg)
self.pair_numbers[pair] = self.counter
self.counter += 1
return curses.color_pair(self.counter - 1)
else:
# If the queue is full, pop the oldest one out
# and use its pair number to create the new one.
_, oldest = self.pair_numbers.popitem(last=False)
curses.init_pair(oldest, fg, bg)
self.pair_numbers[pair] = oldest
return curses.color_pair(oldest)
def __init__(self, enable=True):
self.enable = enable
if not self.enable:
return
self.logger = logging.getLogger('trader-logger')
self.stdscr = curses.initscr()
self.pad = curses.newpad(23, 120)
self.order_pad = curses.newpad(10, 120)
self.timestamp = ""
self.last_order_update = 0
curses.start_color()
curses.noecho()
curses.cbreak()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_RED)
self.stdscr.keypad(1)
self.pad.addstr(1, 0, "Waiting for a trade...")
def __init__(self, curses, curses_window):
curses.start_color()
curses.curs_set(0)
curses_window.nodelay(1)
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(6, curses.COLOR_BLUE, curses.COLOR_BLACK)
self._curses = curses
self._curses_window = curses_window
self._stack = list()
def setup(self):
curses.start_color()
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.cbreak()
curses.echo()
lines, cols = self.stdscr.getmaxyx()
self.logwin = self.stdscr.subwin(lines - 2, cols, 0, 0)
self.sepwin = self.stdscr.subwin(1, cols, lines - 2, 0)
self.cmdwin = self.stdscr.subwin(1, cols, lines - 1, 0)
self.logwin.scrollok(True)
self.sepwin.bkgd(curses.color_pair(1))
self.sepwin.refresh()
def countdown(stdscr, end_time, font, fg_color, bg_color, msg=None):
stdscr.clear()
curses.curs_set(False)
curses.init_pair(1, fg_color, bg_color)
now = datetime.datetime.now()
timefmt = '%I:%M %p' if now.day == end_time.day else '%I:%M %p, %d %b'
stdscr.addstr(0, 0, 'Alarm set for: ' + end_time.strftime(timefmt))
stdscr.refresh()
win = None
while now < end_time:
time_left = str(end_time - now).split('.')[0]
win = center(stdscr, time_left, font, curses.color_pair(1), win)
sleep(1)
now = datetime.datetime.now()
alert(stdscr, args, win)
def get_screen(self):
self.screen = curses.initscr()
curses.start_color()
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
if self.x == 0:
starty, startx = self.screen.getmaxyx()
self.x = startx
self.y = starty
resize = curses.is_term_resized(self.y, self.x)
# Action in loop if resize is True:
if resize is True:
y, x = self.screen.getmaxyx()
self.screen.clear()
curses.resizeterm(self.y, self.x)
self.screen.refresh()
self.show_header()
return self.screen
def _setup_colour_pairs(self):
"""
Initialize all 63 color pairs based on the term:
bg * 8 + 7 - fg
So to get a color, we just need to use that term and get the right color
pair number.
"""
if not self.has_color:
return
for fg in xrange(8):
for bg in xrange(8):
# leave out white on black
if fg == curses.COLOR_WHITE and \
bg == curses.COLOR_BLACK:
continue
curses.init_pair(bg * 8 + 7 - fg, fg, bg)
def processmenu(screen, menu, parent=None, status_bottom = 'Uninitialized...'):
curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.curs_set(0)
status_mid = ''
optioncount = len(menu['options'])
exitmenu = False
# response = None
while not exitmenu: #Loop until the user exits the menu
getin = runmenu(screen, menu, parent, status_mid, status_bottom)
if getin == optioncount:
exitmenu = True
elif menu['options'][getin]['type'] == COMMAND:
screen.clear() #clears previous screen
status_mid, status_bottom = processrequest(menu['options'][getin], screen) # Add additional space
## Show the updated status
screen.clear() #clears previous screen on key press and updates display based on pos
elif menu['options'][getin]['type'] == MENU:
screen.clear() #clears previous screen on key press and updates display based on pos
processmenu(screen, menu['options'][getin], menu, status_bottom) # display the submenu, and make sure the status is persistent
screen.clear() #clears previous screen on key press and updates display based on pos
elif menu['options'][getin]['type'] == EXITMENU:
exitmenu = True
def define_colors(self):
# set curses color pairs manually
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
curses.init_pair(6, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(7, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(8, curses.COLOR_CYAN, curses.COLOR_BLACK)
def set_color_pairs():
# based on the colors of pyradio
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(6, curses.COLOR_BLACK, curses.COLOR_MAGENTA)
curses.init_pair(7, curses.COLOR_BLACK, curses.COLOR_GREEN)
curses.init_pair(8, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
curses.init_pair(9, curses.COLOR_BLACK, curses.COLOR_GREEN)
def init_curses(self):
"""Setup the curses"""
self.window = curses.initscr()
self.window.keypad(True)
curses.noecho()
curses.cbreak()
curses.start_color()
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_CYAN)
self.current = curses.color_pair(2)
def conf_scr():
'''Configure the screen and colors/etc'''
curses.curs_set(0)
curses.start_color()
curses.use_default_colors()
text, banner, banner_text, background = get_theme_colors()
curses.init_pair(2, text, background)
curses.init_pair(3, banner_text, banner)
curses.halfdelay(10)
def init_colors(self):
curses.start_color()
curses.use_default_colors()
colors = [ curses.COLOR_BLUE,
curses.COLOR_CYAN,
curses.COLOR_GREEN,
curses.COLOR_MAGENTA,
curses.COLOR_RED,
curses.COLOR_WHITE,
curses.COLOR_YELLOW ]
curses.init_pair(0, curses.COLOR_WHITE, curses.COLOR_BLACK)
for i, c in enumerate(colors):
curses.init_pair(i + 1, c, curses.COLOR_BLACK)
def __init__(self, arena_size):
self.arena_size = arena_size
self.max_moves = int(25*(2*arena_size*COLORS)/(28*6))
self.screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
try:
curses.curs_set(False)
except curses.error:
pass
self.screen.nodelay(True)
self.window_size = self.screen.getmaxyx()
if self.window_size[0] < self.arena_size+4 or self.window_size[1] < self.arena_size*2:
print('Your screen is too short!')
exit()
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_GREEN)
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_CYAN)
curses.init_pair(4, curses.COLOR_WHITE, curses.COLOR_RED)
curses.init_pair(5, curses.COLOR_WHITE, curses.COLOR_MAGENTA)
curses.init_pair(6, curses.COLOR_WHITE, curses.COLOR_YELLOW)
curses.init_pair(7, curses.COLOR_WHITE, curses.COLOR_WHITE)
self.offset_x = int((self.window_size[1]-2*self.arena_size)/2)
self.offset_y = int((self.window_size[0]-self.arena_size)/2)
self.moves_position=[ self.offset_y+self.arena_size+1, self.offset_x+self.arena_size-5 ]
self.arena_initialize()
self.screen.addstr( self.offset_y-2, self.offset_x, self.title, curses.color_pair(0))
self.screen.addstr( self.offset_y-2, self.offset_x+2*self.arena_size-17, "Press '?' to help", curses.color_pair(0))
def __init__(self):
self.notification_count = 0
self.window = curses.initscr()
curses.start_color()
curses.noecho()
curses.cbreak()
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_WHITE, curses.COLOR_RED)
curses.init_pair(5, curses.COLOR_YELLOW, curses.COLOR_BLUE)
self.window.keypad(1)
self.draw_ui()
def config_curses(self):
# use the default colors of the terminal
curses.use_default_colors ( )
# hide the cursor
curses.curs_set (0)
# add some color for multi_select
# @todo make colors configurable
curses.init_pair (1, curses.COLOR_GREEN, curses.COLOR_WHITE)
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 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 main(screen):
screen.clear()
screen_y, screen_x = screen.getmaxyx()
screen.addstr(0, 0, str(screen.getmaxyx()))
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
global DIR
DIR = dir(screen)
while True:
c = screen.getch()
screen.addstr(2, 2, str(c) + ' ', curses.color_pair(1) |
curses.A_BLINK | curses.A_BOLD)
if c == 'q' or c == ord('q'):
break
def set(self, foreground=None, background=None):
self.foreground = foreground or self.foreground
self.background = background or self.background
curses.init_pair(self.index, self.foreground, self.background)
def main(screen):
screen.clear()
screen_y, screen_x = screen.getmaxyx()
screen.move(1, 1)
screen.addstr(str(screen.getmaxyx()))
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
while True:
c = screen.getch()
screen.move(2, 2)
# screen.addstr(str(c), curses.A_BLINK | curses.A_BOLD)
# screen.addstr(str(c), curses.color_pair(1))
screen.addstr(str(c), curses.color_pair(1) | curses.A_BLINK)
if c == 'q' or c == ord('q'):
break
def _get_color(fg, bg):
key = (fg, bg)
if key not in COLOR_PAIRS_CACHE:
# Use the pairs from 101 and after, so there's less chance they'll be overwritten by the user
pair_num = len(COLOR_PAIRS_CACHE) + 101
curses.init_pair(pair_num, fg, bg)
COLOR_PAIRS_CACHE[key] = pair_num
return COLOR_PAIRS_CACHE[key]