@@ -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,43 @@ 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+ # #
966+ # # else:: `getCacheDir() / "cache"`
967+ when defined (windows):
968+ getCacheDir () / app / " cache"
969+ else :
970+ getCacheDir () / app
971+
972+
938973when defined (windows):
939974 type DWORD = uint32
940975
@@ -972,9 +1007,7 @@ proc getTempDir*(): string {.rtl, extern: "nos$1",
9721007 # # See also:
9731008 # # * `getHomeDir proc <#getHomeDir>`_
9741009 # # * `getConfigDir proc <#getConfigDir>`_
975- # # * `expandTilde proc <#expandTilde,string>`_
976- # # * `getCurrentDir proc <#getCurrentDir>`_
977- # # * `setCurrentDir proc <#setCurrentDir,string>`_
1010+ # # * `getCacheDir proc <#getCacheDir>`_
9781011 runnableExamples:
9791012 from std/ strutils import endsWith
9801013 # See `getHomeDir` for behavior regarding trailing DirSep.
0 commit comments