-
Notifications
You must be signed in to change notification settings - Fork 20
/
util.elv
155 lines (138 loc) · 2.84 KB
/
util.elv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
fn dotify-string {|str dotify-length|
if (or (<= $dotify-length 0) (<= (count $str) $dotify-length)) {
put $str
} else {
put $str[..$dotify-length]'…'
}
}
use file
fn pipesplit {|l1 l2 l3|
var pout = (file:pipe)
var perr = (file:pipe)
run-parallel {
$l1 > $pout 2> $perr
file:close $pout[w]
file:close $perr[w]
} {
$l2 < $pout
file:close $pout[r]
} {
$l3 < $perr
file:close $perr[r]
}
}
var -read-upto-eol~ = {|eol| put (head -n1) }
use builtin
if (has-key $builtin: read-upto~) {
set -read-upto-eol~ = {|eol| read-upto $eol }
}
fn readline {|&eol="\n" &nostrip=$false &prompt=$nil|
if $prompt {
print $prompt > /dev/tty
}
var line = (if $prompt {
-read-upto-eol $eol < /dev/tty
} else {
-read-upto-eol $eol
})
if (and (not $nostrip) (!=s $line '') (==s $line[-1..] $eol)) {
put $line[..-1]
} else {
put $line
}
}
fn y-or-n {|&style=default prompt|
set prompt = $prompt" [y/n] "
if (not-eq $style default) {
set prompt = (styled $prompt $style)
}
print $prompt > /dev/tty
var resp = (readline)
eq $resp y
}
fn getfile {
use re
print 'Drop a file here: ' >/dev/tty
var fname = (read-line)
each {|p|
set fname = (re:replace $p[0] $p[1] $fname)
} [['\\(.)' '$1'] ['^''' ''] ['\s*$' ''] ['''$' '']]
put $fname
}
fn max {|a @rest &with={|v|put $v}|
var res = $a
var val = ($with $a)
each {|n|
var nval = ($with $n)
if (> $nval $val) {
set res = $n
set val = $nval
}
} $rest
put $res
}
fn min {|a @rest &with={|v|put $v}|
var res = $a
var val = ($with $a)
each {|n|
var nval = ($with $n)
if (< $nval $val) {
set res = $n
set val = $nval
}
} $rest
put $res
}
fn cond {|clauses|
range &step=2 (count $clauses) | each {|i|
var exp = $clauses[$i]
if (eq (kind-of $exp) fn) { set exp = ($exp) }
if $exp {
put $clauses[(+ $i 1)]
return
}
}
}
fn optional-input {|@input|
if (eq $input []) {
set input = [(all)]
} elif (== (count $input) 1) {
set input = [ (all $input[0]) ]
} else {
fail "util:optional-input: want 0 or 1 arguments, got "(count $input)
}
put $input
}
fn select {|p @input|
each {|i| if ($p $i) { put $i} } (optional-input $@input)
}
fn remove {|p @input|
each {|i| if (not ($p $i)) { put $i} } (optional-input $@input)
}
fn partial {|f @p-args|
put {|@args|
$f $@p-args $@args
}
}
fn path-in {|obj path &default=$nil|
each {|k|
try {
set obj = $obj[$k]
} catch {
set obj = $default
break
}
} $path
put $obj
}
use str
fn fix-deprecated {|f|
var deprecated = [
&all= all
&str:join= str:join
&str:split= str:split
&str:replace= str:replace
]
var sed-cmd = (str:join "; " [(keys $deprecated | each {|d| put "s/"$d"/"$deprecated[$d]"/" })])
sed -i '' -e $sed-cmd $f
}