Skip to content

Commit

Permalink
Merge branch 'xdg' of https://github.com/mcarton/thefuck into mcarton…
Browse files Browse the repository at this point in the history
…-xdg
  • Loading branch information
nvbn committed Oct 21, 2015
2 parents f5b0e96 + 8f25c95 commit bbfedb8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ Bundled, but not enabled by default:
## Creating your own rules

For adding your own rule you should create `your-rule-name.py`
in `~/.thefuck/rules`. The rule should contain two functions:
in `~/.config/thefuck/rules`. The rule should contain two functions:

```python
match(command: Command) -> bool
Expand All @@ -242,7 +242,7 @@ and optional `enabled_by_default`, `requires_output` and `priority` variables.
`Command` has three attributes: `script`, `stdout` and `stderr`.

*Rules api changed in 3.0:* For accessing settings in rule you need to import it with `from thefuck.conf import settings`.
`settings` is a special object filled with `~/.thefuck/settings.py` and values from env ([see more below](#settings)).
`settings` is a special object filled with `~/.config/thefuck/settings.py` and values from env ([see more below](#settings)).

Simple example of the rule for running script with `sudo`:

Expand Down Expand Up @@ -272,7 +272,7 @@ requires_output = True

## Settings

The Fuck has a few settings parameters which can be changed in `~/.thefuck/settings.py`:
The Fuck has a few settings parameters which can be changed in `$XDG_CONFIG_HOME/thefuck/settings.py` (`$XDG_CONFIG_HOME` defaults to `~/.config`):

* `rules` – list of enabled rules, by default `thefuck.conf.DEFAULT_RULES`;
* `exclude_rules` – list of disabled rules, by default `[]`;
Expand Down
10 changes: 9 additions & 1 deletion thefuck/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
'THEFUCK_PRIORITY': 'priority',
'THEFUCK_DEBUG': 'debug'}

SETTINGS_HEADER = u"""# ~/.thefuck/settings.py: The Fuck settings file
SETTINGS_HEADER = u"""# The Fuck settings file
#
# The rules are defined as in the example bellow:
#
Expand Down Expand Up @@ -73,7 +73,15 @@ def _init_settings_file(self):

def _setup_user_dir(self):
"""Returns user config dir, create it when it doesn't exist."""

# for backward compatibility, use `~/.thefuck` if it exists
user_dir = Path(os.path.expanduser('~/.thefuck'))

if not user_dir.is_dir():
default_xdg_config_dir = os.path.expanduser("~/.config")
xdg_config_dir = os.getenv("XDG_CONFIG_HOME", default_xdg_config_dir)
user_dir = Path(os.path.join(xdg_config_dir, 'thefuck'))

rules_dir = user_dir.joinpath('rules')
if not rules_dir.is_dir():
rules_dir.mkdir(parents=True)
Expand Down
13 changes: 12 additions & 1 deletion thefuck/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,18 @@ def _cache(fn, *args, **kwargs):
if cache.disabled:
return fn(*args, **kwargs)

cache_path = settings.user_dir.joinpath('.thefuck-cache').as_posix()
default_xdg_cache_dir = os.path.expanduser("~/.cache")
cache_dir = os.getenv("XDG_CACHE_HOME", default_xdg_cache_dir)
cache_path = Path(cache_dir).joinpath('thefuck').as_posix()

# Ensure the cache_path exists, Python 2 does not have the exist_ok
# parameter
try:
os.makedirs(cache_dir)
except OSError:
if not os.path.isdir(cache_dir):
raise

# A bit obscure, but simplest way to generate unique key for
# functions and methods in python 2 and 3:
key = '{}.{}'.format(fn.__module__, repr(fn).split('at')[0])
Expand Down

0 comments on commit bbfedb8

Please sign in to comment.