@@ -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+ # # on windows: `getEnv("LOCALAPPDATA")`
940+ # #
941+ # # on osx: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
942+ # #
943+ # # else: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
944+ # #
945+ # # See also:
946+ # # * `getHomeDir proc <#getHomeDir>`_
947+ # # * `getTempDir proc <#getTempDir>`_
948+ # # * `getConfigDir proc <#getConfigDir>`_
949+ # follows https://crates.io/crates/platform-dirs
950+ runnableExamples:
951+ from std/ strutils import endsWith
952+ assert not getCacheDir ().endsWith DirSep
953+ when defined (windows):
954+ result = getEnv (" LOCALAPPDATA" )
955+ elif defined (osx):
956+ result = getEnv (" XDG_CACHE_HOME" , getEnv (" HOME" ) / " Library/Caches" )
957+ else :
958+ result = getEnv (" XDG_CACHE_HOME" , getEnv (" HOME" ) / " .cache" )
959+ result .normalizePathEnd (false )
960+
961+ proc getCacheDir * (app: string ): string =
962+ # # Returns the cache directory for an application `app`.
963+ # #
964+ # # on windows: getCacheDir() / app / "cache"
965+ # # else:: getCacheDir() / "cache"
966+ when defined (windows):
967+ getCacheDir () / app / " cache"
968+ else :
969+ getCacheDir () / app
970+
971+
938972when defined (windows):
939973 type DWORD = uint32
940974
0 commit comments