Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some simplifying changes #3

Merged
merged 1 commit into from Nov 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 17 additions & 19 deletions src/crap.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,35 @@ import
strutils,
times

proc crap*(path: var string) =
var trashHome: string
proc crap*(path: string) =
let fullPath = expandFilename(path)
path = extractFilename(fullPath)
let fname = extractFilename(fullPath)

if existsEnv("XDG_DATA_HOME"):
trashHome = joinPath(getEnv("XDG_DATA_HOME"), "Trash")
let trashHome = if existsEnv("XDG_DATA_HOME"):
getEnv("XDG_DATA_HOME") / "Trash"
else:
trashHome = joinPath(getHomeDir(), ".local/share/Trash")
getHomeDir() / ".local/share/Trash"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be also changed to

getHomeDir() / ".local" / "share" / "Trash"

btw, but I don't think that it's really needed :D


moveFile(fullPath, joinPath(trashHome, "files", path))
moveFile(fullPath, trashHome / "files" / fname)

let
t = getTime()
formattedTime = t.format("yyyy-MM-dd") & "T" & t.format("HH:MM:ss")
formattedTime = t.format("yyyy-MM-dd'T'HH:MM:ss")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is documented in official docs for the times module if you're wondering:

Other strings can be inserted by putting them in ''. 

trashInfo = fmt"""[Trash Info]
Path={fullPath}
DeletionDate={formattedTime}"""

writeFile(joinPath(trashHome, "info", fmt"{path}.trashinfo"), trashInfo)
writeFile(trashHome / "info" / fmt"{fname}.trashinfo", trashInfo)

when isMainModule:
var del: string = ""
var del = ""
if paramCount() == 0:
echo("error: specify at least one file")
quit(1)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also changed quit(0) to quit(1) because that's more logical - if an error happens there shouldn't be a successfull exit code.

try:
if paramCount() == 0:
echo("error: specify at least one file")
quit(0)
else:
for f in countUp(1, paramCount()):
del = paramStr(f).strip()
crap(del)
for f in countUp(1, paramCount()):
del = paramStr(f).strip()
crap(del)
except OSError:
echo("error: no such file or directory")
quit(0)
echo("error: no such file or directory: " & del)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also added path to the file to the error which may help the user in case when moving fails

quit(1)