Table of Contents

Introduction

This is a simple experiment that I'm trying out for myself.

I don't know about you, but I can't remember everything about anything. I once called a friend of five years by the wrong name (Whoops! Very sorry, Jessica!) and didn't realize it until three days later. So how am I supposed to remember everything about what's in my filesystem? todo.txt might be obvious, but what's in ~/code/stuff/? (Turns out it was a rough experiment with bit stuffing.)

One thing I could do is keep a bunch of readmes in each directory, which explains something about the directory, or its files. That's kind of ugly, and I'd have to remember to look at it.

Another thing that can be done is to have the directory description shown when listing the files. If the file doesn't exist, simply don't show it.

Code

To do this, I created a Perl script called newls:

#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
 
my $pwd = cwd();
 
# Check to see if any of the arguments exist as directories.
my @directories = grep { -d $_ || -d "$pwd/$_" } @ARGV;
 
# Use anything beginning with a - as an argument for ls
my @arguments = grep { /^-/ } @ARGV;
 
my $ls_arguments = join(' ', @arguments);
 
if (!@directories)
{
    # If no directories were specified, and all of the other arguments are valid, work on the current directory.
    if (@arguments == @ARGV)
    {
        push @directories, ".";
    }
    else
    {
        # Enumerate the invalid directories
        foreach(grep { !($_ ~~ @directories || $_ ~~ @arguments) } @ARGV)
        {
            print "ls: cannot access $_: No such file or directory\n";
        }
    }
}
 
foreach(@directories)
{
    # Correct any spaces in filenames.
    my $fixed_dir = $_;
    $fixed_dir =~ s/ /\\ /;
 
    # Call ls on each directory separately so that we can output the .ls file beside it.
    if (system("ls $fixed_dir $ls_arguments") == 0)
    {
        # Open the .ls file, if it exists. Otherwise, go on to the next directory.
        open(my $file, '<', "$_/.ls") or next;
        print "---\n";
        print <$file>;
        print "---\n";
        close($file);
    }
}

And another called annotate:

#!/usr/bin/perl
use strict;
use warnings;
 
open(my $file, '>>', ".ls") or die("Unable to open .ls");
 
if(@ARGV)
{
    print $file "@ARGV\n";
}
else
{
    my @lines = <>;
    print $file @lines;
}
 
close($file);

How To Use

I made these executable, put them on my PATH, (I like to use ~/.bin/) and set an alias for ls in my ~/.bashrc file:

alias ls='newls --color=auto'

Now, if you want to make an annotation for the current directory, it can either read from stdin, or the command-line:

ryan@caffeine:~$ annotate
This is my home directory. Whee!

or

ryan@caffeine:~$ annotate This is my home directory. Whee!

(Note, if you're typing at the command-line, you must escape any quotation marks that you want to appear, and enclose many special characters in non-escaped quotes.)

To see your annotations:

ryan@caffeine:~$ ls
code  Desktop  Documents  ds  irclogs  Music  Pictures  qnx   tmp  todo.txt Videos www
---
This is my home directory. Whee!
---
ryan@caffeine:~$

If you need to edit/delete/backup/whatever the annotation file, it's called .ls. The . will hide the file, unless you do something like ls -a.

 
directory_annotations.txt · Last modified: 2009/11/08 03:47 by Ryan Fox
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki