def open_menu(stdscr, items):
"""Opens a menu containing items and returns the selected item.
Blocks until the user selected an item.
"""
width = max(map(len, items)) + 20
height = len(items*2)-1 + 4 # +2 for frame, +2 for padding
curses.curs_set(False)
selected = 0
while True:
center = (curses.COLS//2, curses.LINES//2)
menu_rect = Rect(center[0]-width//2, center[1]-height//2, width, height)
menu_rect = draw_frame(stdscr, menu_rect, thick_border=True)
for i, item in enumerate(items):
attr = curses.A_NORMAL
if i == selected:
attr = curses.A_STANDOUT
stdscr.addstr(menu_rect.y + 1 + i*2, center[0] - len(item)//2, item, attr)
c = stdscr.getch()
if c == curses.KEY_UP:
selected -= 1
if c == curses.KEY_DOWN:
selected += 1
if c == curses.KEY_ENTER or c == 10:
break
selected = clamp(selected, 0, len(items)-1)
curses.curs_set(True)
return items[selected]
python类A_NORMAL的实例源码
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 draw_form(self):
super().draw_form()
menu_advert = " " + self.__class__.MENU_KEY + ": Menu "
x, y = self.display_menu_advert_at()
if isinstance(menu_advert, bytes):
menu_advert = menu_advert.decode('utf-8', 'replace')
self.add_line(
y, x,
menu_advert,
self.make_attributes_list(menu_advert, curses.A_NORMAL),
self.columns - x
)
def WinCprint(self,y,x,msg,color=curses.A_NORMAL):
self.lockvideo.acquire()
try:
#self.VWscreen.addstr(y,x,msg[:msg.find('\n')],color)
self.VWscreen.addstr(y,x,msg,color)
self.VWscreen.refresh()
except:
pass
self.lockvideo.release()
def start_top_window(window, display):
window_lines, window_cols = window.getmaxyx()
bottom_line = window_lines - 1
window.bkgd(curses.A_NORMAL, curses.color_pair(2))
window.scrollok(1)
while True:
# alternate color pair used to visually check how frequently this loop runs
# to tell when user input is blocking
window.addstr(bottom_line, 1, display.recv_string())
window.move(bottom_line, 1)
window.scroll(1)
window.refresh()
def runCheck(initType, scrn):
if initType == "systemd":
try:
getStatus = subprocess.Popen(['systemctl', 'status', 'cgroup_py'], stdout=subprocess.PIPE)
except subprocess.CalledProcessError as e:
eMsg = "Unable to get status of cgroup_py daemon. Exiting!"
scrn.addstr(1,1,eMsg)
time.sleep(2)
sys.exit(2)
statlines = getStatus.communicate()[0].splitlines()
for l in statlines:
if "Active:" in l:
status = l.split(':')[1]
if 'active' in status and not 'inactive' in status:
pass
else:
eMsg = "Cgroup_py daemon does not appear to be active. Exiting."
scrn.erase()
scrn.addstr(1,1,eMsg)
scrn.refresh()
time.sleep(2)
sys.exit(2)
elif initType == "sysV":
try:
getStatus = subprocess.Popen(['/etc/init.d/cgroup_py', 'status'], stdout=subprocess.PIPE)
except subprocess.CalledProcessError as e:
eMsg = "Unable to get status of cgroup_py daemon. Exiting!"
scrn.addstr(1,1, eMsg)
time.sleep(2)
sys.exit(2)
if not "running" in getStatus.communicate()[0]:
eMsg = "Cgroup_py daemon doesn't appear to be running. Exiting."
scrn.addstr(1,1, eMsg, curses.A_NORMAL)
scrn.refresh()
time.sleep(2)
sys.exit(2)
else:
print "Cgroup_py daemon found."
def render_chat(users, history, screen, x, y, w, h):
for (i, a) in enumerate(history[-h:]):
buffer = []
if 'user' in a:
if a['user'] in users:
user_attr = curses.A_BOLD
else:
user_attr = curses.A_NORMAL
if a['message'].startswith('/me '):
text = '* {0} {1}'.format(a['user'], a['message'][4:])
buffer = [
('* ', curses.A_NORMAL),
(a['user'], user_attr),
(u' {0}'.format(a['message'][4:]), curses.A_NORMAL)]
else:
buffer = [
(a['user'], user_attr),
(u'> {0}'.format(a['message']), curses.A_NORMAL)]
else:
buffer = [(u'* {0}'.format(a), curses.A_NORMAL)]
x_ = x + 1
for (text, attr) in buffer:
if not isinstance(text, six.binary_type):
text = text.encode('utf8')
screen.addstr(y + 1 + i, x_, text, attr)
x_ += len(text.decode('utf8'))
def select_mode(self, index):
if self.has_focus:
if self.first_item_index == 0 and index == self.position:
mode = curses.A_REVERSE
elif (self.position - self.first_item_index) == index:
mode = curses.A_REVERSE
elif (self.position + 1 == self.last_item_index) and (index == len(self.employees) - 1):
mode = curses.A_REVERSE
else:
mode = curses.A_NORMAL
else:
mode = curses.A_NORMAL
return mode
def display(self):
self.window.clear()
while True:
self.window.refresh()
curses.doupdate()
for index, item in enumerate(self.items):
if index == self.position:
mode = curses.A_REVERSE
else:
mode = curses.A_NORMAL
msg = '- %s\n (Features: %s, Design: %s)' % (item, item.features, item.design_need)
self.window.addstr(1+index*2, 2, msg, mode)
key = self.window.getch()
if key in [curses.KEY_ENTER, ord('\n')]:
if self.position == len(self.items)-1:
return None
else:
return self.position
if key == curses.KEY_UP:
self.navigate(-1)
elif key == curses.KEY_DOWN:
self.navigate(1)
def create(liste):
screen = curses.initscr()
curses.start_color()
curses.init_pair(1,curses.COLOR_RED, curses.COLOR_WHITE)
s = curses.color_pair(1)
h = curses.A_NORMAL
pos = 0
while 1:
curses.noecho()
screen.clear()
screen.border(0)
screen.keypad(True)
screen.addstr(1,2,"(w)Yukari/Up (s)Asagi/Down (e)Sec", curses.A_BOLD)
a = 0
b = 4
c = 3
for oge in liste:
if int(a/15)<1:
if pos == a:
screen.addstr(b, 4, "".join(liste[a]), s)
else:
screen.addstr(b, 4, "".join(liste[a]), h)
else:
c = c + 1
if pos == a:
screen.addstr(c, 23, "".join(liste[a]), s)
else:
screen.addstr(c, 23, "".join(liste[a]), h)
a = a + 1
b = b + 1
screen.refresh()
try:
inp = screen.getkey(1,1)
if inp == 'e':
screen.refresh()
curses.echo()
screen.keypad(False)
curses.endwin()
break
if inp == 'w':
if pos > 0:
pos = pos - 1
else:
pos = len(liste)-1
if inp== 's':
if pos < len(liste)-1:
pos = pos + 1
else:
pos=0
except Exception as e:
pass
return pos
def addline(self, y, string, attr):
"""
Displays a string on the screen. Handles truncation and borders.
"""
if y >= self.max_y:
return
# Display the left blank border.
self.addstr(
y=y,
x=0,
string=' ' * self.offset_x,
attr=curses.A_NORMAL,
)
# Remove trailing spaces so the truncate logic works correctly.
string = string.rstrip()
# Truncate the string if it is too long.
if self.offset_x + len(string) + self.offset_x > self.max_x:
string = string[:self.max_x - self.offset_x - self.offset_x - 2] + '..'
# Add whitespace between the end of the string and the edge of the
# screen. This is required when scrolling, to blank out characters
# from other lines that had been displayed here previously.
string += ' ' * (self.max_x - self.offset_x - len(string) - self.offset_x)
# Display the string.
self.addstr(
y=y,
x=self.offset_x,
string=string,
attr=attr,
)
# Display the right blank border.
self.addstr(
y=y,
x=self.max_x - self.offset_x,
string=' ' * self.offset_x,
attr=curses.A_NORMAL,
)
def show_funclist(self, pos, item_sup, item_inf):
"""
Draw `function selection list` frame with a index number of the item
selected and the range of items to be displayed.
:param pos: Index of selected item in `function selection list`.
:type pos: int or None
:param item_sup: Upper bound of item index from `function selection
list`.
:type item_sup: int
:param item_inf: Lower bound of item index from `function selection
list`.
:type item_inf: int
"""
# Set UI variable
screen = self._stdscr.subwin(18, 26, 2, 26)
screen.bkgd(' ', curses.color_pair(4))
normal = curses.A_NORMAL
select = curses.color_pair(5)
select += curses.A_BOLD
list_height = 15
# Set local variable
ip = self.settings[1][1]
item_len = len(self.choice[ip])
# Function list
items_show = self.choice[ip][item_sup:item_inf]
items_selec = self._funcs[ip][item_sup:item_inf]
for p, item in enumerate(items_show):
sel_ch = '+' if items_selec[p] else ' '
item_str = ("[%s] %s" % (sel_ch, item[3])).ljust(23)
item_pos = pos - item_sup if pos is not None else None
highlight = select if p == item_pos else normal
if item_len > list_height:
if item_inf - item_sup == list_height - 2:
screen.addstr(4 + p, 2, item_str, highlight)
elif item_inf == item_len:
screen.addstr(4 + p, 2, item_str, highlight)
elif item_sup == 0:
screen.addstr(3 + p, 2, item_str, highlight)
else:
screen.addstr(3 + p, 2, item_str, highlight)
if item_len > list_height:
if item_inf - item_sup == list_height - 2:
screen.addstr(3, 2, " More ".center(23, '.'), normal)
screen.addch(3, 15, curses.ACS_UARROW)
screen.addstr(17, 2, " More ".center(23, '.'), normal)
screen.addch(17, 15, curses.ACS_DARROW)
elif item_inf == item_len:
screen.addstr(3, 2, " More ".center(23, '.'), normal)
screen.addch(3, 15, curses.ACS_UARROW)
elif item_sup == 0:
screen.addstr(17, 2, " More ".center(23, '.'), normal)
screen.addch(17, 15, curses.ACS_DARROW)
else:
for line_i in range(list_height - item_len):
screen.addstr(17 - line_i, 2, ' ' * 23, normal)
if not items_show:
screen.addstr(4, 2, "No data file!".center(23), normal)
screen.refresh()
self._item_sup, self._item_inf = item_sup, item_inf
def process_bar(self, done, block, total, mode=1):
"""
Draw `Process Bar` at the bottom which is used to indicate progress of
downloading operation.
.. note:: This method is a callback function responses to
:meth:`urllib.urlretrieve` method while fetching hosts data
file.
:param done: Block count of packaged retrieved.
:type done: int
:param block: Block size of the data pack retrieved.
:type block: int
:param total: Total size of the hosts data file.
:type total: int
:param mode: A flag indicating the status of `Process Bar`.
The default value of `mode` is `1`.
==== ====================================
mode `Process Bar` status
==== ====================================
1 Downloading operation is processing.
0 Not downloading.
==== ====================================
:type mode: int
"""
screen = self._stdscr.subwin(2, 80, 20, 0)
screen.bkgd(' ', curses.color_pair(4))
normal = curses.A_NORMAL
line_width = 76
prog_len = line_width - 20
# Progress Bar
if mode:
done *= block
prog = 1.0 * prog_len * done / total
progress = ''.join(['=' * int(prog), '-' * int(2 * prog % 2)])
progress = progress.ljust(prog_len)
total = CommonUtil.convert_size(total).ljust(7)
done = CommonUtil.convert_size(done).rjust(7)
else:
progress = ' ' * prog_len
done = total = 'N/A'.center(7)
# Show Progress
prog_bar = "[%s] %s | %s" % (progress, done, total)
screen.addstr(1, 2, prog_bar, normal)
screen.refresh()
def main():
"""
The entry point for the app. Called when music-scraper is typed in terminal.
Starts the GUI and starts the scraping process after the input is given
"""
curses.initscr()
if curses.COLS < 80 or curses.LINES < 5:
curses.endwin()
print('Terminal\'s dimensions are too small')
return
process = CrawlerProcess({'LOG_ENABLED': False})
def gui_input(screen):
GUI.screen = screen
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
GUI.box = curses.newwin(curses.LINES, curses.COLS, 0, 0)
GUI.message = GUI.get_input()
curses.wrapper(gui_input)
s = request.quote(GUI.message)
MusicSpider.start_urls = [
"http://www.google.com/search?q=" + s,
]
process.crawl(MusicSpider)
thread = GUIThread(process, start_gui)
thread.start()
process.start()
if not GUI.gui_stopped:
if len(GUI.strings) == 0:
GUI.box.erase()
GUI.box.addstr(1, 1, "No Results Found... Try with Some other keywords.", GUI.high_light_text)
GUI.add_bottom_menus()
GUI.screen.refresh()
GUI.box.refresh()
else:
GUI.box.addstr(curses.LINES - 2, 1, "Completed Scraping !!", GUI.high_light_text)
GUI.add_bottom_menus()
GUI.screen.refresh()
GUI.box.refresh()
def set_list(self, node):
self.win.erase()
highlighted = node.highlighted
if not node.item_list:
no_data_str = "no VPN data"
self.win.addstr(self.height//2,(self.width-len(no_data_str))//2,
no_data_str)
self.win.refresh()
return
list_len = len(node.item_list)
half_win_height = self.height//2
if highlighted < half_win_height or list_len < self.height:
left = 0
right = min(list_len, self.height)
elif highlighted + half_win_height < list_len:
left = highlighted - half_win_height
right = highlighted + half_win_height
else:
left = list_len - self.height
right = min(highlighted + half_win_height, list_len)
for index in range(left, right):
try:
item = node.item_list[index]
except IndexError:
self.win.addstr("Index out of range!")
break
text_atr = curses.A_REVERSE if index == highlighted else curses.A_NORMAL
left_str = str(item[0])
if isinstance(item[1], int):
right_str = str(item[1])
else:
right_str = "{:.2f}".format(item[1])
left_str = left_str if len(left_str)+len(right_str) < self.width else left_str[:self.width-len(right_str)-3]+'~'
left_str = left_str.ljust(self.width-len(right_str)-1)
item_str = "{}{}".format(left_str, right_str)
self.win.addstr(index-left, 0, item_str, text_atr)
self.win.refresh()