Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add XDG config support #77

Merged
merged 2 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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.
Expand Down
12 changes: 11 additions & 1 deletion lib/amazing_print/inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down