@@ -922,9 +922,7 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1",
922922 # # See also:
923923 # # * `getHomeDir proc <#getHomeDir>`_
924924 # # * `getTempDir proc <#getTempDir>`_
925- # # * `expandTilde proc <#expandTilde,string>`_
926- # # * `getCurrentDir proc <#getCurrentDir>`_
927- # # * `setCurrentDir proc <#setCurrentDir,string>`_
925+ # # * `getCacheDir proc <#getCacheDir>`_
928926 runnableExamples:
929927 from std/ strutils import endsWith
930928 # See `getHomeDir` for behavior regarding trailing DirSep.
@@ -935,6 +933,42 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1",
935933 result = getEnv (" XDG_CONFIG_HOME" , getEnv (" HOME" ) / " .config" )
936934 result .normalizePathEnd (trailingSep = defined (nimLegacyHomeDir))
937935
936+ proc getCacheDir * (): string =
937+ # # Returns the cache directory of the current user for applications.
938+ # #
939+ # # This makes use of the following environment variables:
940+ # #
941+ # # * On Windows: `getEnv("LOCALAPPDATA")`
942+ # #
943+ # # * On macOS: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
944+ # #
945+ # # * On other platforms: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
946+ # #
947+ # # **See also:**
948+ # # * `getHomeDir proc <#getHomeDir>`_
949+ # # * `getTempDir proc <#getTempDir>`_
950+ # # * `getConfigDir proc <#getConfigDir>`_
951+ # follows https://crates.io/crates/platform-dirs
952+ when defined (windows):
953+ result = getEnv (" LOCALAPPDATA" )
954+ elif defined (osx):
955+ result = getEnv (" XDG_CACHE_HOME" , getEnv (" HOME" ) / " Library/Caches" )
956+ else :
957+ result = getEnv (" XDG_CACHE_HOME" , getEnv (" HOME" ) / " .cache" )
958+ result .normalizePathEnd (false )
959+
960+ proc getCacheDir * (app: string ): string =
961+ # # Returns the cache directory for an application `app`.
962+ # #
963+ # # * On windows, this uses: `getCacheDir() / app / "cache"`
964+ # #
965+ # # * On other platforms, this uses: `getCacheDir() / app`
966+ when defined (windows):
967+ getCacheDir () / app / " cache"
968+ else :
969+ getCacheDir () / app
970+
971+
938972when defined (windows):
939973 type DWORD = uint32
940974
@@ -972,9 +1006,7 @@ proc getTempDir*(): string {.rtl, extern: "nos$1",
9721006 # # See also:
9731007 # # * `getHomeDir proc <#getHomeDir>`_
9741008 # # * `getConfigDir proc <#getConfigDir>`_
975- # # * `expandTilde proc <#expandTilde,string>`_
976- # # * `getCurrentDir proc <#getCurrentDir>`_
977- # # * `setCurrentDir proc <#setCurrentDir,string>`_
1009+ # # * `getCacheDir proc <#getCacheDir>`_
9781010 runnableExamples:
9791011 from std/ strutils import endsWith
9801012 # See `getHomeDir` for behavior regarding trailing DirSep.
0 commit comments