appdirs.nim 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ## This module implements helpers for determining special directories used by apps.
  2. ## .. importdoc:: paths.nim
  3. from std/private/osappdirs import nil
  4. import std/paths
  5. import std/envvars
  6. proc getHomeDir*(): Path {.inline, tags: [ReadEnvEffect, ReadIOEffect].} =
  7. ## Returns the home directory of the current user.
  8. ##
  9. ## This proc is wrapped by the `expandTilde proc`_
  10. ## for the convenience of processing paths coming from user configuration files.
  11. ##
  12. ## See also:
  13. ## * `getConfigDir proc`_
  14. ## * `getTempDir proc`_
  15. result = Path(osappdirs.getHomeDir())
  16. proc getConfigDir*(): Path {.inline, tags: [ReadEnvEffect, ReadIOEffect].} =
  17. ## Returns the config directory of the current user for applications.
  18. ##
  19. ## On non-Windows OSs, this proc conforms to the XDG Base Directory
  20. ## spec. Thus, this proc returns the value of the `XDG_CONFIG_HOME` environment
  21. ## variable if it is set, otherwise it returns the default configuration
  22. ## directory ("~/.config/").
  23. ##
  24. ## An OS-dependent trailing slash is always present at the end of the
  25. ## returned string: `\\` on Windows and `/` on all other OSs.
  26. ##
  27. ## See also:
  28. ## * `getHomeDir proc`_
  29. ## * `getTempDir proc`_
  30. result = Path(osappdirs.getConfigDir())
  31. proc getCacheDir*(): Path {.inline.} =
  32. ## Returns the cache directory of the current user for applications.
  33. ##
  34. ## This makes use of the following environment variables:
  35. ##
  36. ## * On Windows: `getEnv("LOCALAPPDATA")`
  37. ##
  38. ## * On macOS: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
  39. ##
  40. ## * On other platforms: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
  41. ##
  42. ## **See also:**
  43. ## * `getHomeDir proc`_
  44. ## * `getTempDir proc`_
  45. ## * `getConfigDir proc`_
  46. # follows https://crates.io/crates/platform-dirs
  47. result = Path(osappdirs.getCacheDir())
  48. proc getCacheDir*(app: Path): Path {.inline.} =
  49. ## Returns the cache directory for an application `app`.
  50. ##
  51. ## * On Windows, this uses: `getCacheDir() / app / "cache"`
  52. ## * On other platforms, this uses: `getCacheDir() / app`
  53. result = Path(osappdirs.getCacheDir(app.string))
  54. proc getTempDir*(): Path {.inline, tags: [ReadEnvEffect, ReadIOEffect].} =
  55. ## Returns the temporary directory of the current user for applications to
  56. ## save temporary files in.
  57. ##
  58. ## On Windows, it calls [GetTempPath](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppathw).
  59. ## On Posix based platforms, it will check `TMPDIR`, `TEMP`, `TMP` and `TEMPDIR` environment variables in order.
  60. ## On all platforms, `/tmp` will be returned if the procs fails.
  61. ##
  62. ## You can override this implementation
  63. ## by adding `-d:tempDir=mytempname` to your compiler invocation.
  64. ##
  65. ## .. Note:: This proc does not check whether the returned path exists.
  66. ##
  67. ## See also:
  68. ## * `getHomeDir proc`_
  69. ## * `getConfigDir proc`_
  70. result = Path(osappdirs.getTempDir())