Go program stores directory paths
Database Voodoo
A new directory is added to the database with dirInsert()
as of line 85. If the SQLite database does not yet exist (Listing 1 checks for the existence of the database file), line 76 builds a new one and creates a fresh dirs
table in it with the SQL create
command. The table's two columns, dir
and date
, are of the text
type. The fact that the directory path is a text string is not surprising, but SQLite also saves dates as text and compares them in string mode, which works because the timestamps are in the format YYYY-MM-DD HH::MM::SS, so later times also alphanumerically follow earlier ones.
We do not want SQLite to generate a new table row for existing paths, but simply refresh the existing entry's timestamp. This could be solved in SQL by a separate select query, followed by case logic, but in the SQLite dialect this is done more elegantly with the special REPLACE
function (line 86).
This works in a similar way to SQL's standard UPDATE
function, except it creates missing entries – but only if a unique index is defined for the corresponding table column. This explains why line 80 adds an index to the dir
column after the table definition, causing replace
in line 86 to create new entries and refresh old ones as needed.
The dirList()
function retrieves existing database entries. The select statement in line 52 sorts them in descending order according to insertion date (i.e., entries created shortly beforehand appear at the top of the selection list). The LIMIT 10
statement fetches a maximum of 10, but since the terminal list display scrolls down on request, you could just as easily leave this out.
The for
loop starting in line 59 uses rows.Next()
and rows.Scan()
to fetch the search query's next hits; the append
statement in line 63 appends them to the items
array slice. If the database is still unpopulated, line 67 inserts the user's home directory; otherwise, the displayed selection list would be empty and leave the user confused.
If two or more hits are found, line 69 steals the first one and removes it from the list, because this is the entry of the last directory visited (i.e., the current directory to which the user will probably not want to jump). The dbPath()
function starting in line 95 specifies the path to the SQLite file in which the data is stored, this is hard-coded in Listing 1 as ~/.cdbm.db
in the user's home directory.
Quite Wordy
It's worth noting that a Go program, which actually doesn't contain so much logic, needs a large number of lines. Go's relentlessly required, explicit error handling of each return value is partly to blame. An exception handler would be more compact for such a simple utility. The panicOnErr()
function starting in line 103, which checks an error value passed to it and immediately aborts the program by calling panic()
, helps to save lines. It is rumored that the next Go version will accommodate program authors with more compact mechanisms.
More Convenience
For hobbyists, the fun only starts here. For instance, the script could be extended to include a search function that only offers paths that match a search term entered at the command line. For example, the user could enter c usr
and only see paths containing the string usr
. And since all the user data are stored in an SQLite database whose schema can be easily extended, it makes sense to assign a counter to each stored path, which cdbm
increments by one each time a directory is visited.
This could shift frequently visited paths higher up in the selection list based on an algorithm, because users would not want to scroll a long way to access frequently used paths. And, who knows, maybe it's worth adding a dash of artificial intelligence? A self-learning directory butler would be attractive to my younger coworkers.
Infos
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/228 @IE
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
![Learn More](https://www.linux-magazine.com/var/linux_magazin/storage/images/media/linux-magazine-eng-us/images/misc/learn-more/834592-1-eng-US/Learn-More_medium.png)
News
-
NVIDIA Released Driver for Upcoming NVIDIA 560 GPU for Linux
Not only has NVIDIA released the driver for its upcoming CPU series, it's the first release that defaults to using open-source GPU kernel modules.
-
OpenMandriva Lx 24.07 Released
If you’re into rolling release Linux distributions, OpenMandriva ROME has a new snapshot with a new kernel.
-
Kernel 6.10 Available for General Usage
Linus Torvalds has released the 6.10 kernel and it includes significant performance increases for Intel Core hybrid systems and more.
-
TUXEDO Computers Releases InfinityBook Pro 14 Gen9 Laptop
Sporting either AMD or Intel CPUs, the TUXEDO InfinityBook Pro 14 is an extremely compact, lightweight, sturdy powerhouse.
-
Google Extends Support for Linux Kernels Used for Android
Because the LTS Linux kernel releases are so important to Android, Google has decided to extend the support period beyond that offered by the kernel development team.
-
Linux Mint 22 Stable Delayed
If you're anxious about getting your hands on the stable release of Linux Mint 22, it looks as if you're going to have to wait a bit longer.
-
Nitrux 3.5.1 Available for Install
The latest version of the immutable, systemd-free distribution includes an updated kernel and NVIDIA driver.
-
Debian 12.6 Released with Plenty of Bug Fixes and Updates
The sixth update to Debian "Bookworm" is all about security mitigations and making adjustments for some "serious problems."
-
Canonical Offers 12-Year LTS for Open Source Docker Images
Canonical is expanding its LTS offering to reach beyond the DEB packages with a new distro-less Docker image.
-
Plasma Desktop 6.1 Released with Several Enhancements
If you're a fan of Plasma Desktop, you should be excited about this new point release.