diff --git a/thefuck/shells/fish.py b/thefuck/shells/fish.py index eb7e9153..2b8be6c9 100644 --- a/thefuck/shells/fish.py +++ b/thefuck/shells/fish.py @@ -6,7 +6,7 @@ import six from .. import logs from ..conf import settings from ..const import ARGUMENT_PLACEHOLDER -from ..utils import DEVNULL, cache +from ..utils import DEVNULL, cache, data_dir from .generic import Generic @@ -83,7 +83,7 @@ class Fish(Generic): return command_script def _get_history_file_name(self): - return os.path.expanduser('~/.config/fish/fish_history') + return data_dir().joinpath("fish").joinpath("fish_history") def _get_history_line(self, command_script): return u'- cmd: {}\n when: {}\n'.format(command_script, int(time())) diff --git a/thefuck/utils.py b/thefuck/utils.py index 466e4ba5..9c01617f 100644 --- a/thefuck/utils.py +++ b/thefuck/utils.py @@ -196,6 +196,40 @@ def for_app(*app_names, **kwargs): return decorator(_for_app) +def _xdg_dir(env, default): + """ + Returns the XDG user directory by first checking the given environment variable + and then resulting to the default filepath. + + # Note + + The directory gets created if it doesn't exist. + """ + + default_xdg_dir = os.path.expanduser(default) + dir = os.getenv(env, default_xdg_dir) + + # Ensure the cache_path exists, Python 2 does not have the exist_ok + # parameter + try: + os.makedirs(dir) + except OSError: + if not os.path.isdir(dir): + raise + + return dir + + +def cache_dir(): + """Returns the user's XDG cache directory.""" + return Path(_xdg_dir("XDG_CACHE_HOME", "~/.cache")) + + +def data_dir(): + """Returns the user's XDG data directory.""" + return Path(_xdg_dir("XDG_DATA_HOME", "~/.local/share")) + + class Cache(object): """Lazy read cache and save changes at exit.""" @@ -210,8 +244,7 @@ class Cache(object): self._db = {} def _setup_db(self): - cache_dir = self._get_cache_dir() - cache_path = Path(cache_dir).joinpath('thefuck').as_posix() + cache_path = cache_dir().joinpath('thefuck').as_posix() try: self._db = shelve.open(cache_path) @@ -223,20 +256,6 @@ class Cache(object): atexit.register(self._db.close) - def _get_cache_dir(self): - default_xdg_cache_dir = os.path.expanduser("~/.cache") - cache_dir = os.getenv("XDG_CACHE_HOME", default_xdg_cache_dir) - - # 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 - - return cache_dir - def _get_mtime(self, path): try: return str(os.path.getmtime(path))