Handle XDG Directories
Handling files and directories correctly in a command-line or TUI application ensures that the application fits seamlessly into a user’s workflow and adheres to established conventions. One of the key conventions on Linux-based systems is the XDG Base Directory Specification.
Why the XDG Base Directory Specification?
The XDG Base Directory Specification is a set of standards that define where user files should reside, ensuring a cleaner home directory and a more organized storage convention. By adhering to this standard, your application will store files in the expected directories, making it more predictable and user-friendly.
directories-rs for Path Resolution
directories-rs library offers a Rust-friendly interface to locate common directories (like
config and data directories) based on established conventions, including the XDG Base Directory
ProjectDirsstruct to retrieve paths based on your project’s domain and project name and create helper functions for getting the
Allow users to specify custom locations using environment variables. This flexibility can be crucial for users with unique directory structures or for testing.
A good practice is to notify the user about the location of the configuration and data directories. An example from the template is to print out these locations when the user invokes the
--versioncommand-line argument. See the section on Command line argument parsing
Here’s an example
get_config_dir() functions for your reference:
You will want to replace
kdheepak with your user name or company name (or any unique name for that
ratatui-app with the name of your CLI.
I own https://kdheepak.com so I tend to use
com.kdheepak.ratatui-app for my project directories.
That way it is unlikely that any other program will mess with the configuration files for the app I
plan on distributing.