Fixed search failing in some dirs; fixed pathbar autofilling with one found path

This commit is contained in:
2023-09-16 01:18:36 -05:00
parent b5d1bbeccd
commit df688d919e
10 changed files with 132 additions and 80 deletions

View File

@@ -60,13 +60,6 @@ class IPCServer:
msg = conn.recv()
try:
if "SEARCH_DONE|" in msg:
ts, ret_code = msg.split("SEARCH_DONE|")[1].strip().split("|", 1)
timestamp = float(ts)
if self.fsearch_time_stamp or self.grep_time_stamp:
if (timestamp > self.fsearch_time_stamp) or (timestamp > self.grep_time_stamp):
GLib.idle_add(self.stop_spinner, (ret_code,), priority=GLib.PRIORITY_HIGH_IDLE)
if "SEARCH|" in msg:
ts, file = msg.split("SEARCH|")[1].strip().split("|", 1)
timestamp = float(ts)
@@ -78,6 +71,10 @@ class IPCServer:
timestamp = float(ts)
if data and (timestamp > self.grep_time_stamp):
GLib.idle_add(self._load_grep_ui, data, priority=GLib.PRIORITY_HIGH_IDLE)
if "SEARCH_DONE|" in msg:
ts, ret_code = msg.split("SEARCH_DONE|")[1].strip().split("|", 1)
GLib.idle_add(self.stop_spinner, (ret_code,), priority=GLib.PRIORITY_HIGH_IDLE)
except Exception as e:
print( repr(e) )

View File

@@ -32,8 +32,12 @@ dt = datetime.now()
ts = datetime.timestamp(dt)
def _log(message: str = "No message passed in...") -> None:
print(message)
def send_ipc_message(message) -> None:
conn = Client(address=_ipc_address, family="AF_UNIX", authkey=_ipc_authkey)
conn = Client(address = _ipc_address, family = "AF_UNIX", authkey = _ipc_authkey)
conn.send(message)
conn.close()
@@ -41,9 +45,11 @@ def send_ipc_message(message) -> None:
time.sleep(0.05)
def file_search(path, query):
def file_search(path: str = None, query: str = None) -> None:
if not path or not query: return
try:
for _path, _dir, _files in os.walk(path, topdown = True):
for _path, _dir, _files in os.walk(path, topdown = True, onerror = _log, followlinks = True):
for file in _files:
if query in file.lower():
target = os.path.join(_path, file)
@@ -54,14 +60,13 @@ def file_search(path, query):
traceback.print_exc()
def grep_search(target=None, query=None):
if not query or not target:
return
def grep_search(target: str = None, query: str = None):
if not target or not query: return
# NOTE: -n = provide line numbers, -R = Search recursive in given target
# -i = insensitive, -F = don't do regex parsing. (Treat as raw string)
command = ["grep", "-n", "-R", "-i", "-F", query, target]
proc = subprocess.Popen(command, stdout=subprocess.PIPE, encoding="utf-8")
proc = subprocess.Popen(command, stdout = subprocess.PIPE, encoding = "utf-8")
raw_data = proc.communicate()[0].strip()
proc_data = raw_data.split("\n") # NOTE: Will return data AFTER completion (if any)
collection = {}
@@ -85,17 +90,23 @@ def grep_search(target=None, query=None):
except Exception as e:
traceback.print_exc()
proc.terminate()
data = f"GREP|{ts}|{json.dumps(collection, separators=(',', ':'), indent=4)}"
send_ipc_message(data)
collection = {}
def search(args):
path = args.dir
if (path[0] == "'" and path[-1] == "'") or \
path[0] == '"' and path[-1] == '"':
path = path[1:-1]
if args.type == "file_search":
file_search(args.dir, args.query.lower())
file_search(path, args.query.lower())
if args.type == "grep_search":
grep_search(args.dir, args.query.encode("utf-8"))
grep_search(path, args.query.encode("utf-8"))
if __name__ == "__main__":