elvish_config/lib/moi/util/file.elv

63 lines
1.3 KiB
Plaintext

use str
use ./list
use ./map
search-type = [
&exact= [m]{ ls $m 2>/dev/null }
&match= [m]{ put *$m* }
&prefix= [m]{ put $m* }
&suffix= [m]{ put *$m }
&deep-match= [m]{ put **$m** }
&deep-prefix= [m]{ put $m** }
&deep-suffix= [m]{ put **$m }
]
fn -less [f1 f2]{
local:fl1 local:fl2 = (str:to-lower $f1) (str:to-lower $f2)
local:c = (str:compare $fl1 $fl2)
if (== $c 0) {
c = (str:compare $f1 $f2)
}
put (< $c 0)
}
fn -get-results [sort result]{
if $sort {
keys $result | order $-less~
} else {
keys $result
}
}
fn -search [&sort=$false &type=exact @motive]{
local:f = $search-type[$type]
local:result = [&]
if (list:empty $motive) {
result = (put * | map:to-set)
} else {
peach [m]{
local:m = $m
try {
$f $m | peach [e]{
local:e = $e
result[$e] = $nil
}
} except e { }
} $motive
}
-get-results $sort $result
}
fn match-files [&sort=$false &type=prefix @motive]{ -search &sort=$sort &type=$type $@motive }
fn match-extensions [&sort=$false &type=deep-prefix motive @extensions]{
result = [&]
-search &type=$type $motive | peach [f]{
local:f = $f
if (list:contains [e]{ str:has-suffix $f .$e } $extensions) {
result[$f] = $nil
}
}
-get-results $sort $result
}