ospaths.expandTilde: handle ~ correctly; refactor to use DirSep, AltSep
This commit is contained in:
parent
143834ba4e
commit
20591845df
1 changed files with 12 additions and 14 deletions
|
|
@ -548,23 +548,21 @@ proc getTempDir*(): string {.rtl, extern: "nos$1",
|
||||||
|
|
||||||
proc expandTilde*(path: string): string {.
|
proc expandTilde*(path: string): string {.
|
||||||
tags: [ReadEnvEffect, ReadIOEffect].} =
|
tags: [ReadEnvEffect, ReadIOEffect].} =
|
||||||
## Expands a path starting with ``~/`` to a full path.
|
## Expands ``~`` or a path starting with ``~/`` to a full path, replacing
|
||||||
|
## ``~`` with ``getHomeDir()`` (otherwise returns ``path`` unmodified).
|
||||||
##
|
##
|
||||||
## If `path` starts with the tilde character and is followed by `/` or `\\`
|
## Windows: this is still supported despite Windows platform not having this
|
||||||
## this proc will return the reminder of the path appended to the result of
|
## convention; also, both ``~/`` and ``~\`` are handled.
|
||||||
## the getHomeDir() proc, otherwise the input path will be returned without
|
runnableExamples:
|
||||||
## modification.
|
doAssert expandTilde("~" / "appname.cfg") == getHomeDir() / "appname.cfg"
|
||||||
##
|
if len(path) == 0 or path[0] != '~':
|
||||||
## The behaviour of this proc is the same on the Windows platform despite
|
result = path
|
||||||
## not having this convention. Example:
|
elif len(path) == 1:
|
||||||
##
|
result = getHomeDir()
|
||||||
## .. code-block:: nim
|
elif (path[1] in {DirSep, AltSep}):
|
||||||
## let configFile = expandTilde("~" / "appname.cfg")
|
|
||||||
## echo configFile
|
|
||||||
## # --> C:\Users\amber\appname.cfg
|
|
||||||
if len(path) > 1 and path[0] == '~' and (path[1] == '/' or path[1] == '\\'):
|
|
||||||
result = getHomeDir() / path.substr(2)
|
result = getHomeDir() / path.substr(2)
|
||||||
else:
|
else:
|
||||||
|
# TODO: handle `~bob` and `~bob/` which means home of bob
|
||||||
result = path
|
result = path
|
||||||
|
|
||||||
proc quoteShellWindows*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} =
|
proc quoteShellWindows*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue