#!/opt/imh-python/bin/python3
# hi, this routine isn't licensed. but credit would be nice somewhere.
# See http://pp19dd.com/2013/11/simple-python-list-picker-using-curses/
# title = 'Delete all files',
# options = ["Yes", "No"]
"""Allows you to select from a list with curses"""
self.stdscr = curses.initscr()
self.win = curses.newwin(
5 + self.window_height, self.window_width, 2, 4
ret_s = [x for x in self.all_options if x["selected"]]
ret = [x["label"] for x in ret_s]
self.win.addstr(self.window_height + 4, 5, " " + self.footer + " ")
option_range = self.all_options[
self.offset : self.offset + self.window_height + 1
for option in option_range:
line_label = self.c_selected + " "
line_label = self.c_empty + " "
self.win.addstr(position + 2, 5, line_label + option["label"])
# hint for more content above
self.win.addstr(1, 5, self.more)
# hint for more content below
if self.offset + self.window_height <= self.length - 2:
self.win.addstr(self.window_height + 3, 5, self.more)
self.win.addstr(0, 5, " " + self.title + " ")
" " + str(self.selcount) + "/" + str(self.length) + " ",
self.win.addstr(self.cursor + 2, 1, self.arrow)
def check_cursor_up(self):
self.offset = self.offset - 1
def check_cursor_down(self):
if self.cursor >= self.length:
self.cursor = self.cursor - 1
if self.cursor > self.window_height:
self.cursor = self.window_height
self.offset = self.offset + 1
if self.offset + self.cursor >= self.length:
self.offset = self.offset - 1
def curses_loop(self, stdscr):
if char == ord('q') or char == ord('Q'):
if char == curses.KEY_UP:
self.cursor = self.cursor - 1
elif char == curses.KEY_DOWN:
self.cursor = self.cursor + 1
# elif c == curses.KEY_PPAGE:
# elif c == curses.KEY_NPAGE:
self.all_options[self.selected][
] = not self.all_options[self.selected]["selected"]
# deal with interaction limits
# compute selected position only after dealing with limits
self.selected = self.cursor + self.offset
temp = self.get_selected()
self.selcount = len(temp)
footer="Space = toggle, Enter = accept, q = cancel",
self.c_selected = c_selected
self.all_options.append({"label": option, "selected": False})
self.length = len(self.all_options)
curses.wrapper(self.curses_loop)