Skip to content

Commit

Permalink
feat(os): expandTilde supports "~bob". closes nim-lang#24655
Browse files Browse the repository at this point in the history
  • Loading branch information
litlighilit committed Jan 29, 2025
1 parent d4466da commit b9c9e90
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,38 @@ proc expandTilde*(path: string): string {.
assert expandTilde("~/foo/bar") == getHomeDir() / "foo/bar"
assert expandTilde("/foo/bar") == "/foo/bar"

template tryGetHomeOrRet =
result = getHomeDir()
if result == "":
result = path
return

if len(path) == 0 or path[0] != '~':
result = path
elif len(path) == 1:
result = getHomeDir()
tryGetHomeOrRet
elif (path[1] in {DirSep, AltSep}):
result = getHomeDir() / path.substr(2)
tryGetHomeOrRet
result = result / path.substr(2)
elif compiles(getHomeDir("bob")):
# handle path beginning with `~bob` and `~bob/`
# which means home of bob
var i = path.find(DirSep, 1)
if i < 0:
i = len(path)

let
name = path[1..<i]
userhome = getHomeDir(name)

if userhome == "" and hostOS == "vsworks":
# XXX: As of the moment this line is written,
# hostOS won't be "vsworks" yet.
return path

result = userhome & path.substr(i)

else:
# TODO: handle `~bob` and `~bob/` which means home of bob
result = path

proc quoteShellWindows*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} =
Expand Down

0 comments on commit b9c9e90

Please # to comment.