@@ -922,9 +922,7 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1",
922
922
# # See also:
923
923
# # * `getHomeDir proc <#getHomeDir>`_
924
924
# # * `getTempDir proc <#getTempDir>`_
925
- # # * `expandTilde proc <#expandTilde,string>`_
926
- # # * `getCurrentDir proc <#getCurrentDir>`_
927
- # # * `setCurrentDir proc <#setCurrentDir,string>`_
925
+ # # * `getCacheDir proc <#getCacheDir>`_
928
926
runnableExamples:
929
927
from std/ strutils import endsWith
930
928
# See `getHomeDir` for behavior regarding trailing DirSep.
@@ -935,6 +933,43 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1",
935
933
result = getEnv (" XDG_CONFIG_HOME" , getEnv (" HOME" ) / " .config" )
936
934
result .normalizePathEnd (trailingSep = defined (nimLegacyHomeDir))
937
935
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
+
938
973
when defined (windows):
939
974
type DWORD = uint32
940
975
0 commit comments