Skip to content

Commit

Permalink
add os.getCacheDir
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed May 30, 2021
1 parent cfe1924 commit 79e66c9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
3 changes: 2 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@
- `--gc:orc` is now 10% faster than previously for common workloads. If
you have trouble with its changed behavior, compile with `-d:nimOldOrc`.


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

- Added `os.getCacheDir()` to return platform specific cache directory.

- Added a simpler to use `io.readChars` overload.

- Added `**` to jsffi.
Expand Down
41 changes: 38 additions & 3 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -922,9 +922,7 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1",
## See also:
## * `getHomeDir proc <#getHomeDir>`_
## * `getTempDir proc <#getTempDir>`_
## * `expandTilde proc <#expandTilde,string>`_
## * `getCurrentDir proc <#getCurrentDir>`_
## * `setCurrentDir proc <#setCurrentDir,string>`_
## * `getCacheDir proc <#getCacheDir>`_
runnableExamples:
from std/strutils import endsWith
# See `getHomeDir` for behavior regarding trailing DirSep.
Expand All @@ -935,6 +933,43 @@ proc getConfigDir*(): string {.rtl, extern: "nos$1",
result = getEnv("XDG_CONFIG_HOME", getEnv("HOME") / ".config")
result.normalizePathEnd(trailingSep = defined(nimLegacyHomeDir))

proc getCacheDir*(): string =
## Returns the cache directory of the current user for applications.
##
## on windows: `getEnv("LOCALAPPDATA")`
##
## on osx: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")`
##
## else: `getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")`
##
## See also:
## * `getHomeDir proc <#getHomeDir>`_
## * `getTempDir proc <#getTempDir>`_
## * `getConfigDir proc <#getConfigDir>`_
# follows https://crates.io/crates/platform-dirs
runnableExamples:
from std/strutils import endsWith
assert not getCacheDir().endsWith DirSep
when defined(windows):
result = getEnv("LOCALAPPDATA")
elif defined(osx):
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / "Library/Caches")
else:
result = getEnv("XDG_CACHE_HOME", getEnv("HOME") / ".cache")
result.normalizePathEnd(false)

proc getCacheDir*(app: string): string =
## Returns the cache directory for an application `app`.
##
## on windows: `getCacheDir() / app / "cache"`
##
## else:: `getCacheDir() / "cache"`
when defined(windows):
getCacheDir() / app / "cache"
else:
getCacheDir() / app


when defined(windows):
type DWORD = uint32

Expand Down

0 comments on commit 79e66c9

Please sign in to comment.