From f056e4a0b18ba1df032cf169f975ee8114da9c47 Mon Sep 17 00:00:00 2001 From: stefanwascoding Date: Thu, 15 Jul 2021 13:06:52 +0200 Subject: [PATCH 1/2] Add support for loading config file from $XDG_CONFIG_HOME/aprc By supporting the [XDG Base Directory specification](https://wiki.archlinux.org/title/XDG_Base_Directory#User_directories) we can keep the $HOME folder clean. If we do not find a file in the expected XDG location, we fall back to the previously used ~/.aprc This feature is loosely related to https://github.com/amazing-print/amazing_print/issues/63, which is about a larger overhaul of the config loading system. --- CHANGELOG.md | 3 +++ lib/amazing_print/inspector.rb | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef9dfad..ca6ab40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## Unreleased + - Support loading config from `$XDG_CONFIG_HOME/aprc` - #63 + ## v1.2.2 - Support Ruby 3.0 / IRB 1.2.6 - #57 - Fix FrozenError - #51 diff --git a/lib/amazing_print/inspector.rb b/lib/amazing_print/inspector.rb index f7388b5..8d6d387 100644 --- a/lib/amazing_print/inspector.rb +++ b/lib/amazing_print/inspector.rb @@ -159,13 +159,23 @@ def merge_options!(options = {}) @options.merge!(options) end + def find_dotfile + xdg_config_home = File.expand_path(ENV.fetch('XDG_CONFIG_HOME', '~/.config')) + xdg_config_path = File.join(xdg_config_home, 'aprc') # ${XDG_CONFIG_HOME}/aprc + + return xdg_config_path if File.exist?(xdg_config_path) + + # default to ~/.aprc + File.join(ENV['HOME'], '.aprc') + end + # This method needs to be mocked during testing so that it always loads # predictable values #--------------------------------------------------------------------------- def load_dotfile return if @@dotfile # Load the dotfile only once. - dotfile = File.join(ENV['HOME'], '.aprc') + dotfile = find_dotfile load dotfile if dotfile_readable?(dotfile) end From 08e9e675b60e079e4f2b3818a6fc032b71a18aa7 Mon Sep 17 00:00:00 2001 From: stefanwascoding Date: Thu, 15 Jul 2021 13:10:16 +0200 Subject: [PATCH 2/2] update readme to mention XDG_CONFIG_HOME support --- README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 731e424..badf6d0 100644 --- a/README.md +++ b/README.md @@ -325,12 +325,13 @@ red text # (it's red) ``` ### Setting Custom Defaults ### -You can set your own default options by creating ``.aprc`` file in your home -directory. Within that file assign your defaults to ``AmazingPrint.defaults``. +You can set your own default options by creating ``aprc`` file in your `$XDG_CONFIG_HOME` +directory (defaults to `~/.config` if undefined). Within that file assign your defaults +to ``AmazingPrint.defaults``. For example: ```ruby -# ~/.aprc file. +# ~/.config/aprc file. AmazingPrint.defaults = { :indent => -2, :color => { @@ -340,6 +341,8 @@ AmazingPrint.defaults = { } ``` +The previous `~/.aprc` location is still supported as fallback. + ## Versioning AmazingPrint follows the [Semantic Versioning](http://semver.org/) standard.