Skip to content

Commit 79e66c9

Browse files
committed
add os.getCacheDir
1 parent cfe1924 commit 79e66c9

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@
171171
- `--gc:orc` is now 10% faster than previously for common workloads. If
172172
you have trouble with its changed behavior, compile with `-d:nimOldOrc`.
173173

174-
175174
- `os.FileInfo` (returned by `getFileInfo`) now contains `blockSize`,
176175
determining preferred I/O block size for this file object.
177176

177+
- Added `os.getCacheDir()` to return platform specific cache directory.
178+
178179
- Added a simpler to use `io.readChars` overload.
179180

180181
- Added `**` to jsffi.

lib/pure/os.nim

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
938973
when defined(windows):
939974
type DWORD = uint32
940975

0 commit comments

Comments
 (0)