Added a scrolling ncurses menu for manage mode.
This commit is contained in:
parent
bfb5c22059
commit
8def32624b
106
gempost
106
gempost
|
@ -11,6 +11,9 @@ from sys import argv
|
|||
from hashlib import sha1
|
||||
from random import random
|
||||
|
||||
import curses
|
||||
from math import ceil
|
||||
|
||||
# colored output functions
|
||||
def prRed(skk): print("\033[91m {}\033[00m" .format(skk))
|
||||
def prGreen(skk): print("\033[92m {}\033[00m" .format(skk))
|
||||
|
@ -27,6 +30,99 @@ postDir = wdir + "postdir/" # all post files along with the archive.gmi are stor
|
|||
indexFile = wdir + "postIndex" # the index is maintained in this file
|
||||
editor = "nano --restricted -t"
|
||||
|
||||
def menuFunction(menu = []):
|
||||
stdscr = curses.initscr() # initializing curses
|
||||
curses.noecho()
|
||||
curses.cbreak()
|
||||
stdscr.keypad(True)
|
||||
|
||||
sy, sx = stdscr.getmaxyx() # getting terminal dimensions
|
||||
pageLength = sy - 5 # no. of items I can show in a single page.
|
||||
pages = ceil(len(menu)/pageLength) + 1 # calculating the no. of pages required
|
||||
|
||||
pageIndex = 1 # current page
|
||||
imenu = [] # internal list that is modified to only contain items of a specific page
|
||||
|
||||
endOfPage = pageLength # separate variable is required for this because the last page might have lesser entries than {pageLength}
|
||||
|
||||
imenu = menu[((pageLength)*(pageIndex-1)):(pageLength*pageIndex)]
|
||||
|
||||
if (menu == []): # do nothing if list is empty
|
||||
curses.endwin()
|
||||
print("There is nothing to manage.")
|
||||
return None
|
||||
|
||||
highlight, highlight_prev = 0, 0 # variable that controls which entry is selected, variable that stores its previous value
|
||||
while True:
|
||||
stdscr.clear()
|
||||
|
||||
if (highlight >= endOfPage): # incrementing page number
|
||||
if (pageIndex == pages - 1) or (pageIndex == 1):
|
||||
highlight = highlight_prev
|
||||
pass
|
||||
else:
|
||||
pageIndex += 1 # incrementing page index
|
||||
imenu = [] # resetting imenu
|
||||
try:
|
||||
imenu = menu[((pageLength)*(pageIndex-1)):(pageLength*pageIndex)] # only {pageLength} no. of items
|
||||
except IndexError:
|
||||
try:
|
||||
imenu = menu[((pageLength)*(pageIndex-1)):]
|
||||
except IndexError:
|
||||
pass
|
||||
endOfPage = len(imenu)
|
||||
highlight = 0
|
||||
|
||||
if (highlight < 0): # decrementing page number
|
||||
if (pageIndex == 1):
|
||||
highlight = highlight_prev
|
||||
pass
|
||||
else:
|
||||
pageIndex -= 1
|
||||
imenu = []
|
||||
try:
|
||||
imenu = menu[((pageLength)*(pageIndex-1)):(pageLength*pageIndex)]
|
||||
except IndexError:
|
||||
pass
|
||||
#sno = pageLength*(pageIndex-1)
|
||||
endOfPage = len(imenu)
|
||||
highlight = endOfPage - 1
|
||||
|
||||
stdscr.addstr("Use the arrow keys to make a selection. Press 'c' to cancel.\n")
|
||||
|
||||
for sno in range(len(imenu)):
|
||||
if (sno == highlight):
|
||||
stdscr.addstr(f"\n{((pageLength)*(pageIndex-1)) + (sno+1)} - {imenu[sno]}", curses.A_STANDOUT)
|
||||
else:
|
||||
stdscr.addstr(f"\n{((pageLength)*(pageIndex-1)) + (sno+1)} - {imenu[sno]}")
|
||||
sno += 1
|
||||
|
||||
stdscr.addstr(f"\n\npage {pageIndex} / {pages - 1}\n") # a line on the bottom showing page number
|
||||
stdscr.refresh()
|
||||
|
||||
c = stdscr.getch()
|
||||
|
||||
if (c == ord('c')): # None is returned so that no action is taken
|
||||
highlight = None
|
||||
break
|
||||
|
||||
elif (c == curses.KEY_DOWN):
|
||||
highlight_prev = highlight
|
||||
highlight += 1
|
||||
|
||||
elif (c == curses.KEY_UP):
|
||||
highlight_prev = highlight
|
||||
highlight -= 1
|
||||
|
||||
elif (c == curses.KEY_ENTER or c == 10 or c == 13):
|
||||
break
|
||||
|
||||
curses.endwin()
|
||||
if (highlight != None):
|
||||
return (pageLength * (pageIndex-1)) + highlight
|
||||
else:
|
||||
return highlight
|
||||
|
||||
def checkInit():
|
||||
""" Checks if the public_gemini/ directory has been initialized for use with gempost or is missing some files. """
|
||||
contents = listdir(wdir)
|
||||
|
@ -176,6 +272,11 @@ def manage():
|
|||
for i in allposts: # creating a list containing titles corresponding to filenames
|
||||
postsList.append([i[56:-1],i[11:51]])
|
||||
|
||||
menuItems = []
|
||||
for i in postsList: # formatting it into a string list for the ncurses menu function
|
||||
menuItems.append(f"{i[0]} | {i[1]}")
|
||||
|
||||
"""
|
||||
print() # line break
|
||||
try:
|
||||
counter = 0
|
||||
|
@ -187,6 +288,11 @@ def manage():
|
|||
except:
|
||||
prRed("\n Invalid selection. Exiting...")
|
||||
return 0
|
||||
"""
|
||||
|
||||
which = menuFunction(menuItems)
|
||||
print(f"\nYou have selected:\n\nTitle: {postsList[which][0]}\nFilename: {postsList[which][1]}.gmi")
|
||||
|
||||
try:
|
||||
mode = int(input("\nSELECT MODE:\n1 - EDIT\n2 - DELETE\n3 - CANCEL\n-> ")) # what does the user want to do with the selection?
|
||||
except ValueError:
|
||||
|
|
Loading…
Reference in New Issue