├── crap.nimble ├── LICENSE ├── src └── crap.nim └── readme.md /crap.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "0.2.3" 4 | author = "Anirudh" 5 | description = "`rm` files without fear" 6 | license = "MIT" 7 | srcDir = "src" 8 | installExt = @["nim"] 9 | bin = @["crap"] 10 | 11 | 12 | # Dependencies 13 | 14 | requires "nim >= 0.18.0" 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Anirudh Oppiliappan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/crap.nim: -------------------------------------------------------------------------------- 1 | import 2 | os, 3 | strformat, 4 | strutils, 5 | times 6 | 7 | proc crap*(path: string) = 8 | var fullPath = expandFilename(path) 9 | var fname = extractFilename(fullPath) 10 | 11 | let trashHome = if existsEnv("XDG_DATA_HOME"): 12 | getEnv("XDG_DATA_HOME") / "Trash" 13 | else: 14 | getHomeDir() / ".local/share/Trash" 15 | 16 | if existsFile(trashHome / "files" / fname) or existsDir(trashHome / "files" / fname): 17 | var i = 2 18 | while true: 19 | if not (existsFile(trashHome / "files" / fname & '.' & $i) or existsDir(trashHome / "files" / fname & '.' & $i) ): 20 | fname = fname & '.' & $i 21 | moveFile(fullPath, trashHome / "files" / fname) 22 | break 23 | i = i+1 24 | else: 25 | moveFile(fullPath, trashHome / "files" / fname) 26 | 27 | let 28 | t = getTime() 29 | formattedTime = t.format("yyyy-MM-dd'T'HH:MM:ss") 30 | trashInfo = fmt"""[Trash Info] 31 | Path={fullPath} 32 | DeletionDate={formattedTime}""" 33 | 34 | writeFile(trashHome / "info" / fmt"{fname}.trashinfo", trashInfo) 35 | 36 | when isMainModule: 37 | var del = "" 38 | if paramCount() == 0: 39 | echo("error: specify at least one file") 40 | quit(1) 41 | try: 42 | for f in countUp(1, paramCount()): 43 | del = paramStr(f).strip() 44 | crap(del) 45 | except OSError: 46 | echo("error: no such file or directory: " & del) 47 | quit(1) 48 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 |

2 | 3 |

4 | 5 | > `rm` files without fear 6 | 7 | ### Why? 8 | Ever `rm`ed a file and instantly regretted it? Fear not, for `crap` is here. 9 | 10 | ### How? 11 | `crap` follows the FreeDesktop.org Trash spec. So instead of perma-deleting your file, `crap` moves it to the `$XDG_DATA_HOME/Trash` folder. If this environment variable isn't set, it defaults to `~/.local/share/Trash`. 12 | 13 | And obviously, ***this works only on Linux***. 14 | 15 | ## Installation 16 | ```console 17 | $ nimble install crap 18 | ``` 19 | 20 | or download the binary from the [Releases](https://github.com/icyphox/crap/releases/) page. 21 | 22 | ### Tip 23 | Alias `crap` to `rm` in your `.bashrc`/`.zshrc`. But that's kinda the point. 24 | 25 | ## Usage 26 | ```console 27 | $ crap foo.png 28 | 29 | $ crap *.png 30 | 31 | $ crap a.pdf b.jpg c.mp4 32 | 33 | ``` 34 | Aaannddd it'll show up in your file manager's Trash. 35 | 36 | ![trash](https://x.icyphox.sh/H6iNv.png) 37 | 38 | ## API 39 | This package exposes a single proc: `crap(path: var string)` 40 | 41 | ```nim 42 | import crap 43 | 44 | crap("~/Pictures/*") 45 | 46 | ``` 47 | 48 | ## TODO 49 | - [x] support multiple files 50 | - [x] trashing dirs 51 | - [ ] support for other OSs (idk maybe) 52 | 53 | ## Prior art 54 | - [sindresorhus/trash-cli](https://github.com/sindresorhus/trash-cli) 55 | 56 | ## Contributing 57 | Bad code? New feature in mind? Open an issue. Better still, learn [Nim](https://nim-lang.org/documentation.html) and shoot a PR :sparkles: 58 | 59 | ## License 60 | MIT © [Anirudh Oppiliappan](https://icyphox.sh) 61 | --------------------------------------------------------------------------------