Fixed broken search and management due to newline characters.

This commit is contained in:
= 2022-06-05 22:51:39 +05:30
parent 27c729654e
commit 56263ba0e0

41
gempost
View File

@ -211,6 +211,8 @@ def newpost(title, existing_content = None, filename = None):
if (title[-1] == '\n'): # remove the \n at end of title if its there if (title[-1] == '\n'): # remove the \n at end of title if its there
title = title[:-1] title = title[:-1]
if (filename[-1] == '\n'): # remove the \n at end of filename if its there
filename = filename[:-1]
exec(f"touch {wdir + filename}.gmi") # creating empty file exec(f"touch {wdir + filename}.gmi") # creating empty file
@ -270,14 +272,16 @@ def manage(direct = ""):
allposts = i.readlines() allposts = i.readlines()
postsList = [] postsList = []
"""
for i in allposts: # creating a list containing titles corresponding to filenames
postsList.append([i[56:-1],i[11:51]])
"""
for i in allposts: # creating a list containing titles corresponding to filenames for i in allposts: # creating a list containing titles corresponding to filenames
postsList.append(i[11:].split(".gmi ", 1)) postsList.append(i[11:].split(".gmi ", 1))
for i in postsList: # removing the \n characters
if i[1][-1] == '\n':
i[1] == i[1][:-1]
if i[0][-1] == '\n':
i[0] = i[0][:-1]
menuItems = [] menuItems = []
for i in postsList: # formatting it into a string list for the ncurses menu function for i in postsList: # formatting it into a string list for the ncurses menu function
menuItems.append(f"{i[0]} | {i[1][:-1]}") menuItems.append(f"{i[0]} | {i[1][:-1]}")
@ -290,7 +294,7 @@ def manage(direct = ""):
print("\nCancelled.") print("\nCancelled.")
return 0 return 0
print(f"\nYou have selected:\n\nTITLE: {postsList[which][0]}\nFILENAME: {postsList[which][1]}.gmi") print(f"\nYou have selected:\n\nTITLE: {postsList[which][1][:-1]}\nFILENAME: {postsList[which][0]}.gmi")
try: try:
mode = int(input("\nSELECT MODE:\n1 - EDIT\n2 - DELETE\n3 - CANCEL\n-> ")) # what does the user want to do with the selection? mode = int(input("\nSELECT MODE:\n1 - EDIT\n2 - DELETE\n3 - CANCEL\n-> ")) # what does the user want to do with the selection?
@ -300,16 +304,16 @@ def manage(direct = ""):
if (mode == 1): # if mode is EDIT, open editor with the post file if (mode == 1): # if mode is EDIT, open editor with the post file
if (direct == ""): if (direct == ""):
print(f'\nEditing "{postsList[which][0]}" ...') print(f'\nEditing "{postsList[which][1][:-1]}" ...')
exec(f"{editor} {postDir}{postsList[which][1]}.gmi") exec(f"{editor} {postDir}{postsList[which][0]}.gmi")
else: else:
print(f'\nEditing {direct}.gmi') print(f'\nEditing {direct}.gmi')
exec(f"{editor} {postDir}{direct}.gmi") exec(f"{editor} {postDir}{direct}.gmi")
prGreen("\nPost updated.") prGreen("\nPost updated.")
elif (mode == 2): # for DELETE mode, first delete post file, delete its reference from {wdir}postIndex, and rebuild the {wdir}posts.gmi and {postDir}archive.gmi files elif (mode == 2): # for DELETE mode, first delete post file, delete its reference from {wdir}postIndex, and rebuild the {wdir}posts.gmi and {postDir}archive.gmi files
if (direct == ""): if (direct == ""):
exec(f"mv {postDir}{postsList[which][1]}.gmi {wdir}trash/") exec(f"mv {postDir}{postsList[which][0]}.gmi {wdir}trash/")
exec(f"sed -i '/{postsList[which][1]}.gmi/d' {indexFile}") exec(f"sed -i '/{postsList[which][0]}.gmi/d' {indexFile}")
else: else:
exec(f"mv {postDir}{direct}.gmi {wdir}trash/") exec(f"mv {postDir}{direct}.gmi {wdir}trash/")
exec(f"sed -i '/{direct}.gmi/d' {indexFile}") exec(f"sed -i '/{direct}.gmi/d' {indexFile}")
@ -510,10 +514,19 @@ elif (arg == "search"):
with open(f"{indexFile}",'r') as i: with open(f"{indexFile}",'r') as i:
allposts = i.readlines() allposts = i.readlines()
tempList = []
for i in allposts: # creating a list having titles corresponding to filenames for posts that have the search term in their titles
tempList.append(i[11:].split(".gmi ", 1))
postsList = [] postsList = []
for i in allposts: # creating a list containing titles corresponding to filenames for posts that have the search term in their titles for i in tempList:
if t in i[56:-1]: if t in i[1]:
postsList.append(i[11:].split(".gmi ", 1)) postsList.append(i)
"""
for i in postsList: # removing the \n characters
if i[1][-1] == '\n':
i[1] == i[1][:-1]
"""
menuItems = [] menuItems = []
for i in postsList: # formatting it into a string list for the ncurses menu function for i in postsList: # formatting it into a string list for the ncurses menu function
@ -525,11 +538,11 @@ elif (arg == "search"):
print("\nNo items found.") print("\nNo items found.")
exit() exit()
else: else:
print(f"\nYou have selected:\n\nTITLE: {postsList[which][0]}\nFILENAME: {postsList[which][1]}.gmi") print(f"\nYou have selected:\n\nTITLE: {postsList[which][1][:-1]}\nFILENAME: {postsList[which][0]}.gmi")
c = input("\nOpen manager? (y/[n]) ") c = input("\nOpen manager? (y/[n]) ")
if (c == 'y' or c == 'Y'): if (c == 'y' or c == 'Y'):
manage(f"{postsList[which][1]}") manage(f"{postsList[which][0]}")
elif (arg == "purge"): # permanently delete trashed posts elif (arg == "purge"): # permanently delete trashed posts
checkInit() checkInit()