appdirs.nim 2.7 KB

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