Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/sharedra.../python
File: picker.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
[1] Fix | Delete
# hi, this routine isn't licensed. but credit would be nice somewhere.
[2] Fix | Delete
# See http://pp19dd.com/2013/11/simple-python-list-picker-using-curses/
[3] Fix | Delete
[4] Fix | Delete
# usage:
[5] Fix | Delete
# opts = Picker(
[6] Fix | Delete
# title = 'Delete all files',
[7] Fix | Delete
# options = ["Yes", "No"]
[8] Fix | Delete
# ).get_selected()
[9] Fix | Delete
[10] Fix | Delete
# returns a simple list
[11] Fix | Delete
# cancel returns False
[12] Fix | Delete
[13] Fix | Delete
import curses
[14] Fix | Delete
[15] Fix | Delete
[16] Fix | Delete
class Picker:
[17] Fix | Delete
"""Allows you to select from a list with curses"""
[18] Fix | Delete
[19] Fix | Delete
stdscr = None
[20] Fix | Delete
win = None
[21] Fix | Delete
title = ""
[22] Fix | Delete
arrow = ""
[23] Fix | Delete
footer = ""
[24] Fix | Delete
more = ""
[25] Fix | Delete
c_selected = ""
[26] Fix | Delete
c_empty = ""
[27] Fix | Delete
[28] Fix | Delete
cursor = 0
[29] Fix | Delete
offset = 0
[30] Fix | Delete
selected = 0
[31] Fix | Delete
selcount = 0
[32] Fix | Delete
aborted = False
[33] Fix | Delete
[34] Fix | Delete
window_height = 15
[35] Fix | Delete
window_width = 60
[36] Fix | Delete
all_options = []
[37] Fix | Delete
length = 0
[38] Fix | Delete
[39] Fix | Delete
def curses_start(self):
[40] Fix | Delete
self.stdscr = curses.initscr()
[41] Fix | Delete
curses.noecho()
[42] Fix | Delete
curses.cbreak()
[43] Fix | Delete
self.win = curses.newwin(
[44] Fix | Delete
5 + self.window_height, self.window_width, 2, 4
[45] Fix | Delete
)
[46] Fix | Delete
[47] Fix | Delete
def curses_stop(self):
[48] Fix | Delete
curses.nocbreak()
[49] Fix | Delete
self.stdscr.keypad(0)
[50] Fix | Delete
curses.echo()
[51] Fix | Delete
curses.endwin()
[52] Fix | Delete
[53] Fix | Delete
def get_selected(self):
[54] Fix | Delete
if self.aborted:
[55] Fix | Delete
return False
[56] Fix | Delete
[57] Fix | Delete
ret_s = [x for x in self.all_options if x["selected"]]
[58] Fix | Delete
ret = [x["label"] for x in ret_s]
[59] Fix | Delete
return ret
[60] Fix | Delete
[61] Fix | Delete
def redraw(self):
[62] Fix | Delete
self.win.clear()
[63] Fix | Delete
self.win.border(
[64] Fix | Delete
self.border[0],
[65] Fix | Delete
self.border[1],
[66] Fix | Delete
self.border[2],
[67] Fix | Delete
self.border[3],
[68] Fix | Delete
self.border[4],
[69] Fix | Delete
self.border[5],
[70] Fix | Delete
self.border[6],
[71] Fix | Delete
self.border[7],
[72] Fix | Delete
)
[73] Fix | Delete
self.win.addstr(self.window_height + 4, 5, " " + self.footer + " ")
[74] Fix | Delete
[75] Fix | Delete
position = 0
[76] Fix | Delete
option_range = self.all_options[
[77] Fix | Delete
self.offset : self.offset + self.window_height + 1
[78] Fix | Delete
]
[79] Fix | Delete
for option in option_range:
[80] Fix | Delete
if option["selected"]:
[81] Fix | Delete
line_label = self.c_selected + " "
[82] Fix | Delete
else:
[83] Fix | Delete
line_label = self.c_empty + " "
[84] Fix | Delete
[85] Fix | Delete
self.win.addstr(position + 2, 5, line_label + option["label"])
[86] Fix | Delete
position = position + 1
[87] Fix | Delete
[88] Fix | Delete
# hint for more content above
[89] Fix | Delete
if self.offset > 0:
[90] Fix | Delete
self.win.addstr(1, 5, self.more)
[91] Fix | Delete
[92] Fix | Delete
# hint for more content below
[93] Fix | Delete
if self.offset + self.window_height <= self.length - 2:
[94] Fix | Delete
self.win.addstr(self.window_height + 3, 5, self.more)
[95] Fix | Delete
[96] Fix | Delete
self.win.addstr(0, 5, " " + self.title + " ")
[97] Fix | Delete
self.win.addstr(
[98] Fix | Delete
0,
[99] Fix | Delete
self.window_width - 8,
[100] Fix | Delete
" " + str(self.selcount) + "/" + str(self.length) + " ",
[101] Fix | Delete
)
[102] Fix | Delete
self.win.addstr(self.cursor + 2, 1, self.arrow)
[103] Fix | Delete
self.win.refresh()
[104] Fix | Delete
[105] Fix | Delete
def check_cursor_up(self):
[106] Fix | Delete
if self.cursor < 0:
[107] Fix | Delete
self.cursor = 0
[108] Fix | Delete
if self.offset > 0:
[109] Fix | Delete
self.offset = self.offset - 1
[110] Fix | Delete
[111] Fix | Delete
def check_cursor_down(self):
[112] Fix | Delete
if self.cursor >= self.length:
[113] Fix | Delete
self.cursor = self.cursor - 1
[114] Fix | Delete
[115] Fix | Delete
if self.cursor > self.window_height:
[116] Fix | Delete
self.cursor = self.window_height
[117] Fix | Delete
self.offset = self.offset + 1
[118] Fix | Delete
[119] Fix | Delete
if self.offset + self.cursor >= self.length:
[120] Fix | Delete
self.offset = self.offset - 1
[121] Fix | Delete
[122] Fix | Delete
def curses_loop(self, stdscr):
[123] Fix | Delete
while 1:
[124] Fix | Delete
self.redraw()
[125] Fix | Delete
char = stdscr.getch()
[126] Fix | Delete
[127] Fix | Delete
if char == ord('q') or char == ord('Q'):
[128] Fix | Delete
self.aborted = True
[129] Fix | Delete
break
[130] Fix | Delete
if char == curses.KEY_UP:
[131] Fix | Delete
self.cursor = self.cursor - 1
[132] Fix | Delete
elif char == curses.KEY_DOWN:
[133] Fix | Delete
self.cursor = self.cursor + 1
[134] Fix | Delete
# elif c == curses.KEY_PPAGE:
[135] Fix | Delete
# elif c == curses.KEY_NPAGE:
[136] Fix | Delete
elif char == ord(' '):
[137] Fix | Delete
self.all_options[self.selected][
[138] Fix | Delete
"selected"
[139] Fix | Delete
] = not self.all_options[self.selected]["selected"]
[140] Fix | Delete
elif char == 10:
[141] Fix | Delete
break
[142] Fix | Delete
[143] Fix | Delete
# deal with interaction limits
[144] Fix | Delete
self.check_cursor_up()
[145] Fix | Delete
self.check_cursor_down()
[146] Fix | Delete
[147] Fix | Delete
# compute selected position only after dealing with limits
[148] Fix | Delete
self.selected = self.cursor + self.offset
[149] Fix | Delete
[150] Fix | Delete
temp = self.get_selected()
[151] Fix | Delete
self.selcount = len(temp)
[152] Fix | Delete
[153] Fix | Delete
def __init__(
[154] Fix | Delete
self,
[155] Fix | Delete
options,
[156] Fix | Delete
title='Select',
[157] Fix | Delete
arrow="-->",
[158] Fix | Delete
footer="Space = toggle, Enter = accept, q = cancel",
[159] Fix | Delete
more="...",
[160] Fix | Delete
border="||--++++",
[161] Fix | Delete
c_selected="[X]",
[162] Fix | Delete
c_empty="[ ]",
[163] Fix | Delete
):
[164] Fix | Delete
self.title = title
[165] Fix | Delete
self.arrow = arrow
[166] Fix | Delete
self.footer = footer
[167] Fix | Delete
self.more = more
[168] Fix | Delete
self.border = border
[169] Fix | Delete
self.c_selected = c_selected
[170] Fix | Delete
self.c_empty = c_empty
[171] Fix | Delete
[172] Fix | Delete
self.all_options = []
[173] Fix | Delete
[174] Fix | Delete
for option in options:
[175] Fix | Delete
self.all_options.append({"label": option, "selected": False})
[176] Fix | Delete
self.length = len(self.all_options)
[177] Fix | Delete
[178] Fix | Delete
self.curses_start()
[179] Fix | Delete
curses.wrapper(self.curses_loop)
[180] Fix | Delete
self.curses_stop()
[181] Fix | Delete
[182] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function