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类start_color()的实例源码
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 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 __init__(self, hosts):
self.stdscr = curses.initscr()
curses.start_color()
self.height, self.width = self.stdscr.getmaxyx()
curses.cbreak()
curses.noecho()
self.stdscr.keypad(1)
self.hosts = hosts
self.format = (
'%(hostname)10.10s '
'%(pid)7.7s '
'%(ppid)7.7s '
'%(pcpu)6.6s '
'%(rss)5.5s '
'%(command)20s'
)
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 __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 __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 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 _create_desktop(self):
global _s_screen
_s_screen = curses.initscr()
curses.noecho()
curses.curs_set(0)
curses.start_color()
self._desktop = Form(None, 0, 0)
self._stats = Form(self._desktop, 0, 0, 70, 20)
self._stats.set_centering(True, True)
self._title = Label(self._stats, 0, 0, 'S2E')
self._title.set_centering(True, False)
self._exitmsg = Label(self._stats, 0, 17, 'Press q to exit')
self._exitmsg.set_centering(True, False)
self._table = Table(self._stats, 2, 2, self._data, self._legend,
self._layout)
self._table.set_centering(True, True)
def __init__(self, this_plant, this_data):
'''Initialization'''
self.initialized = False
self.screen = curses.initscr()
curses.noecho()
curses.raw()
curses.start_color()
try:
curses.curs_set(0)
except curses.error:
# Not all terminals support this functionality.
# When the error is ignored the screen will look a little uglier, but that's not terrible
# So in order to keep botany as accesible as possible to everyone, it should be safe to ignore the error.
pass
self.screen.keypad(1)
self.plant = this_plant
self.user_data = this_data
self.plant_string = self.plant.parse_plant()
self.plant_ticks = str(self.plant.ticks)
self.exit = False
self.infotoggle = 0
self.maxy, self.maxx = self.screen.getmaxyx()
# Highlighted and Normal line definitions
self.define_colors()
self.highlighted = curses.color_pair(1)
self.normal = curses.A_NORMAL
# Threaded screen update for live changes
screen_thread = threading.Thread(target=self.update_plant_live, args=())
screen_thread.daemon = True
screen_thread.start()
self.screen.clear()
self.show(["water","look","garden","instructions"], title=' botany ', subtitle='options')
def wrapper(func, *args, **kwds):
"""Wrapper function that initializes curses and calls another function,
restoring normal keyboard/screen behavior on error.
The callable object 'func' is then passed the main window 'stdscr'
as its first argument, followed by any other arguments passed to
wrapper().
"""
try:
# Initialize curses
stdscr = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
curses.noecho()
curses.cbreak()
# In keypad mode, escape sequences for special keys
# (like the cursor keys) will be interpreted and
# a special value like curses.KEY_LEFT will be returned
stdscr.keypad(1)
# Start color, too. Harmless if the terminal doesn't have
# color; user can test with has_color() later on. The try/catch
# works around a minor bit of over-conscientiousness in the curses
# module -- the error return from C start_color() is ignorable.
try:
curses.start_color()
except:
pass
return func(stdscr, *args, **kwds)
finally:
# Set everything back to normal
if 'stdscr' in locals():
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
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 __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 _inner_addstr(window, string, y=-1, x=-1):
assert curses.has_colors(), "Curses wasn't configured to support colors. Call curses.start_color()"
cur_y, cur_x = window.getyx()
if y == -1:
y = cur_y
if x == -1:
x = cur_x
for line in string.split(os.linesep):
_add_line(y, x, window, line)
# next line
y += 1
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 wrapper(func, *args, **kwds):
"""Wrapper function that initializes curses and calls another function,
restoring normal keyboard/screen behavior on error.
The callable object 'func' is then passed the main window 'stdscr'
as its first argument, followed by any other arguments passed to
wrapper().
"""
try:
# Initialize curses
stdscr = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
curses.noecho()
curses.cbreak()
# In keypad mode, escape sequences for special keys
# (like the cursor keys) will be interpreted and
# a special value like curses.KEY_LEFT will be returned
stdscr.keypad(1)
# Start color, too. Harmless if the terminal doesn't have
# color; user can test with has_color() later on. The try/catch
# works around a minor bit of over-conscientiousness in the curses
# module -- the error return from C start_color() is ignorable.
try:
curses.start_color()
except:
pass
return func(stdscr, *args, **kwds)
finally:
# Set everything back to normal
if 'stdscr' in locals():
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()
def __init__(self, ticks, silent, debug, compat_debug, debug_lines, autostep_debug, head):
super().__init__()
self.ticks = ticks
self.silent = silent
self.debug = debug
self.compat_debug = compat_debug
self.debug_lines = debug_lines
self.autostep_debug = autostep_debug
self.head = head
self.tick_number = 0
self.output_count = 0
if self.debug and not self.compat_debug:
self.logging_loc = 0
self.logging_x = 1
self.stdscr = 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)
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.noecho()
curses.curs_set(False)
self.win_program = curses.newwin(self.debug_lines, curses.COLS - 1, 0, 0)
self.logging_pad = curses.newpad(1000, curses.COLS - 1)
def signal_handler(signal, frame):
self.on_finish()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
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 wrapper(func, *args, **kwds):
"""Wrapper function that initializes curses and calls another function,
restoring normal keyboard/screen behavior on error.
The callable object 'func' is then passed the main window 'stdscr'
as its first argument, followed by any other arguments passed to
wrapper().
"""
try:
# Initialize curses
stdscr = curses.initscr()
# Turn off echoing of keys, and enter cbreak mode,
# where no buffering is performed on keyboard input
curses.noecho()
curses.cbreak()
# In keypad mode, escape sequences for special keys
# (like the cursor keys) will be interpreted and
# a special value like curses.KEY_LEFT will be returned
stdscr.keypad(1)
# Start color, too. Harmless if the terminal doesn't have
# color; user can test with has_color() later on. The try/catch
# works around a minor bit of over-conscientiousness in the curses
# module -- the error return from C start_color() is ignorable.
try:
curses.start_color()
except:
pass
return func(stdscr, *args, **kwds)
finally:
# Set everything back to normal
if 'stdscr' in locals():
stdscr.keypad(0)
curses.echo()
curses.nocbreak()
curses.endwin()