Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview

Artifact ID: 5ab353fb5e6acd270e6841b9d8fbf7e33b3ff9f8e9ffd2861df9fbcf892cab14
Page Name:Developing trash-d
Date: 2022-06-16 14:04:34
Original User: steven
Mimetype:text/x-markdown
Parent: 4f259b5c1f694b5c6333bcbbdd46262f719fea4324a802fff50d0cfad7cfb78e (diff)
Next 34ee0e96ac63fabb45f9b6341c19da51499159a5fa019464e9c9038eafaae204
Content

Developing trash-d 2022-06-13

This is a rewrite and update of a talk I gave at DoomConf August 2021 about trash-d, a "near drop-in replacement for rm that uses the trash bin". (There is a very scuffed recording of half the talk here)

The D Programming Language

trash-d is written in the D programming language, which was first released in 2001 as an alternative to C++. Since then it has been continually updated, but only mildly popular at best.

Some notable features include...

  • A garbage collector
  • An actual string type
  • Generics and templating
  • Good support for dynamic arrays and hashmaps
  • Contract programming
  • Easy C and C++ interop
  • Lambdas and higher order functions
  • Built in unit testing
  • Uniform Function Call Syntax

And a whole bunch of other things I forgot to mention here. In particular I am a big fan of UFCS and think more languages should use it.

D also has a fairly robust standard library with things like: ranges and iterators, many utility functions, fibers and continuations, SQLite bindings, and a getopt implementation.

All this probably makes D sound pretty nice but there are some weird bits. Unicode support is a bit all over, with different types such as char and dchar (string seems to mostly just do the right thing, so that's nice). There are both Classes and Structs with a lot of feature overlap. And in general it's a big standardized language with a lot of features, and even multiple different compilers that you sometimes have to worry about.

All that said the worst part about D is that the ecosystem is honestly quite dead. It still has a small and loyal following, but outside of that there's really not much going on. Which is a real shame, I think if the ecosystem was bigger and more vibrant I would be using D for a lot more projects. Though the renewed interest in systems programming over the last few years has led to some more people (like me) discovering D, so there's some hope...

In general if you're looking for something nicer than C++, less complicated than Rust, and more featureful than Go, then maybe give D a try. Or if you just wanna try out a cool language with some awesome but less common features.

Beginnings of trash-d

I had sort of been annoyed for a while about the destructive nature of the standard rm command. You get no second chances, no warnings that you're about to blow away something important. Yes I know about -i, but I am lazy and forgetful. So I had thought about making a simple tool to use the trash bin instead.

Around the same time that I was first looking into D a friend of mine accidentally rm -rf'd his music folder and deleted his entire collection. This was the impetus that I required to start this project.

At first I started out writing it in Bash, before quickly giving up on that because Bash is a horrible language to actually write programs in and switching to D. So far D has been a great choice, mostly because trash-d is simple enough that I don't require anything other than the standard library. But also because of the somewhat renewed interest in the language there now existed good D compilers for most UNIX-like systems.

So I set off on what I thought would be a simple tool...

The Long Road

Turns out that implementing trash-d has been a much longer process than I expected. There have been a lot more edges and corners than I anticipated. Well, I say that but it's been spread out over many months and now 17 "versions". A number of versions are missing as I would bump, find a bug, bump again, and then finally tag a release (they're all in the commit history though).

The entire point of the project was to emulate the semantics of GNU rm as much as possible, to the point that you could add alias rm=trash to your config and not notice any difference. This was ultimately very tricky, as rm has a lot of subtle implicit behavior, and the interactions between its flags can be opaque. But I think we've gotten pretty close! trash-d now mimics all of rm's flags and most of its error messages and semantics. For the majority of users this should be more than enough.

But along the way I've gotten a lot more experience with D, had some outside contributors make PRs, garnered 80 stars at the time of writing, and made something that I find really useful. I've even had other people message me to say that trash-d saved their bacon!

Competitors

Now I will be the first to admit that trash-d is not an original idea. There are already quite a few CLI trash tools on GitHub, and honestly I'm surprised there's no distro that's made their own or shipped with one by default.

But I think trash-d has some important advantages over all the other versions that I've seen.

  1. It's compatible with rm. None of the others seem to make this a priority, and some of them even make the explicit point that they're not compatible. I think that's a real shame, to me the point of a tool like this is to save me from the mistakes I would make with rm.
  2. Most of the alternatives are written in Python or Bash. Nothing wrong with this per-say, but I think it's another missed opportunity. Not every system has Python or Bash (before you say "every system has Bash" I remind you that the BSDs exist). trash-d may require a D compiler, but it can be easily compiled to a single static executable that can then just be copied onto other systems.

Conclusions and Help

All this is to say that I have thoroughly enjoyed working on trash-d and I hope to continue doing so and get it to a really solid state where lots of people can use it without issue.

And I need your help! Tell your friends, package it for your distro, open issues or even make PRs!