appdirs.nim 2.8 KB

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