elvish_config/lib/moi/util/file.elv

59 lines
1.3 KiB
Plaintext
Raw Normal View History

use str
use ./list
use ./map
2021-10-13 18:49:48 +00:00
var search-type = [
2021-12-11 21:09:02 +00:00
&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 }
2020-03-13 14:03:36 +00:00
]
2021-12-11 21:09:02 +00:00
fn -less {|f1 f2|
2021-08-27 20:06:49 +00:00
var fl1 fl2 = (str:to-lower $f1) (str:to-lower $f2)
var c = (str:compare $fl1 $fl2)
2020-03-13 14:03:36 +00:00
if (== $c 0) {
2021-10-13 18:49:48 +00:00
set c = (str:compare $f1 $f2)
2020-03-13 14:03:36 +00:00
}
put (< $c 0)
}
2021-12-11 21:09:02 +00:00
fn -get-results {|sort result|
2020-03-13 14:03:36 +00:00
if $sort {
2020-07-22 09:24:43 +00:00
keys $result | order $-less~
2020-03-13 14:03:36 +00:00
} else {
keys $result
}
}
2021-12-11 21:09:02 +00:00
fn -search {|&sort=$false &type=exact @motive|
2021-08-27 20:06:49 +00:00
var f result = $search-type[$type] [&]
2020-03-13 14:03:36 +00:00
if (list:empty $motive) {
2021-10-13 18:49:48 +00:00
set result = (put * | map:to-set)
2020-03-13 14:03:36 +00:00
} else {
2021-12-11 21:09:02 +00:00
peach {|m|
2020-03-13 14:03:36 +00:00
try {
2021-12-11 21:09:02 +00:00
$f $m | peach {|e|
2021-10-13 18:49:48 +00:00
set result[$e] = $nil
2020-03-13 14:03:36 +00:00
}
} except e { }
} $motive
}
-get-results $sort $result
}
2021-12-11 21:09:02 +00:00
fn match-files {|&sort=$false &type=prefix @motive| -search &sort=$sort &type=$type $@motive }
2020-03-13 14:03:36 +00:00
2021-12-11 21:09:02 +00:00
fn match-extensions {|&sort=$false &type=deep-prefix motive @extensions|
2021-10-13 18:49:48 +00:00
var result = [&]
2021-12-11 21:09:02 +00:00
-search &type=$type $motive | peach {|f|
if (list:contains {|e| str:has-suffix $f .$e } $extensions) {
2021-10-13 18:49:48 +00:00
set result[$f] = $nil
2020-03-13 14:03:36 +00:00
}
}
-get-results $sort $result
}