Commit 8b5b9395 by Michelle Awh

stuff

parents e31b350d 91640d98
import sys,os
import curses
'''
if key == curses.KEY_MOUSE:
stdscr.clear()
stdscr.addstr(0, 0, "You clicked!")
'''
menu = ['Search', 'Filters', 'Exit']
filters = {'categories': ['garden [ ]', 'art [ ]', 'computers [ ]'], 'forbidden words': ['on [ ]', 'off [ ]'], }
def print_menu(stdscr, selected_row_idx):
stdscr.clear()
h, w = stdscr.getmaxyx()
for idx, row in enumerate(menu):
x = w//2 - len(row)//2
y = h//2 - len(menu)//2 + idx
if idx == selected_row_idx:
stdscr.attron(curses.color_pair(1))
stdscr.addstr(y, x, row)
stdscr.attroff(curses.color_pair(1))
else:
stdscr.addstr(y, x, row)
stdscr.refresh()
def get_center(stdscr, text):
h, w = stdscr.getmaxyx()
x = w//2 - len(text)//2
y = h//2
return y, x
def print_center(stdscr, text):
stdscr.clear()
y, x = get_center(stdscr, text)
stdscr.addstr(y, x, text)
stdscr.refresh()
def my_new_input(stdscr):
#y, x = get_center(stdscr, prompt_string)
stdscr.clear()
#curses.echo()
#window = curses.newwin(10, 60, 0, 0)
columns = [i for i in filters.keys()]
h, w = stdscr.getmaxyx()
col_split = w/5
j = 0
for i, value in enumerate(columns):
k = 5
j += int(col_split)
stdscr.addstr(k, j, value)
for inx, word in enumerate(filters[value]):
k+=2
stdscr.addstr(k, j, word)
stdscr.refresh()
input_ = stdscr.getstr(10 + 1, 100, 100)
#return input_
def my_raw_input(stdscr, prompt_string):
y, x = get_center(stdscr, prompt_string)
stdscr.clear()
curses.echo()
stdscr.addstr(y, x, prompt_string)
stdscr.refresh()
input_ = stdscr.getstr(y + 1, x, x)
return input_
def main(stdscr):
# turn off cursor blinking
curses.curs_set(0)
# color scheme for selected row
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
# specify the current selected row
current_row = 0
# print the menu
print_menu(stdscr, current_row)
b = "print_menu(stdscr, current_row)"
curses.mousemask(curses.BUTTON1_CLICKED)
stdscr.keypad(True)
key = 0
cursor_x = 0
cursor_y = 0
while key != ord('q'):
key = stdscr.getch()
if key == curses.KEY_UP and current_row > 0:
current_row -= 1
elif key == curses.KEY_DOWN and current_row < len(menu)-1:
current_row += 1
elif key == curses.KEY_ENTER or key in [10, 13]:
if menu[current_row] == 'Search':
my_raw_input(stdscr, 'enter your search')
b = "my_raw_input(stdscr, 'enter your search')"
# if user selected last row, exit the program
if menu[current_row] == 'Filters':
my_new_input(stdscr)
b = "my_new_input(stdscr)"
if menu[current_row] == 'Exit':
break
key = stdscr.getch()
elif key == curses.KEY_RIGHT:
print_menu(stdscr, current_row)
b = "print_menu(stdscr, current_row)"
eval(b)
curses.wrapper(main)
'''
def draw_menu(stdscr):
k = 0
cursor_x = 0
cursor_y = 0
# Clear and refresh the screen for a blank canvas
stdscr.clear()
stdscr.refresh()
# Start colors in curses
curses.start_color()
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
# Loop where k is the last character pressed
while (k != ord('q')):
# Initialization
stdscr.clear()
height, width = stdscr.getmaxyx()
if k == curses.KEY_DOWN:
cursor_y = cursor_y + 1
elif k == curses.KEY_UP:
cursor_y = cursor_y - 1
elif k == curses.KEY_RIGHT:
cursor_x = cursor_x + 1
elif k == curses.KEY_LEFT:
cursor_x = cursor_x - 1
cursor_x = max(0, cursor_x)
cursor_x = min(width-1, cursor_x)
cursor_y = max(0, cursor_y)
cursor_y = min(height-1, cursor_y)
# Declaration of strings
title = "Curses example"[:width-1]
subtitle = "Written by Clay McLeod"[:width-1]
keystr = "Last key pressed: {}".format(k)[:width-1]
statusbarstr = "Press 'q' to exit | STATUS BAR | Pos: {}, {}".format(cursor_x, cursor_y)
if k == 0:
keystr = "No key press detected..."[:width-1]
# Centering calculations
start_x_title = int((width // 2) - (len(title) // 2) - len(title) % 2)
start_x_subtitle = int((width // 2) - (len(subtitle) // 2) - len(subtitle) % 2)
start_x_keystr = int((width // 2) - (len(keystr) // 2) - len(keystr) % 2)
start_y = int((height // 2) - 2)
# Rendering some text
whstr = "Width: {}, Height: {}".format(width, height)
stdscr.addstr(0, 0, whstr, curses.color_pair(1))
# Render status bar
stdscr.attron(curses.color_pair(3))
stdscr.addstr(height-1, 0, statusbarstr)
stdscr.addstr(height-1, len(statusbarstr), " " * (width - len(statusbarstr) - 1))
stdscr.attroff(curses.color_pair(3))
# Turning on attributes for title
stdscr.attron(curses.color_pair(2))
stdscr.attron(curses.A_BOLD)
# Rendering title
stdscr.addstr(start_y, start_x_title, title)
# Turning off attributes for title
stdscr.attroff(curses.color_pair(2))
stdscr.attroff(curses.A_BOLD)
# Print rest of text
stdscr.addstr(start_y + 1, start_x_subtitle, subtitle)
stdscr.addstr(start_y + 3, (width // 2) - 2, '-' * 4)
stdscr.addstr(start_y + 5, start_x_keystr, keystr)
stdscr.move(cursor_y, cursor_x)
# Refresh the screen
stdscr.refresh()
# Wait for next input
k = stdscr.getch()
def main():
curses.wrapper(draw_menu)
if __name__ == "__main__":
main()
'''
'''
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
'''
import sys,os
import curses
stdscr = curses.initscr()
'''
#curses.mousemask(curses.BUTTON1_CLICKED)
def main(stdscr):
curses.curs_set(0)
curses.mousemask(1)
#curses.mousemask(curses.ALL_MOUSE_EVENTS)
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_RED)
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_GREEN)
stdscr.addstr(0, 0, "Hello!")
stdscr.addstr(1, 0, "Red")
stdscr.addstr(2, 0, "Green")
while 1:
stdscr.refresh()
key = stdscr.getch()
if key == curses.KEY_MOUSE:
_, x, y, _, _ = curses.getmouse()
if y == 1 and x in range(3):
stdscr.attron(curses.color_pair(1))
stdscr.addstr(0, 0, "Hello!")
stdscr.attroff(curses.color_pair(1))
elif y == 2 and x in range(5):
stdscr.attron(curses.color_pair(2))
stdscr.addstr(0, 0, "Hello!")
stdscr.attroff(curses.color_pair(2))
elif key == 27:
break
curses.wrapper(main)
'''
'''
menu = ['Home', 'Play', 'Scoreboard', 'Exit']
def my_raw_input(stdscr, r, c, prompt_string):
curses.echo()
stdscr.addstr(r, c, prompt_string)
stdscr.refresh()
input_ = stdscr.getstr(r+1, c, 20)
return input_
def get_center(stdscr, text):
h, w = stdscr.getmaxyx()
x = w//2 - len(text)//2
y = h//2
return y, x
def print_menu(stdscr, selected_row_idx):
stdscr.clear()
h, w = stdscr.getmaxyx()
for idx, row in enumerate(menu):
x = w//2 - len(row)//2
y = h//2 - len(menu)//2 + idx
if idx == selected_row_idx:
stdscr.attron(curses.color_pair(1))
stdscr.addstr(y, x, row)
stdscr.attroff(curses.color_pair(1))
else:
stdscr.addstr(y, x, row)
stdscr.refresh()
if __name__ == "__main__":
text = "hotter or colder"
stdscr = curses.initscr()
#stdscr.clear()
y, x = get_center(stdscr, text)
# turn off cursor blinking
curses.curs_set(0)
# color scheme for selected row
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
# specify the current selected row
current_row = 1
# print the menu
print_menu(stdscr, current_row)
while 1:
key = stdscr.getch()
if key == curses.KEY_UP and current_row > 0:
current_row -= 1
elif key == curses.KEY_DOWN and current_row < len(menu)-1:
stdscr.clear()
print('this is key', key)
current_row += 1
elif key == curses.KEY_ENTER or key in [10, 13]:
print('this is key', key)
stdscr.clear()
stdscr.addstr(y, x, 'You selected {}'.format(menu[current_row]))
stdscr.refresh()
stdscr.getch()
# if user selected last row, exit the program
if current_row == len(menu)-1:
break
print(current_row)
print_menu(stdscr, current_row)
curses.wrapper(main)
'''
'''
def my_raw_input(stdscr, r, c, prompt_string):
curses.echo()
stdscr.addstr(r, c, prompt_string)
stdscr.refresh()
input = stdscr.getstr(r + 1, c, 20)
return input
if __name__ == "__main__":
stdscr = curses.initscr()
stdscr.clear()
choice = my_raw_input(stdscr, 2, 3, "cool or hot?").lower()
if choice == "cool":
stdscr.addstr(5,3,"Super cool!")
elif choice == "hot":
stdscr.addstr(5, 3," HOT!")
else:
stdscr.addstr(5, 3," Invalid input")
stdscr.refresh()
stdscr.getch()
curses.endwin()
'''
'''
menu = ['Home', 'Play', 'Scoreboard', 'Exit']
def print_menu(stdscr, selected_row_idx):
stdscr.clear()
h, w = stdscr.getmaxyx()
for idx, row in enumerate(menu):
x = w//2 - len(row)//2
y = h//2 - len(menu)//2 + idx
if idx == selected_row_idx:
stdscr.attron(curses.color_pair(1))
stdscr.addstr(y, x, row)
stdscr.attroff(curses.color_pair(1))
else:
stdscr.addstr(y, x, row)
stdscr.refresh()
def print_center(stdscr, text):
stdscr.clear()
h, w = stdscr.getmaxyx()
x = w//2 - len(text)//2
y = h//2
stdscr.addstr(y, x, text)
stdscr.refresh()
def main(stdscr):
# turn off cursor blinking
curses.curs_set(0)
# color scheme for selected row
curses.start_color()
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
# specify the current selected row
current_row = 0
# print the menu
print_menu(stdscr, current_row)
while 1:
key = stdscr.getch()
if key == curses.KEY_UP and current_row > 0:
current_row -= 1
elif key == curses.KEY_DOWN and current_row < len(menu)-1:
current_row += 1
elif key == curses.KEY_ENTER or key in [10, 13]:
print_center(stdscr, "You selected '{}'".format(menu[current_row]))
stdscr.getch()
# if user selected last row, exit the program
if current_row == len(menu)-1:
break
print_menu(stdscr, current_row)
curses.wrapper(main)
'''
def draw_menu(stdscr):
k = 0
cursor_x = 0
cursor_y = 0
# Clear and refresh the screen for a blank canvas
stdscr.clear()
stdscr.refresh()
# Start colors in curses
curses.start_color()
curses.init_pair(1, curses.COLOR_CYAN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_WHITE)
# Loop where k is the last character pressed
while (k != ord('q')):
# Initialization
stdscr.clear()
height, width = stdscr.getmaxyx()
if k == curses.KEY_DOWN:
cursor_y = cursor_y + 1
elif k == curses.KEY_UP:
cursor_y = cursor_y - 1
elif k == curses.KEY_RIGHT:
cursor_x = cursor_x + 1
elif k == curses.KEY_LEFT:
cursor_x = cursor_x - 1
cursor_x = max(0, cursor_x)
cursor_x = min(width-1, cursor_x)
cursor_y = max(0, cursor_y)
cursor_y = min(height-1, cursor_y)
# Declaration of strings
title = "Curses example"[:width-1]
subtitle = "Written by Clay McLeod"[:width-1]
keystr = "Last key pressed: {}".format(k)[:width-1]
statusbarstr = "Press 'q' to exit | STATUS BAR | Pos: {}, {}".format(cursor_x, cursor_y)
if k == 0:
keystr = "No key press detected..."[:width-1]
# Centering calculations
start_x_title = int((width // 2) - (len(title) // 2) - len(title) % 2)
start_x_subtitle = int((width // 2) - (len(subtitle) // 2) - len(subtitle) % 2)
start_x_keystr = int((width // 2) - (len(keystr) // 2) - len(keystr) % 2)
start_y = int((height // 2) - 2)
# Rendering some text
whstr = "Width: {}, Height: {}".format(width, height)
stdscr.addstr(0, 0, whstr, curses.color_pair(1))
# Render status bar
stdscr.attron(curses.color_pair(3))
stdscr.addstr(height-1, 0, statusbarstr)
stdscr.addstr(height-1, len(statusbarstr), " " * (width - len(statusbarstr) - 1))
stdscr.attroff(curses.color_pair(3))
# Turning on attributes for title
stdscr.attron(curses.color_pair(2))
stdscr.attron(curses.A_BOLD)
# Rendering title
stdscr.addstr(start_y, start_x_title, title)
# Turning off attributes for title
stdscr.attroff(curses.color_pair(2))
stdscr.attroff(curses.A_BOLD)
# Print rest of text
stdscr.addstr(start_y + 1, start_x_subtitle, subtitle)
stdscr.addstr(start_y + 3, (width // 2) - 2, '-' * 4)
stdscr.addstr(start_y + 5, start_x_keystr, keystr)
stdscr.move(cursor_y, cursor_x)
# Refresh the screen
stdscr.refresh()
# Wait for next input
k = stdscr.getch()
def main():
curses.wrapper(draw_menu)
if __name__ == "__main__":
main()
'''
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
'''
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment