Skip to content

Commit

Permalink
Fix FindProc
Browse files Browse the repository at this point in the history
  • Loading branch information
sneekyfoxx committed Jan 7, 2024
1 parent f8e7585 commit 6ce1c0b
Showing 1 changed file with 69 additions and 70 deletions.
139 changes: 69 additions & 70 deletions src/FindProc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import std/[os, strutils, strformat, times, terminal]
proc sigintHandler() {.noconv.} =
stdout.writeLine("\u001b[2K")
stdout.flushFile
showCursor()
quit(0)

const
Expand All @@ -18,8 +19,10 @@ proc Find*(name: string = "", use: string = "/", mode: string = "strict", limit:
dirsFound: seq[string]
filesFound: seq[string]
file: File
startTime: float
endTime: string
searchStartTime: float
searchEndTime: string
writeStartTime: float
writeEndTime: string
info: PathComponent
entryDirName: string
entryFileName: string
Expand All @@ -30,29 +33,26 @@ proc Find*(name: string = "", use: string = "/", mode: string = "strict", limit:
counter: int
dot: string = "."

if name != "" and name.startsWith("/") or name.endsWith("/"):
if name != "" and (name.startsWith("/") or name.endsWith("/")):
stderr.writeLine("\n{red}ERROR{reset}: {yellow}found leading or trailing{reset} '{red}/{reset}'\n".fmt)
stderr.flushFile
stderr.flushFile()
quit(1)

if Write.len > 0:
if fileExists(expandTilde("./"&Write)):
file = open(expandTilde("./"&Write), fmAppend)

else:
file = open(expandTilde("./"&Write), fmWrite)

elif name != "" and (name.startsWith("\\") or name.endsWith("\\")):
stderr.writeLine("\n{red}ERROR{reset}: {yellow}found leading or trailing{reset} '{red}\\{reset}'\n".fmt)
stderr.flushFile()
quit(0)

stdout.hideCursor()
startTime = cpuTime()
searchStartTime = cpuTime()

for entry in walkDirRec(expandTilde(use), yieldFilter={pcFile, pcDir, pcLinkToFile, pcLinkToDir}, relative = false, checkdir = false):
stdout.writeLine("{yellow}Searching for{reset} '{blue}{name}{reset}': {blue}{dot}{reset}".fmt)
stdout.flushFile
cursorUp(1)
dot &= "."

if dot.len == 21:
if dot.len == 20:
dot = "."
eraseLine()

Expand All @@ -68,16 +68,9 @@ proc Find*(name: string = "", use: string = "/", mode: string = "strict", limit:
if info == pcDir:
if (mode == "strict" and entryDirName.endsWith(strictDirName)) or (mode == "passive" and entryDirName.contains(passiveDirName)):
counter.inc()
dirsFound.add(entry)
if Write.len > 0:
var tempname: string = entry & "/\n"
stdout.writeLine("\n{yellow}Writing{reset}{red}!{reset}".fmt)
file.write(tempname)
cursorUp(1)
eraseLine()
cursorUp(1)

else:
dirsFound.add(entry & "/")

if Write.len == 0:
stdout.writeLine("<", blue, "Folder", reset, "> ", green, "Path", reset, ": ", blue, entry & "/", reset, "\n")
stdout.flushFile

Expand All @@ -87,16 +80,9 @@ proc Find*(name: string = "", use: string = "/", mode: string = "strict", limit:
elif info == pcLinkToDir:
if (mode == "strict" and entryDirName.endsWith(strictDirName)) or (mode == "passive" and entryDirName.contains(passiveDirName)):
counter.inc()
dirsFound.add(entry)
if Write.len > 0:
var tempname: string = entry & "/\n"
stdout.writeLine("\n{yellow}Writing{reset}{red}!{reset}".fmt)
file.write(tempname)
cursorUp(1)
eraseLine()
cursorUp(1)

else:
dirsFound.add(entry & "/")

if Write.len == 0:
stdout.writeLine("<", blue, "Symlink->Folder", reset, "> ", green, "Path", reset, ": ", blue, entry & "/", reset, "\n")
stdout.flushFile

Expand All @@ -107,15 +93,8 @@ proc Find*(name: string = "", use: string = "/", mode: string = "strict", limit:
if (mode == "strict" and entryFileName.endsWith(strictFileName)) or (mode == "passive" and entryFileName.contains(passiveFileName)):
counter.inc()
filesFound.add(entry)
if Write.len > 0:
var tempname: string = entry & "\n"
stdout.writeLine("\n{yellow}Writing{reset}{red}!{reset}".fmt)
file.write(tempname)
cursorUp(1)
eraseLine()
cursorUp(1)

else:

if Write.len == 0:
stdout.writeLine("<", blue, "File", reset, "> ", green, "Path", reset, ": ", blue, entry, reset, "\n")
stdout.flushFile

Expand All @@ -126,15 +105,8 @@ proc Find*(name: string = "", use: string = "/", mode: string = "strict", limit:
if (mode == "strict" and entryFileName.endsWith(strictFileName)) or (mode == "passive" and entryFileName.contains(passiveFileName)):
counter.inc()
filesFound.add(entry)
if Write.len > 0:
var tempname: string = entry & "\n"
stdout.writeLine("\n{yellow}Writing{reset}{red}!{reset}".fmt)
file.write(tempname)
cursorUp(1)
eraseLine()
cursorUp(1)

else:

if Write.len == 0:
stdout.writeLine("<", blue, "Symlink->File", reset, "> ", green, "Path", reset, ": ", blue, entry, reset, "\n")
stdout.flushFile

Expand All @@ -144,37 +116,64 @@ proc Find*(name: string = "", use: string = "/", mode: string = "strict", limit:
except OSError:
continue

endTime = "{cpuTime() - startTime:.2f}".fmt
searchEndTime = "{cpuTime() - searchStartTime:.2f}".fmt
eraseLine()
stdout.styledWriteLine("{yellow}Searching{reset}: {green}Done!{reset}".fmt)
cursorUp(1)

var found: string = intToStr(dirsFound.len + filesFound.len)
if Write.len > 0:
file.close()

if Write.len == 0:
sleep(2000)
eraseLine()
stdout.writeLine("\n{green}Found{reset} {cyan}{found}{reset} paths in {green}{searchEndTime}{reset} seconds".fmt)
stdout.flushFile()
stdout.showCursor()
quit(0)

else:
sleep(1000)
if fileExists(expandTilde("./"&Write)):
file = open(expandTilde("./"&Write), fmAppend)

else:
file = open(expandTilde("./"&Write), fmAppend)

if parseInt(found) > 0:
let data: seq[string] = dirsFound & filesFound
dot = "."

writeStartTime = cpuTime()

for value in data:
var filename: string = value & "\n"
stdout.writeLine("\n{yellow}Writing{reset}{blue}{dot}{reset}".fmt)
file.write(filename)
cursorUp(1)
eraseLine()
cursorUp(1)
sleep(1000)

if dot.len == 20:
dot = "."

stdout.writeLine("\n{yellow}Writing{reset}: {green}Done!{reset}".fmt)
cursorUp(2)
sleep(2000)
eraseLine()
file.close()

elif parseInt(found) == 0:
var tempname: string = "./" & Write

if readFile(expandTilde(tempname)).len == 0:
removeFile(tempname)

stdout.writeLine("\n{green}Wrote{reset} {cyan}{found}{reset} paths to '{yellow}{Write}{reset}' in {green}{endTime}{reset} seconds".fmt)
stdout.flushFile

else:
sleep(2000)
eraseLine()
stdout.writeLine("\n{green}Found{reset} {cyan}{found}{reset} paths in {green}{endTime}{reset} seconds".fmt)
stdout.flushFile
writeEndTime = "{cpuTime() - writeStartTime:.2f}".fmt
stdout.writeLine("\n{green}Wrote{reset} {cyan}{found}{reset} paths to '{yellow}{Write}{reset}' in {green}{writeEndTime}{reset} seconds".fmt)
stdout.flushFile()
showCursor()
quit(1)

stdout.showCursor()
quit(0)
else:
writeEndTime = "{cpuTime() - writeStartTime:.2f}".fmt
stdout.writeLine("\n{green}Wrote{reset} {cyan}{found}{reset} paths to '{yellow}{Write}{reset}' in {green}{writeEndTime}{reset} seconds".fmt)
stdout.flushFile()
showCursor()
quit(0)

setControlCHook(sigintHandler)

0 comments on commit 6ce1c0b

Please sign in to comment.