appdirs.nim 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 getDataDir*(): Path {.inline, tags: [ReadEnvEffect, ReadIOEffect].} =
  17. ## Returns the data 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_DATA_HOME` environment
  21. ## variable if it is set, otherwise it returns the default configuration
  22. ## directory ("~/.local/share" or "~/Library/Application Support" on macOS).
  23. ##
  24. ## See also:
  25. ## * `getHomeDir proc`_
  26. ## * `getConfigDir proc`_
  27. ## * `getTempDir proc`_
  28. ## * `expandTilde proc`_
  29. ## * `getCurrentDir proc`_
  30. ## * `setCurrentDir proc`_
  31. result = Path(osappdirs.getDataDir())
  32. proc getConfigDir*(): Path {.inline, tags: [ReadEnvEffect, ReadIOEffect].} =
  33. ## Returns the config directory of the current user for applications.
  34. ##
  35. ## On non-Windows OSs, this proc conforms to the XDG Base Directory
  36. ## spec. Thus, this proc returns the value of the `XDG_CONFIG_HOME` environment
  37. ## variable if it is set, otherwise it returns the default configuration
  38. ## directory ("~/.config/").
  39. ##
  40. ## An OS-dependent trailing slash is always present at the end of the
  41. ## returned string: `\\` on Windows and `/` on all other OSs.
  42. ##
  43. ## See also:
  44. ## * `getHomeDir proc`_
  45. ## * `getTempDir proc`_
  46. result = Path(osappdirs.getConfigDir())
  47. proc getCacheDir*(): Path {.inline.} =
  48. ## Returns the cache directory of the current user for applications.
  49. ##
  50. ## This makes use of the following environment variables:
  51. ##
  52. ## * On Windows: `getEnv("LOCALAPPDATA")`
  53. ##
  54. ## * On macOS: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
  55. ##
  56. ## * On other platforms: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
  57. ##
  58. ## **See also:**
  59. ## * `getHomeDir proc`_
  60. ## * `getTempDir proc`_
  61. ## * `getConfigDir proc`_
  62. # follows https://crates.io/crates/platform-dirs
  63. result = Path(osappdirs.getCacheDir())
  64. proc getCacheDir*(app: Path): Path {.inline.} =
  65. ## Returns the cache directory for an application `app`.
  66. ##
  67. ## * On Windows, this uses: `getCacheDir() / app / "cache"`
  68. ## * On other platforms, this uses: `getCacheDir() / app`
  69. result = Path(osappdirs.getCacheDir(app.string))
  70. proc getTempDir*(): Path {.inline, tags: [ReadEnvEffect, ReadIOEffect].} =
  71. ## Returns the temporary directory of the current user for applications to
  72. ## save temporary files in.
  73. ##
  74. ## On Windows, it calls [GetTempPath](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-gettemppathw).
  75. ## On Posix based platforms, it will check `TMPDIR`, `TEMP`, `TMP` and `TEMPDIR` environment variables in order.
  76. ## On all platforms, `/tmp` will be returned if the procs fails.
  77. ##
  78. ## You can override this implementation
  79. ## by adding `-d:tempDir=mytempname` to your compiler invocation.
  80. ##
  81. ## .. Note:: This proc does not check whether the returned path exists.
  82. ##
  83. ## See also:
  84. ## * `getHomeDir proc`_
  85. ## * `getConfigDir proc`_
  86. result = Path(osappdirs.getTempDir())