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()
python类use_default_colors()的实例源码
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 __enter__(self):
# Default delay when pressing ESC is too long, at 1000ms
os.environ["ESCDELAY"] = "25"
self.pairs = Pairs()
# Make curses use unicode
locale.setlocale(locale.LC_ALL, "")
self.screen = curses.initscr()
curses.noecho()
# Using raw instead of cbreak() gives us access to CTRL+C and others
curses.raw()
self.screen.keypad(True)
if not self.blocking_events:
self.screen.timeout(33)
curses.start_color()
curses.use_default_colors()
self.hide_cursor()
return self
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 main(stdscr):
curses.use_default_colors()
game_field = GameField(win=32)
state_actions = {} # Init, Game, Win, Gameover, Exit
def init():
game_field.reset()
return 'Game'
state_actions['Init'] = init
def not_game(state):
game_field.draw(stdscr)
action = get_user_action(stdscr)
responses = defaultdict(lambda: state)
responses['Restart'], responses['Exit'] = 'Init', 'Exit'
return responses[action]
state_actions['Win'] = lambda: not_game('Win')
state_actions['Gameover'] = lambda: not_game('Gameover')
def game():
game_field.draw(stdscr)
action = get_user_action(stdscr)
if action == 'Restart':
return 'Init'
if action == 'Exit':
return 'Exit'
if game_field.move(action): # move successful
if game_field.is_win():
return 'Win'
if game_field.is_gameover():
return 'Gameover'
return 'Game'
state_actions['Game'] = game
state = 'Init'
while state != 'Exit':
state = state_actions[state]()
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__(self, ts, injector, tests, do_tick, disassembler=disas_capstone):
self.ts = ts;
self.injector = injector
self.T = tests
self.gui_thread = None
self.do_tick = do_tick
self.ticks = 0
self.last_ins_count = 0
self.delta_log = deque(maxlen=self.RATE_Q)
self.time_log = deque(maxlen=self.RATE_Q)
self.disas = disassembler
self.stdscr = curses.initscr()
curses.start_color()
# doesn't work
# self.orig_colors = [curses.color_content(x) for x in xrange(256)]
curses.use_default_colors()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
self.stdscr.nodelay(1)
self.sx = 0
self.sy = 0
self.init_colors()
self.stdscr.bkgd(curses.color_pair(self.WHITE))
self.last_time = time.time()
def start(self, no_delay):
self.window = curses.initscr()
curses.start_color()
curses.use_default_colors()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
self.window.nodelay(no_delay)
self.init_colors()
self.window.bkgd(curses.color_pair(self.WHITE))
locale.setlocale(locale.LC_ALL, '') # set your locale
self.code = locale.getpreferredencoding()
def __init__(self):
"""
Initialize a new TUI window in terminal.
"""
locale.setlocale(locale.LC_ALL, '')
self._stdscr = curses.initscr()
curses.start_color()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
# Set colors
curses.use_default_colors()
for i, color in enumerate(self.colorpairs):
curses.init_pair(i + 1, *color)
def run(self):
'''
Runs all the windows added to the application, and returns a `Result`
object.
'''
result = Result()
try:
self.scr = curses.initscr()
self.MAX_HEIGHT, self.MAX_WIDTH = self.scr.getmaxyx()
curses.noecho()
curses.cbreak()
curses.start_color()
curses.use_default_colors()
self.window = self.scr.subwin(0, 0)
self.window.keypad(1)
self.window.nodelay(1)
self._run_windows()
self.threads += [gevent.spawn(self._input_loop)]
gevent.joinall(self.threads)
for thread in self.threads:
if thread.exception:
result._extract_thread_exception(thread)
except KeyboardInterrupt:
result._extract_exception()
except Exception:
result._extract_exception()
finally:
if self.scr is not None:
self.scr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
return result
def __init__(self):
self.screen = curses.initscr()
self.screen.timeout(100) # the screen refresh every 100ms
# charactor break buffer
curses.cbreak()
self.screen.keypad(1)
self.netease = NetEase()
curses.start_color()
if Config().get_item('curses_transparency'):
curses.use_default_colors()
curses.init_pair(1, curses.COLOR_GREEN, -1)
curses.init_pair(2, curses.COLOR_CYAN, -1)
curses.init_pair(3, curses.COLOR_RED, -1)
curses.init_pair(4, curses.COLOR_YELLOW, -1)
else:
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK)
# term resize handling
size = terminalsize.get_terminal_size()
self.x = max(size[0], 10)
self.y = max(size[1], 25)
self.startcol = int(float(self.x) / 5)
self.indented_startcol = max(self.startcol - 3, 0)
self.update_space()
self.lyric = ''
self.now_lyric = ''
self.tlyric = ''
self.storage = Storage()
self.config = Config()
self.newversion = False
def create_screen(fn):
"""
Initializes curses and passes a Screen to the main loop function `fn`.
Based on curses.wrapper.
"""
try:
# Make escape key more responsive
os.environ['ESCDELAY'] = '25'
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)
stdscr.nodelay(1)
curses.curs_set(0)
curses.mousemask(curses.BUTTON1_CLICKED)
if curses.has_colors():
curses.start_color()
curses.use_default_colors()
screen = Screen(stdscr)
fn(screen)
finally:
# Set everything back to normal
if 'stdscr' in locals():
curses.use_default_colors()
curses.curs_set(1)
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def init_colors():
"""
Init the colors for the screen
"""
curses.use_default_colors()
# Colors we use for messages, etc
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(6, curses.COLOR_BLACK, curses.COLOR_WHITE)
curses.init_pair(7, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(8, curses.COLOR_WHITE, curses.COLOR_WHITE)
colors['white'] = curses.color_pair(1)
colors['green'] = curses.color_pair(2)
colors['cyan'] = curses.color_pair(3)
colors['yellow'] = curses.color_pair(4)
colors['green-black'] = curses.color_pair(5)
colors['black-white'] = curses.color_pair(6)
colors['red'] = curses.color_pair(7)
colors['white-white'] = curses.color_pair(8)
# Allocate colour ranges here for the ma display.
maprange = 10
for i in range(curses.COLORS - maprange):
curses.init_pair(i + maprange, 0, i)
def main(stdscr):
def init():
# ??????
game_field.reset()
return 'Game'
def not_game(state):
# ?? GameOver ???
# ?????????Restart??Exit
game_field.draw(stdscr)
action = get_user_action(stdscr)
responses = defaultdict(lambda: state)
responses['Restart'], responses['Exit'] = 'Init', 'Exit'
return responses[action]
def game():
game_field.draw(stdscr)
action = get_user_action(stdscr)
if action == 'Restart':
return 'Init'
if action == 'Exit':
return 'Exit'
if game_field.move(action):
if game_field.is_win():
return 'Win'
if game_field.is_gameover():
return 'Gameover'
return 'Game'
state_actions = {
'Init': init,
'Win': lambda: not_game('Win'),
'Gameover': lambda: not_game('Gameover'),
'Game': game
}
curses.use_default_colors()
game_field = GameField(win=32)
state = 'Init'
while state != 'Exit':
state = state_actions[state]()
def screen_curses_init():
#
# number of milliseconds to wait after reading an escape character, to
# distinguish between an individual escape character entered on the
# keyboard from escape sequences sent by cursor and function keys (see
# curses(3X).
os.putenv("ESCDELAY", "0") # was 25
#
global STDSCR
STDSCR = curses.initscr()
curses.noecho()
curses.cbreak()
#
if not curses.has_colors():
raise Exception("Need colour support to run.")
curses.raw()
#
curses.start_color()
#
# This is what allows us to use -1 for default when we initialise
# the pairs
curses.use_default_colors()
#
curses.init_pair(PROFILE_GREY , curses.COLOR_WHITE , -1)
curses.init_pair(PROFILE_WHITE , curses.COLOR_WHITE , -1)
curses.init_pair(PROFILE_RED , curses.COLOR_RED , -1)
curses.init_pair(PROFILE_VERMILION , curses.COLOR_RED , -1)
curses.init_pair(PROFILE_ORANGE , curses.COLOR_RED , -1)
curses.init_pair(PROFILE_AMBER , curses.COLOR_YELLOW , -1)
curses.init_pair(PROFILE_YELLOW , curses.COLOR_YELLOW , -1)
curses.init_pair(PROFILE_CHARTREUSE , curses.COLOR_GREEN , -1)
curses.init_pair(PROFILE_GREEN , curses.COLOR_GREEN , -1)
curses.init_pair(PROFILE_TEAL , curses.COLOR_CYAN , -1)
curses.init_pair(PROFILE_BLUE , curses.COLOR_BLUE , -1)
curses.init_pair(PROFILE_VIOLET , curses.COLOR_MAGENTA , -1)
curses.init_pair(PROFILE_PURPLE , curses.COLOR_MAGENTA , -1)
curses.init_pair(PROFILE_MAGENTA , curses.COLOR_MAGENTA , -1)
curses.init_pair(PROFILE_BLACK_INFO , curses.COLOR_BLACK , curses.COLOR_WHITE)
curses.init_pair(PROFILE_ALARM, curses.COLOR_RED , curses.COLOR_WHITE)
def start_screen(self):
self.screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
self.screen.keypad(True)
curses.start_color()
curses.use_default_colors()
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(2, curses.COLOR_GREEN, -1)
curses.init_pair(3, curses.COLOR_CYAN, -1)
curses.init_pair(4, curses.COLOR_YELLOW, -1)
return None
def __init__(self):
self.screen = curses.initscr()
self.screen.timeout(100) # the screen refresh every 100ms
# charactor break buffer
curses.cbreak()
self.screen.keypad(1)
self.netease = NetEase()
curses.start_color()
if Config().get_item('curses_transparency'):
curses.use_default_colors()
curses.init_pair(1, curses.COLOR_GREEN, -1)
curses.init_pair(2, curses.COLOR_CYAN, -1)
curses.init_pair(3, curses.COLOR_RED, -1)
curses.init_pair(4, curses.COLOR_YELLOW, -1)
else:
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK)
# term resize handling
size = terminalsize.get_terminal_size()
self.x = max(size[0], 10)
self.y = max(size[1], 25)
self.startcol = int(float(self.x) / 5)
self.indented_startcol = max(self.startcol - 3, 0)
self.update_space()
self.lyric = ''
self.now_lyric = ''
self.tlyric = ''
self.storage = Storage()
self.config = Config()
self.newversion = False
def config_curses(self):
# use the default colors of the terminal
curses.use_default_colors()
# hide the cursor
curses.curs_set(0)
def defineColors():
curses.start_color()
curses.use_default_colors()
curses.init_pair(color.BLACK, curses.COLOR_BLACK, -1)
curses.init_pair(color.GREY, 250, -1)
curses.init_pair(color.RED, curses.COLOR_RED, -1)
curses.init_pair(color.YELLOW, 143, -1)
curses.init_pair(color.BLUE, curses.COLOR_BLUE, -1)
# highlight text
curses.init_pair(color.RED_H, curses.COLOR_RED, curses.COLOR_WHITE)
curses.init_pair(color.YELLOW_H, curses.COLOR_YELLOW, curses.COLOR_WHITE)
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 _start(self):
"""
Initialize the screen and input mode.
"""
self.s = curses.initscr()
self.has_color = curses.has_colors()
if self.has_color:
curses.start_color()
if curses.COLORS < 8:
# not colourful enough
self.has_color = False
if self.has_color:
try:
curses.use_default_colors()
self.has_default_colors=True
except _curses.error:
self.has_default_colors=False
self._setup_colour_pairs()
curses.noecho()
curses.meta(1)
curses.halfdelay(10) # use set_input_timeouts to adjust
self.s.keypad(0)
if not self._signal_keys_set:
self._old_signal_keys = self.tty_signal_keys()
super(Screen, self)._start()
def __init__(self,win):
self.win = win
self.size = self.win.getmaxyx()
curses.start_color()
curses.use_default_colors()
self.win.nodelay(1)
for i in range(0, curses.COLORS):
curses.init_pair(i + 1, i, -1)
curses.curs_set(0)
def colortest(stdscr):
# http://stackoverflow.com/questions/18551558/how-to-use-terminal-color-palette-with-curses
curses.start_color()
curses.use_default_colors()
stdscr.addstr(0,0,"%s colors on \"%s\"\n" % (str(curses.COLORS),str(curses.termname().decode('ascii'))))
for i in range(0, curses.COLORS):
curses.init_pair(i + 1, i, -1)
for i in range(curses.COLORS):
try:
stdscr.addstr(str(i)+"\t",curses.color_pair(i))
except:
pass
try:
stdscr.getch()
except: pass
def set_colours(colours):
"""
Set curses colours.
Arguments:
colours: Dict with colour information.
"""
crs.start_color()
# Define colours.
if colours['background'] == 'default':
crs.use_default_colors()
background = -1
else:
crs.init_color(0, *hex_to_rgb(colours['background']))
background = 0
crs.init_color(1, *hex_to_rgb(colours['foreground']))
crs.init_color(2, *hex_to_rgb(colours['highlight']))
crs.init_color(3, *hex_to_rgb(colours['content1']))
crs.init_color(4, *hex_to_rgb(colours['content2']))
# Define colour pairs.
crs.init_pair(1, 1, background)
crs.init_pair(2, 2, background)
crs.init_pair(3, 3, background)
crs.init_pair(4, 4, background)
# Set colours.
crs.start_color()
common.w.main.bkgdset(' ', crs.color_pair(1))
common.w.inbar.bkgdset(' ', crs.color_pair(1))
common.w.infobar.bkgdset(' ', crs.color_pair(2))
common.w.outbar.bkgdset(' ', crs.color_pair(4))
common.w.refresh()
def init_curses(cls, stdscr):
# An attempt to limit the damage from this bug in curses:
# https://bugs.python.org/issue13051
# The input textbox is 8 rows high. So assuming a maximum terminal
# width of 512 columns, we arrive at 8x512=4096. Most terminal windows
# should be smaller than this.
sys.setrecursionlimit(4096)
cls.stdscr = stdscr
curses.use_default_colors()
cls._init_color_pairs()
curses.curs_set(1)
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_curses(self):
self.stdscr.refresh()
curses.use_default_colors()
self.max_y, self.max_x = self.stdscr.getmaxyx()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
self.stdscr.keypad(1)
def main(stdscr):
def init():
#??????
game_field.reset()
return 'Game'
def not_game(state):
#?? GameOver ?? Win ???
game_field.draw(stdscr)
#????????action??????????????
action = get_user_action(stdscr)
responses = defaultdict(lambda: state) #???????????????????????
responses['Restart'], responses['Exit'] = 'Init', 'Exit' #???????????????
return responses[action]
def game():
#????????
game_field.draw(stdscr)
#????????action
action = get_user_action(stdscr)
if action == 'Restart':
return 'Init'
if action == 'Exit':
return 'Exit'
if game_field.move(action): # move successful
if game_field.is_win():
return 'Win'
if game_field.is_gameover():
return 'Gameover'
return 'Game'
state_actions = {
'Init': init,
'Win': lambda: not_game('Win'),
'Gameover': lambda: not_game('Gameover'),
'Game': game
}
curses.use_default_colors()
game_field = GameField(win=32)
state = 'Init'
#???????
while state != 'Exit':
state = state_actions[state]()