python tool: simplified cli

This commit is contained in:
quenousimporte 2023-09-14 09:02:57 +02:00
parent a4e015af10
commit accc2433ca
2 changed files with 27 additions and 20 deletions

View File

@ -6,9 +6,12 @@ import os
import sys import sys
def listnotes(filter = ""): def listnotes(filter = ""):
cnt = 0
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
return cnt
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:
@ -30,7 +33,7 @@ def editnote(note):
if newcontent != content: if newcontent != content:
listnotes() listnotes()
subprocess.call(["diff", "--color", "data/backupnote.md", "data/note.md"]) subprocess.call(settings["commands"]["diff"] + ["data/backupnote.md", "data/note.md"])
note["content"] = newcontent note["content"] = newcontent
data.remove(note) data.remove(note)
@ -47,6 +50,7 @@ def editnote(note):
subprocess.call(["curl", "-s", "-X", "POST", "-d", "@data/postdata", settings["url"] + "/handler.php"]) subprocess.call(["curl", "-s", "-X", "POST", "-d", "@data/postdata", settings["url"] + "/handler.php"])
else: else:
listnotes()
print("no change") print("no change")
settings = json.loads(readtextfile("settings.json")) settings = json.loads(readtextfile("settings.json"))
@ -59,29 +63,31 @@ subprocess.call([settings["commands"]["gpg"], "-q", "--yes", "--output", "data/b
data = json.loads(readtextfile("data/backupdata.json")) data = json.loads(readtextfile("data/backupdata.json"))
command = "list" command = ""
if len(sys.argv) > 1: if len(sys.argv) > 1:
command = sys.argv[1] command = sys.argv[1]
while not (command == "quit" or command == "exit" or command == "q"): while not (command == "quit" or command == "exit" or command == "q"):
if command == "list": try:
listnotes() index = int(command)
elif command.split()[0] == "find": note = data[index]
searchstring = command.replace("find ", "") except:
listnotes(searchstring) note = next((x for x in data if x["title"] == command), None)
if note:
editnote(note)
else: else:
cnt = listnotes(command)
try: if cnt == 0:
index = int(command) answer = input("create '" + command + "'? ")
note = data[index] if answer == "y" or answer == "yes":
note = {
except: "title": command,
note = next((x for x in data if x["title"] == command), None) "content": "---\ntitle: " + command + "\n---\n\n"
}
if note: data.insert(0, note)
editnote(note) editnote(note)
else:
print("unknown command")
command = input("> ") command = input("> ")

View File

@ -5,6 +5,7 @@
"commands": "commands":
{ {
"editor": ["vim"], "editor": ["vim"],
"gpg": "gpg" "gpg": "gpg",
"diff": ["diff", "--color"]
} }
} }