feat(py): grep command

This commit is contained in:
quenousimporte 2023-09-15 08:49:36 +02:00
parent 8f01d2f185
commit a017206120
1 changed files with 14 additions and 4 deletions

View File

@ -6,12 +6,19 @@ import os
import sys import sys
import time import time
def listnotes(filter = ""): def listnotes(filter = "", grep = False):
matching = [] matching = []
for i in reversed(range(len(data))): for i in reversed(range(len(data))):
if filter.lower() in data[i]["title"].lower(): if filter.lower() in data[i]["title"].lower():
print("[" + str(i) + "]", data[i]["title"]) print("[" + str(i) + "]", data[i]["title"])
matching.append(data[i]) matching.append(data[i])
elif grep and filter.lower() in data[i]["content"].lower():
print("[" + str(i) + "]", data[i]["title"])
for line in data[i]["content"].split("\n"):
if filter.lower() in line.lower():
index = line.lower().index(filter.lower())
print("\t", line[index:index+30])
matching.append(data[i])
return matching return matching
def readtextfile(path): def readtextfile(path):
@ -89,6 +96,9 @@ while not (command == "quit" or command == "exit" or command == "q"):
elif command[0:3] == "mv ": elif command[0:3] == "mv ":
action = "rename" action = "rename"
command = command[3:] command = command[3:]
elif command[0:5] == "grep ":
action = "grep"
command = command[5:]
try: try:
index = int(command) index = int(command)
@ -106,11 +116,11 @@ while not (command == "quit" or command == "exit" or command == "q"):
if newname: if newname:
note["title"] = newname note["title"] = newname
savedata() savedata()
elif note: elif note and not action == "grep":
editnote(note) editnote(note)
else: else:
matching = listnotes(command) matching = listnotes(command, action == "grep")
if len(matching) == 0: if len(matching) == 0 and not action == "grep":
if ask("create '" + command + "'? "): if ask("create '" + command + "'? "):
note = { note = {
"title": command, "title": command,