Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
codyopel committed Sep 15, 2024
1 parent 80e56a1 commit 58c3186
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
28 changes: 13 additions & 15 deletions path.elv
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,12 @@ fn escape {|path_ &unix=$false &input=$false &invert=$false|
}

if (and $platform:is-windows (not $unix)) { # DOS
# WARNING: Improperly escaped strings fail powershell functions
# silently for an unknown reason.
# FIXME: this is missing characters that need to be escaped.
# WARNING: Improperly escaped strings can silently fail powershell functions
var specialChars = [
'['
']'
]
var single = [
var singleEscape = [
'`' # Must come first
''''
' ' # Space
Expand All @@ -77,26 +75,26 @@ fn escape {|path_ &unix=$false &input=$false &invert=$false|
';'
'$'
]
var unicodeQuoteFinalPunctuationChars = [ (re:find '\p{Pf}' $path_) ]
for i $unicodeQuoteFinalPunctuationChars {
if (not (list:has $single $i)) {
set single = [ $@single $i ]
var unicodeChars = [ (re:find '([^\x00-\x7F])' $path_) ]
for i $unicodeChars {
if (not (list:has $singleEscape $i)) {
set singleEscape = [ $@singleEscape $i ]
}
}
var double = [ ]
var doubleEscape = [ ]
if $input {
# Special characters have to be double escaped when passed as input.
set double = $specialChars
set doubleEscape = $specialChars
} else {
set single = [
$@single
set singleEscape = [
$@singleEscape
$@specialChars
]
}
for i $double {
for i $doubleEscape {
set path_ = (str:replace (-order $i '``'$i) $path_)
}
for i $single {
for i $singleEscape {
set path_ = (str:replace (-order $i '`'$i) $path_)
}
put $path_
Expand Down Expand Up @@ -153,7 +151,7 @@ fn join {|@pathObjects|
clean (str:join $DELIMITER $pathObjects)
}

# Converts an absolute path in a relative path.
# Converts an absolute path into a relative path.
fn relative-to {|absolutePath relativeToAbsolutePath|
var p1 = $absolutePath
var p2 = $relativeToAbsolutePath
Expand Down
4 changes: 2 additions & 2 deletions path_test.elv
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ test:assert {

test:assert {
==s ^
(path:relative-to $E:HOME/.config/elvish $E:HOME/.local) ^
(path:relative-to $E:HOME'/.config/elvish' $E:HOME'/.local') ^
'../.config/elvish'
}
test:assert {
==s ^
(path:relative-to $E:HOME/.local $E:HOME/.config/elvish) ^
(path:relative-to $E:HOME'/.local' $E:HOME'/.config/elvish') ^
'../../.local'
}
2 changes: 1 addition & 1 deletion utils.elv
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use ./str


# env-var is a comma separated environment variable of preferred commands.
# cmds is a list of fallback commands if none exist in env-var.
# fallbackCmds is a list of fallback commands if none exist in env-var.
fn get-preferred-cmd {|envVar fallbackCmds|
var cmds = $fallbackCmds
try {
Expand Down

0 comments on commit 58c3186

Please sign in to comment.