in Projects, Thoughts

Automatic Rainbows in your Terminal with fuse-colors

fuse-colors is a perl script that allows you to wrap every command with arbitrary bash!  You can check it out here:

https://github.com/Stantheman/fuse-colors

This post explains how fuse-colors works, and the upcoming post details some of the more fun problems I encountered.

FUSE lets you pretend to be a filesystem. The goal with fuse-colors is to run whatever the user asked for, but also automatically append “| lolcat” to the string. It should be flexible, transparent, and it shouldn’t break. fuse-colors achieves each of these.

How it works

Example of fuse-colors running

Example of fuse-colors running

  1. Get the user’s current PATH
  2. Make a list of binaries that use ncurses so they dont get lolcatted
  3. Take an optional format string for commands, defaulting to “__command__ | lolcat”
  4. Start the filesystem

So as soon as a user runs “./fuse-colors /tmp/fuse”, they have a custom filesystem mounted on /tmp/fuse. From here they can set their PATH equal to the mountpoint they just made (/tmp/fuse). When they run  “ls”, it’ll automatically turn into “ls | lolcat”. The shell will look for a file named “ls” in each directory in the user’s $PATH. Since the fuse-colors mountpoint is the first item, it checks there first.

The fuse-colors filesystem lies about any file it is asked about. If you try to read any file under the mount, you’ll always get a response, even if it doesn’t exist in “ls” output. There’s three types of output for fuse-colors to choose from:

  1. A perl script that runs the requested command with any arguments passed (things that use ncurses like vim)
  2. A perl script that runs the requested binary with ” | lolcat” appended.
  3. If you specifically ask for a file named “refresh-ncurses-cache”, you’ll force an update of the list of ncurses binaries.

So your shell tries to read /tmp/fuse/ls, gets a legitimate executable script back, and runs it. In this case, the executable is a perl script that runs exactly what you asked for and appends “| lolcat” before executing it.

The next post goes into more details about some of the hurdles I encountered during development. That’s it!

Write a Comment

Comment