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()
python类start_color()的实例源码
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 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 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 init_display():
"""
Inits the display GUI
"""
if not GUI.gui_stopped:
curses.noecho()
curses.cbreak()
curses.start_color()
GUI.screen.keypad(1)
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_CYAN)
GUI.high_light_text = curses.color_pair(1)
GUI.normal_text = curses.A_NORMAL
curses.curs_set(0)
GUI.refresh_values()
GUI.position = 1
GUI.page = 1
GUI.box = curses.newwin(GUI.max_row + 3, curses.COLS, 0, 0)
GUI.box.addstr(1, 1, GUI.status, GUI.high_light_text)
GUI.add_bottom_menus()
GUI.screen.refresh()
GUI.box.refresh()
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 _init_screen(self):
screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.start_color()
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_WHITE)
screen.border(0)
return screen
def print_stock_data(col, row, data, title, scr_main, scr_strip, cursor_row, change_amount, scr_dim):
scr_strip.addstr(0, col+10, title)
data_length = len(str(data))
spaces_length = 9 - data_length
n = 0
if col+10+18 > scr_dim[1]:
spaces_length = spaces_length + scr_dim[1] - col-10-9
while n < spaces_length:
data = data + " "
n = n + 1
curses.start_color()
curses.init_pair(8, curses.COLOR_BLACK, curses.COLOR_RED)
curses.init_pair(9, curses.COLOR_BLACK, curses.COLOR_GREEN)
curses.init_pair(10, curses.COLOR_BLACK, curses.COLOR_YELLOW)
curses.init_pair(11, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(12, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(13, curses.COLOR_YELLOW, curses.COLOR_BLACK)
if cursor_row == 1:
if change_amount == -1:
scr_main.addstr(row, col, data, curses.color_pair(8))
elif change_amount == 1:
scr_main.addstr(row, col, data, curses.color_pair(9))
else:
scr_main.addstr(row, col, data, curses.color_pair(10))
else:
if change_amount == -1:
scr_main.addstr(row, col, data, curses.color_pair(11))
elif change_amount == 1:
scr_main.addstr(row, col, data, curses.color_pair(12))
else:
scr_main.addstr(row, col, data, curses.color_pair(13))
def input_n(cursor, scr_bottom, max_stock_range, stock_list, scr_dim):
stock_input = None
curses.start_color()
curses.init_pair(5,curses.COLOR_WHITE,curses.COLOR_BLUE)
stock_win = curses.newwin(1, 10, scr_dim[0]-1, 0)
stock_win.bkgd(curses.color_pair(5))
stock_box = textpad.Textbox(stock_win)
stock_win.refresh()
scr_bottom.addstr(0, curses.COLS-20, " [Enter]Save/Exit")
scr_bottom.refresh()
stock_input = stock_box.edit()
stock_input = stock_input.upper()
if str(stock_input) != "" and str(stock_input) not in stock_list:
stocks.add_stock_code(str(stock_input))
total_stocks = len(stock_list) + 1
if total_stocks > scr_dim[0] - 6:
cursor[1] = total_stocks
cursor[2] = max_stock_range
else:
cursor[1] = max_stock_range + 1
cursor[2] = cursor[1]
elif str(stock_input) or ((str(stock_input)[0:(len(str(stock_input)) - 2)] and str(stock_input)[len(str(stock_input))])) in stock_list:
total_stocks = len(stock_list)
stock_pos = stock_list.index(str(stock_input)) + 1
cursor[1] = stock_pos
if total_stocks > max_stock_range:
cursor[2] = 1
else:
cursor[2] = cursor[1]
return cursor
def print_permanents(scr_top, perm, row, col, perm_data, scr_dim):
if perm == "GC=F":
perm = "Gold"
elif perm == "SI=F":
perm = "Silver"
elif perm == "HG=F":
perm = "Copper"
elif perm == "CL=F":
perm = "Crude"
elif perm[-2:] == "=X":
perm = perm[0:3] + "/" + perm[3:6]
elif perm[0] == "^":
perm = perm[1:]
curses.start_color()
curses.init_pair(20, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(21, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(22, curses.COLOR_RED, curses.COLOR_BLACK)
try:
printing_perm = str(perm) + "=" + str(perm_data["price"])
except:
printing_perm = str(perm) + "=N/A"
perm_length = len(printing_perm) + 1
if perm_length+col < scr_dim[1]:
if perm_data["change"] != "N/A":
if float(perm_data["change"]) >= 0.5:
scr_top.addstr(1+row, col, str(printing_perm), curses.color_pair(20))
if float(perm_data["change"]) <= -0.5:
scr_top.addstr(1+row, col, str(printing_perm), curses.color_pair(22))
else:
scr_top.addstr(1+row, col, str(printing_perm), curses.color_pair(21))
else:
scr_top.addstr(1+row, col, str(printing_perm))
return perm_length
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()
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 show(self):
t = threading.Thread(target=self.get_data,args=())
t.setDaemon(True)
t.start()
try:
mainwindow = curses.initscr()
curses.cbreak(); mainwindow.keypad(1); curses.noecho()
mainwindow.border(0)
mainwindow.refresh()
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN)
(h,w)= mainwindow.getmaxyx()
l1= mainwindow.subwin(8,w/2,0,0)
l1.border(0)
t1 = threading.Thread(target=self._showdata,args=(l1,))
t1.setDaemon(True)
t1.start()
t1.join()
t.join()
mainwindow.addstr(h/2,w/2-15,"RUN OVER,PLEASE ENTER!",curses.color_pair(1))
mainwindow.getch()
finally:
curses.nocbreak(); mainwindow.keypad(0); curses.echo()
curses.endwin()
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 setup_colours(self):
curses.start_color()
# 0 - NOT_WORKED
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_RED)
# 1 - WORKED_COUNTRY_AND_STATION
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
# 2 - WORKED_COUNTRY_NOT_STATION
curses.init_pair(3, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
# 3 - WORKED_COUNTRY_DIFF_BAND
curses.init_pair(4, curses.COLOR_CYAN, curses.COLOR_BLACK)
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 run(self):
"""
"""
self.setup()
# Clear screen
self.screen.clear()
#
curses.start_color()
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_MAGENTA, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(5, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(6, curses.COLOR_WHITE, curses.COLOR_BLACK)
while True:
#
session.expire_all()
# TODO: Add some standard header to the top? (like interval time etc)
#
self.render()
#
self.increment.reset()
#
self.screen.refresh()
#
time.sleep(self.interval)
self.running += self.interval
return
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)