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 time
def listnotes(filter = ""):
def listnotes(filter = "", grep = False):
matching = []
for i in reversed(range(len(data))):
if filter.lower() in data[i]["title"].lower():
print("[" + str(i) + "]", data[i]["title"])
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
def readtextfile(path):
@ -89,6 +96,9 @@ while not (command == "quit" or command == "exit" or command == "q"):
elif command[0:3] == "mv ":
action = "rename"
command = command[3:]
elif command[0:5] == "grep ":
action = "grep"
command = command[5:]
try:
index = int(command)
@ -106,11 +116,11 @@ while not (command == "quit" or command == "exit" or command == "q"):
if newname:
note["title"] = newname
savedata()
elif note:
elif note and not action == "grep":
editnote(note)
else:
matching = listnotes(command)
if len(matching) == 0:
matching = listnotes(command, action == "grep")
if len(matching) == 0 and not action == "grep":
if ask("create '" + command + "'? "):
note = {
"title": command,