From 4def38f66f1f411f86cc1dd3880ffbd395023b83 Mon Sep 17 00:00:00 2001 From: Wolfgang Nowak Date: Sun, 28 Nov 2021 15:47:58 +0100 Subject: [PATCH] dialogtest for webgen - menues --- dialogtest2.py | 78 +++++++++++++++++++++++++++++++++----------------- 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/dialogtest2.py b/dialogtest2.py index 398de26..9e32fc6 100755 --- a/dialogtest2.py +++ b/dialogtest2.py @@ -5,38 +5,62 @@ from dialog import Dialog ## Initialize a dialog.Dialog instance d = Dialog(dialog="dialog") -clear = "\x1b[2J\x1b[H" -locale.setlocale(locale.LC_ALL, "") sourcedir = "/home/wn/python/webgen/" targetdir = "/home/wn/www/i21k.de/posts/" -sourcefolder = os.listdir(sourcedir) -targetfolder = os.listdir(targetdir) -xwidth = os.get_terminal_size()[0] - 10 -ywidth = os.get_terminal_size()[1] - 10 +locale.setlocale(locale.LC_ALL, "") +clear = "\x1b[2J\x1b[H" +xwidth = os.get_terminal_size()[0] - 3 +ywidth = os.get_terminal_size()[1] - 3 +os.chdir(sourcedir) -# Put files in sourcefolder into list -zahl = 0 -tabelle = [("", "")] -for zeile in sourcefolder: - tabelle.insert(zahl, (str(zahl), zeile)) - zahl += 1 +# Select a topic for the new post or reedit an existing one +def selectfile(): + # Put files in sourcefolder into list + zahl = 0 + tabelle = [] + for zeile in glob.glob("*.md"): + tabelle.insert(zahl, (str(zahl), zeile)) + zahl += 1 -# Open list in menu and let user choose one -ausgewaehlt = d.menu("Biddesehr:", width=xwidth, height=0, menu_height=ywidth, title="Such dir ne Datei", choices=tabelle) + # Open list in menu and let user choose one + ausgewaehlt = d.menu( + "Such dir ne Datei:", + width=xwidth, + height=ywidth, + menu_height=ywidth, + title="Ein Thema wählen:", + choices=tabelle, + cancel="Neuen Namen eingeben", + ) -if ausgewaehlt[0] == "cancel": - sys.exit("Na, dann eben nicht...") + if ausgewaehlt[0] == "cancel": + newtopic = d.inputbox("Ok, gib hier ein neues Thema an:", width=xwidth, height=ywidth, title="Ein Thema wählen:", cancel="Exit") + if newtopic[0] == "cancel" or newtopic[1] == "": + sys.exit("Na, dann eben nicht...") + headline = newtopic[1] + else: + headline = "" -# Clean filename from leading .md for later construction of a headline -fn = re.sub("\.md$", "", tabelle[int(ausgewaehlt[1])][1]) + # Clean filename from leading .md for later construction of a headline + if headline == "": + headline = re.sub("\.md$", "", tabelle[int(ausgewaehlt[1])][1]) -sourcefile = str(glob.glob(sourcedir + fn + ".md")[0]) -targetfile = glob.glob(targetdir + "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-2][0-9]:[0-6][0-9] " + fn + "*") -targetfilelength = len(targetfile) -if targetfilelength >= 1: - targetfile = targetfile[targetfilelength - 1] -if not targetfile: - targetfile = targetdir + datetime.datetime.now().strftime("%Y-%m-%d %H:00 ") + fn -print(clear, "\nSourcefile: ", sourcefile, "\nTargetfile: ", targetfile) + # if the searched filename doesnt match an existing file we construct a new one + if len(glob.glob(sourcedir + headline + ".md")) == 0: + sourcefile = sourcedir + headline + ".md" + else: + sourcefile = str(glob.glob(sourcedir + headline + ".md")[0]) -# return sourcefile, targetfile + # if the targetfilename doesnt match an existing file we construct a new one + targetfile = glob.glob(targetdir + "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-2][0-9]:[0-6][0-9] " + headline + "*") + targetfilelength = len(targetfile) + if targetfilelength >= 1: + targetfile = targetfile[targetfilelength - 1] + else: + targetfile = targetdir + datetime.datetime.now().strftime("%Y-%m-%d %H:00 ") + headline + + return sourcefile, targetfile, headline + + +antwort = selectfile() +print(antwort)