From 56263ba0e091fd36852e925c8ddab98864d3a2f6 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sun, 5 Jun 2022 22:51:39 +0530 Subject: [PATCH] Fixed broken search and management due to newline characters. --- gempost | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/gempost b/gempost index c790e90..27f61a6 100755 --- a/gempost +++ b/gempost @@ -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 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 @@ -270,13 +272,15 @@ def manage(direct = ""): allposts = i.readlines() 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 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 = [] for i in postsList: # formatting it into a string list for the ncurses menu function @@ -290,7 +294,7 @@ def manage(direct = ""): print("\nCancelled.") 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: 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 (direct == ""): - print(f'\nEditing "{postsList[which][0]}" ...') - exec(f"{editor} {postDir}{postsList[which][1]}.gmi") + print(f'\nEditing "{postsList[which][1][:-1]}" ...') + exec(f"{editor} {postDir}{postsList[which][0]}.gmi") else: print(f'\nEditing {direct}.gmi') exec(f"{editor} {postDir}{direct}.gmi") 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 if (direct == ""): - exec(f"mv {postDir}{postsList[which][1]}.gmi {wdir}trash/") - exec(f"sed -i '/{postsList[which][1]}.gmi/d' {indexFile}") + exec(f"mv {postDir}{postsList[which][0]}.gmi {wdir}trash/") + exec(f"sed -i '/{postsList[which][0]}.gmi/d' {indexFile}") else: exec(f"mv {postDir}{direct}.gmi {wdir}trash/") exec(f"sed -i '/{direct}.gmi/d' {indexFile}") @@ -510,10 +514,19 @@ elif (arg == "search"): with open(f"{indexFile}",'r') as i: 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 = [] - for i in allposts: # creating a list containing titles corresponding to filenames for posts that have the search term in their titles - if t in i[56:-1]: - postsList.append(i[11:].split(".gmi ", 1)) + for i in tempList: + if t in i[1]: + postsList.append(i) + """ + for i in postsList: # removing the \n characters + if i[1][-1] == '\n': + i[1] == i[1][:-1] + """ menuItems = [] 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.") exit() 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]) ") if (c == 'y' or c == 'Y'): - manage(f"{postsList[which][1]}") + manage(f"{postsList[which][0]}") elif (arg == "purge"): # permanently delete trashed posts checkInit()