python tool: open note if only one match

This commit is contained in:
quenousimporte 2023-09-14 13:37:13 +02:00
parent 5181d573a6
commit 860d16f0c8
1 changed files with 7 additions and 4 deletions

View File

@ -7,12 +7,12 @@ import sys
import time import time
def listnotes(filter = ""): def listnotes(filter = ""):
cnt = 0 matching = []
for i in reversed(range(len(data))): for i in reversed(range(len(data))):
if filter in data[i]["title"]: if filter in data[i]["title"]:
print("[" + str(i) + "]", data[i]["title"]) print("[" + str(i) + "]", data[i]["title"])
cnt += 1 matching.append(data[i])
return cnt return matching
def readtextfile(path): def readtextfile(path):
with io.open(path, mode = "r", encoding = "utf-8") as f: with io.open(path, mode = "r", encoding = "utf-8") as f:
@ -100,7 +100,8 @@ while not (command == "quit" or command == "exit" or command == "q"):
elif note: elif note:
editnote(note) editnote(note)
else: else:
if listnotes(command) == 0: matching = listnotes(command)
if len(matching) == 0:
if ask("create '" + command + "'? "): if ask("create '" + command + "'? "):
note = { note = {
"title": command, "title": command,
@ -108,5 +109,7 @@ while not (command == "quit" or command == "exit" or command == "q"):
} }
data.insert(0, note) data.insert(0, note)
editnote(note) editnote(note)
elif len(matching) == 1:
editnote(matching.pop())
command = input("> ") command = input("> ")