1
0
mirror of https://github.com/nvbn/thefuck.git synced 2025-03-14 06:38:32 +00:00

Merge 80e1e9efc911fd5ae42bd1b75502f732b859b7cd into c7e7e1d884d3bb241ea6448f72a989434c2a35ec

This commit is contained in:
Sven 2024-04-11 08:33:12 -04:00 committed by GitHub
commit db27b7bf4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 18 deletions

View File

@ -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()))

View File

@ -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))