Designed the windows and settled with ncurses
This commit is contained in:
81
ex1.py
Normal file
81
ex1.py
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import curses
|
||||||
|
from curses import panel
|
||||||
|
|
||||||
|
|
||||||
|
class Menu(object):
|
||||||
|
def __init__(self, items, stdscreen):
|
||||||
|
self.window = stdscreen.subwin(0, 0)
|
||||||
|
self.window.keypad(1)
|
||||||
|
self.panel = panel.new_panel(self.window)
|
||||||
|
self.panel.hide()
|
||||||
|
panel.update_panels()
|
||||||
|
|
||||||
|
self.position = 0
|
||||||
|
self.items = items
|
||||||
|
self.items.append(("exit", "exit"))
|
||||||
|
|
||||||
|
def navigate(self, n):
|
||||||
|
self.position += n
|
||||||
|
if self.position < 0:
|
||||||
|
self.position = 0
|
||||||
|
elif self.position >= len(self.items):
|
||||||
|
self.position = len(self.items) - 1
|
||||||
|
|
||||||
|
def display(self):
|
||||||
|
self.panel.top()
|
||||||
|
self.panel.show()
|
||||||
|
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 = "%d. %s" % (index, item[0])
|
||||||
|
self.window.addstr(1 + index, 1, msg, mode)
|
||||||
|
|
||||||
|
key = self.window.getch()
|
||||||
|
|
||||||
|
if key in [curses.KEY_ENTER, ord("\n")]:
|
||||||
|
if self.position == len(self.items) - 1:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
self.items[self.position][1]()
|
||||||
|
|
||||||
|
elif key == curses.KEY_UP:
|
||||||
|
self.navigate(-1)
|
||||||
|
|
||||||
|
elif key == curses.KEY_DOWN:
|
||||||
|
self.navigate(1)
|
||||||
|
|
||||||
|
self.window.clear()
|
||||||
|
self.panel.hide()
|
||||||
|
panel.update_panels()
|
||||||
|
curses.doupdate()
|
||||||
|
|
||||||
|
|
||||||
|
class MyApp(object):
|
||||||
|
def __init__(self, stdscreen):
|
||||||
|
self.screen = stdscreen
|
||||||
|
curses.curs_set(0)
|
||||||
|
|
||||||
|
submenu_items = [("beep", curses.beep), ("flash", curses.flash)]
|
||||||
|
submenu = Menu(submenu_items, self.screen)
|
||||||
|
|
||||||
|
main_menu_items = [
|
||||||
|
("beep", curses.beep),
|
||||||
|
("flash", curses.flash),
|
||||||
|
("submenu", submenu.display),
|
||||||
|
]
|
||||||
|
main_menu = Menu(main_menu_items, self.screen)
|
||||||
|
main_menu.display()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
curses.wrapper(MyApp)
|
75
main.py
75
main.py
@@ -1,48 +1,51 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
from blessed import Terminal
|
import curses
|
||||||
|
from curses import wrapper
|
||||||
menu = [["login to system"], ["create account"], ["disconnect"]]
|
|
||||||
|
|
||||||
|
|
||||||
def display_screen(selection):
|
|
||||||
term = Terminal()
|
|
||||||
term.clear()
|
|
||||||
|
|
||||||
for (idx, m) in enumerate(menu):
|
class MyApp(object):
|
||||||
if idx == selection:
|
def __init__(self, screen):
|
||||||
print('{t.bold_red_reverse}{title}'.format(t=term, title=m[0]))
|
curses.curs_set(0)
|
||||||
else:
|
curses.init_color(0,0,0,0)
|
||||||
print('{t.normal}{title}'.format(t=term, title=m[0]))
|
screen.box()
|
||||||
|
|
||||||
|
Cols22 = (curses.COLS - 2) * 0.22
|
||||||
|
Cols22 = int(Cols22)
|
||||||
|
|
||||||
|
Cols56 = (curses.COLS - 2) * 0.56
|
||||||
|
Cols56 = int(Cols56) + 1
|
||||||
|
|
||||||
|
Lines = (curses.LINES -2) * 0.95
|
||||||
|
Lines = int(Lines)
|
||||||
|
|
||||||
|
title = "Sparks-Skraps" + " COLS -> " + str(curses.COLS) + " LINES -> " + str(Cols56)
|
||||||
|
titlePos = (curses.COLS / 2) - (len(title) / 2)
|
||||||
|
|
||||||
|
screen.addstr(1, int(titlePos), title)
|
||||||
|
screen.refresh()
|
||||||
|
|
||||||
|
|
||||||
def run_selection(selection):
|
components = curses.newwin(Lines,Cols22,3,1)
|
||||||
term.green_reverse('Running {}'.format(menu[selection][0]))
|
components.box()
|
||||||
|
components.addstr(1, 1, "precious components")
|
||||||
|
components.refresh()
|
||||||
|
|
||||||
|
parts = curses.newwin(Lines, Cols22, 3, 1+Cols22)
|
||||||
|
parts.box()
|
||||||
|
parts.addstr(1, 1, "My precious parts")
|
||||||
|
parts.refresh()
|
||||||
|
|
||||||
|
info = curses.newwin(Lines, Cols56, 3, 1+Cols22*2)
|
||||||
|
info.box()
|
||||||
|
info.addstr(1, 1, "My precious info")
|
||||||
|
info.refresh()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
c = screen.getch()
|
||||||
term = Terminal()
|
|
||||||
with term.fullscreen():
|
|
||||||
selection = 0
|
|
||||||
display_screen(selection)
|
|
||||||
selection_inprogress = True
|
|
||||||
with term.cbreak():
|
|
||||||
while selection_inprogress:
|
|
||||||
key = term.inkey()
|
|
||||||
if key.is_sequence:
|
|
||||||
if key.name == 'KEY_TAB':
|
|
||||||
selection += 1
|
|
||||||
if key.name == 'KEY_DOWN':
|
|
||||||
selection += 1
|
|
||||||
if key.name == 'KEY_UP':
|
|
||||||
selection -= 1
|
|
||||||
if key.name == 'KEY_ENTER':
|
|
||||||
selection_inprogress = False
|
|
||||||
elif key:
|
|
||||||
print("got {0}.".format(key))
|
|
||||||
|
|
||||||
selection = selection % len(menu)
|
|
||||||
|
|
||||||
display_screen(selection)
|
|
||||||
|
|
||||||
run_selection(selection)
|
if __name__ == "__main__":
|
||||||
|
curses.wrapper(MyApp)
|
||||||
|
Reference in New Issue
Block a user