Compatibilité elvish 0.17

This commit is contained in:
Benjamin VAUDOUR 2021-12-11 21:09:02 +00:00
parent be84588373
commit 87eb82a791
43 changed files with 323 additions and 322 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.old
lib/github.com lib/github.com
private private

View File

@ -1,4 +1,4 @@
edit:add-var addbadips~ [@argv]{ edit:add-var addbadips~ {|@argv|
use moi/util/ip use moi/util/ip
use moi/util/list use moi/util/list
use moi/util/map use moi/util/map
@ -7,7 +7,7 @@ edit:add-var addbadips~ [@argv]{
var f = ~/bad_ips.txt var f = ~/bad_ips.txt
var ips = [ (cat $f) ] var ips = [ (cat $f) ]
var m c = (map:to-set $ips) 0 var m c = (map:to-set $ips) 0
each [e]{ each {|e|
var cond = (and (not (has-key $m $e)) (ip:is-ipv4 $e)) var cond = (and (not (has-key $m $e)) (ip:is-ipv4 $e))
if $cond { if $cond {
echo $e ajoutée echo $e ajoutée

View File

@ -1,4 +1,4 @@
edit:add-var adguard~ [@argv]{ edit:add-var adguard~ {|@argv|
use str use str
sudo chattr -i /etc/resolv.conf sudo chattr -i /etc/resolv.conf
var beg = 's|^.*\(192\.168\.0\.25\)|' var beg = 's|^.*\(192\.168\.0\.25\)|'

View File

@ -1,17 +1,17 @@
edit:add-var cat~ [@argv]{ e:bat -pp --tabs 2 $@argv } edit:add-var cat~ {|@argv| e:bat -pp --tabs 2 $@argv }
edit:add-var cp~ [@argv]{ e:cp -iv $@argv } edit:add-var cp~ {|@argv| e:cp -iv $@argv }
edit:add-var curl~ $e:curlie~ edit:add-var curl~ $e:curlie~
edit:add-var df~ $e:duf~ edit:add-var df~ $e:duf~
edit:add-var diff~ $e:colordiff~ edit:add-var diff~ $e:colordiff~
edit:add-var dig~ [@argv]{ e:dig +noall +answer $@argv } edit:add-var dig~ {|@argv| e:dig +noall +answer $@argv }
edit:add-var dogall~ [@argv]{ e:dog A AAAA CNAME TXT MX NS SOA $@argv } edit:add-var dogall~ {|@argv| e:dog A AAAA CNAME TXT MX NS SOA $@argv }
edit:add-var du~ [@argv]{ e:du -h $@argv } edit:add-var du~ {|@argv| e:du -h $@argv }
edit:add-var git~ $e:hub~ edit:add-var git~ $e:hub~
edit:add-var grep~ [@argv]{ e:grep --color=tty -d skip $@argv } edit:add-var grep~ {|@argv| e:grep --color=tty -d skip $@argv }
edit:add-var mv~ [@argv]{ e:mv -iv $@argv } edit:add-var mv~ {|@argv| e:mv -iv $@argv }
edit:add-var pf~ [@argv]{ e:pkgfile -vri $@argv } edit:add-var pf~ {|@argv| e:pkgfile -vri $@argv }
edit:add-var rm~ [@argv]{ e:rm -Iv $@argv } edit:add-var rm~ {|@argv| e:rm -Iv $@argv }
edit:add-var rename~ $e:go-rename~ edit:add-var rename~ $e:go-rename~
edit:add-var timidity~ [@argv]{ e:timidity -a -K 1 $@argv } edit:add-var timidity~ {|@argv| e:timidity -a -K 1 $@argv }
edit:add-var tree~ [@argv]{ e:tree -puhC --dirsfirst --du -D $@argv } edit:add-var tree~ {|@argv| e:tree -puhC --dirsfirst --du -D $@argv }
edit:add-var wget~ [@argv]{ e:wget2 $@argv } edit:add-var wget~ {|@argv| e:wget2 $@argv }

View File

@ -1,6 +1,6 @@
var cmd = $e:arc~ var cmd = $e:arc~
edit:add-var archiver~ [@argv]{ edit:add-var archiver~ {|@argv|
use str use str
var help = { var help = {
echo "Usage : archiver [(-zlxgb)] <file to compress>" echo "Usage : archiver [(-zlxgb)] <file to compress>"
@ -27,7 +27,7 @@ edit:add-var archiver~ [@argv]{
if (== (count $argv) 0) { if (== (count $argv) 0) {
$help $help
} }
peach [f]{ peach {|f|
var ff = $f var ff = $f
if (eq $ff[-1] /) { if (eq $ff[-1] /) {
set f = $ff[..-1] set f = $ff[..-1]
@ -37,16 +37,16 @@ edit:add-var archiver~ [@argv]{
} $argv } $argv
} }
edit:add-var desarchiver~ [@argv]{ edit:add-var desarchiver~ {|@argv|
peach [f]{ peach {|f|
var ff = $f var ff = $f
$cmd unarchive $ff $cmd unarchive $ff
echo "'"$ff"'" désarchivé echo "'"$ff"'" désarchivé
} $argv } $argv
} }
edit:add-var listarchiver~ [@argv]{ edit:add-var listarchiver~ {|@argv|
peach [f]{ peach {|f|
var ff = $f var ff = $f
$cmd ls $ff $cmd ls $ff
} $argv } $argv

View File

@ -1,4 +1,4 @@
edit:add-var updatecarapace~ []{ edit:add-var updatecarapace~ {||
var cur = (pwd) var cur = (pwd)
var wd = $E:HOME/Documents/OtherGo/github.com/rsteube/carapace-bin var wd = $E:HOME/Documents/OtherGo/github.com/rsteube/carapace-bin
var bd = $E:HOME/Go/bin var bd = $E:HOME/Go/bin

View File

@ -1,8 +1,8 @@
use str use str
fn -check [f]{ fn -check {|f|
var @missings = (try { var @missings = (try {
sudo ldd $f 2>/dev/null | grep 'not found' | eawk [_ m @_]{ put $m } sudo ldd $f 2>/dev/null | grep 'not found' | eawk {|_ m @_| put $m }
} except e { } except e {
}) })
if (> (count $missings) 1) { if (> (count $missings) 1) {
@ -11,28 +11,28 @@ fn -check [f]{
} }
} }
fn -check-executable [f]{ fn -check-executable {|f|
if ?(test -x $f) { if ?(test -x $f) {
-check $f -check $f
} }
} }
fn -cbin []{ fn -cbin {||
echo (styled 'Searching broken binaries…' bold bright-yellow) echo (styled 'Searching broken binaries…' bold bright-yellow)
sudo find /usr/bin -maxdepth 1 -type f 2>/dev/null | each $-check~ sudo find /usr/bin -maxdepth 1 -type f 2>/dev/null | each $-check~
} }
fn -clib []{ fn -clib {||
echo (styled 'Searching broken libs…' bold bright-yellow) echo (styled 'Searching broken libs…' bold bright-yellow)
sudo find /usr/lib/*.so* -maxdepth 1 -type f 2>/dev/null | each $-check-executable~ sudo find /usr/lib/*.so* -maxdepth 1 -type f 2>/dev/null | each $-check-executable~
} }
fn -copt []{ fn -copt {||
echo (styled 'Searching broken binariess in opt…' bold bright-yellow) echo (styled 'Searching broken binariess in opt…' bold bright-yellow)
sudo find /opt -maxdepth 3 -type f 2>/dev/null | each $-check-executable~ sudo find /opt -maxdepth 3 -type f 2>/dev/null | each $-check-executable~
} }
edit:add-var checklinks~ []{ edit:add-var checklinks~ {||
-cbin -cbin
-clib -clib
-copt -copt

View File

@ -1,10 +1,10 @@
edit:add-var cleankh~ [@argv]{ edit:add-var cleankh~ {|@argv|
use str use str
use moi/util/condition use moi/util/condition
use moi/util/list use moi/util/list
use moi/util/ip use moi/util/ip
fn less [cmp e1 e2]{ < ($cmp $e1 $e2) 0 } fn less {|cmp e1 e2| < ($cmp $e1 $e2) 0 }
fn sort-host [e1 e2]{ fn sort-host {|e1 e2|
var i1 = (condition:cset (ip:is-ip $e1) 1 0) var i1 = (condition:cset (ip:is-ip $e1) 1 0)
var i2 = (condition:cset (ip:is-ip $e2) 1 0) var i2 = (condition:cset (ip:is-ip $e2) 1 0)
if (!= $i1 $i2) { if (!= $i1 $i2) {
@ -17,17 +17,17 @@ edit:add-var cleankh~ [@argv]{
} }
var khosts = [&] var khosts = [&]
cat ~/.ssh/known_hosts | eawk [_ hosts @rest]{ cat ~/.ssh/known_hosts | eawk {|_ hosts @rest|
var key = (str:join ' ' $rest) var key = (str:join ' ' $rest)
if (not (has-key $khosts $key)) { if (not (has-key $khosts $key)) {
set khosts[$key] = [&] set khosts[$key] = [&]
} }
str:split , $hosts | each [h]{ str:split , $hosts | each {|h|
set khosts[$key][$h] = $nil set khosts[$key][$h] = $nil
} }
} }
var lines = [(keys $khosts | each [key]{ var lines = [(keys $khosts | each {|key|
var hosts = (keys $khosts[$key] | order &less-than=$sort-host~ | str:join ,) var hosts = (keys $khosts[$key] | order &less-than=$sort-host~ | str:join ,)
put $hosts $key | str:join ' ' put $hosts $key | str:join ' '
})] })]

View File

@ -1,11 +1,11 @@
edit:add-var commitkcp~ [@argv]{ edit:add-var commitkcp~ {|@argv|
use str use str
var m = [ var m = [
&name= &name=
&ver= &ver=
] ]
keys $m | each [k]{ keys $m | each {|k|
var p = 'pkg'$k'=' var p = 'pkg'$k'='
set m[$k] = (str:trim-prefix (cat PKGBUILD | grep $p | take 1) $p) set m[$k] = (str:trim-prefix (cat PKGBUILD | grep $p | take 1) $p)
} }

View File

@ -1,4 +1,4 @@
edit:add-var digall~ [@argv]{ edit:add-var digall~ {|@argv|
use moi/util/list use moi/util/list
use moi/util/strutil use moi/util/strutil
use str use str
@ -27,12 +27,12 @@ edit:add-var digall~ [@argv]{
] ]
var domains use_dig = [] $true var domains use_dig = [] $true
fn init []{ fn init {||
if (and (> (count $argv) 0) (is $argv[0] -d)) { if (and (> (count $argv) 0) (is $argv[0] -d)) {
set use_dig = $false set use_dig = $false
set argv = $argv[1:] set argv = $argv[1:]
} }
each [e]{ each {|e|
if (is $e[0] '+') { if (is $e[0] '+') {
set flags = [ (all $flags) $e ] set flags = [ (all $flags) $e ]
} elif (is $e[0] @) { } elif (is $e[0] @) {
@ -48,10 +48,10 @@ edit:add-var digall~ [@argv]{
} }
} }
fn format [results]{ fn format {|results|
eawk [_ @args]{ eawk {|_ @args|
put [ $args[0] (all $args[2..]) ] put [ $args[0] (all $args[2..]) ]
} $results | list:remove-duplicate | list:sort [l1 l2]{ } $results | list:remove-duplicate | list:sort {|l1 l2|
var c = (str:compare $l1[0] $l2[0]) var c = (str:compare $l1[0] $l2[0])
if (== $c 0) { if (== $c 0) {
set c = (str:compare $l1[2] $l2[2]) set c = (str:compare $l1[2] $l2[2])
@ -59,23 +59,23 @@ edit:add-var digall~ [@argv]{
} | strutil:format &col=$cols | str:join "\n" } | strutil:format &col=$cols | str:join "\n"
} }
fn drillr [d @args]{ fn drillr {|d @args|
var results = [ (peach [r]{ var results = [ (peach {|r|
e:drill $@args $r $d | list:filter [e]{ e:drill $@args $r $d | list:filter {|e|
and (> (count $e) 2) (not (is $e[:2] ';;')) and (> (count $e) 2) (not (is $e[:2] ';;'))
} }
} $records) ] } $records) ]
format $results format $results
} }
fn digr [d @args]{ fn digr {|d @args|
var results = [ (peach [r]{ var results = [ (peach {|r|
e:dig $@args $r $d $@flags e:dig $@args $r $d $@flags
} $records) ] } $records) ]
format $results format $results
} }
fn req [d @args]{ fn req {|d @args|
if $use_dig { if $use_dig {
digr $d $@args digr $d $@args
} else { } else {
@ -83,15 +83,15 @@ edit:add-var digall~ [@argv]{
} }
} }
fn digs [s d]{ fn digs {|s d|
put (echo (styled $s bright-yellow)) put (echo (styled $s bright-yellow))
req $d $s req $d $s
} }
fn digd [d]{ fn digd {|d|
var results = [ (echo (styled 'Domaine: '$d bright-green)) ] var results = [ (echo (styled 'Domaine: '$d bright-green)) ]
if (> (count $servers) 0) { if (> (count $servers) 0) {
each [s]{ each {|s|
set @results = $@results (digs $s $d) set @results = $@results (digs $s $d)
} $servers } $servers
} else { } else {

View File

@ -1,7 +1,7 @@
use re use re
use str use str
fn -value [v]{ fn -value {|v|
var p = (re:find '^(\d+)\((\w+)\)$' $v) var p = (re:find '^(\d+)\((\w+)\)$' $v)
if (and (has-key $p groups) (== (count $p[groups]) 3)) { if (and (has-key $p groups) (== (count $p[groups]) 3)) {
put [ put [
@ -11,7 +11,7 @@ fn -value [v]{
} }
} }
fn -values [v]{ fn -values {|v|
var vv = [ (str:split ',' $v) ] var vv = [ (str:split ',' $v) ]
if (== (count $vv) 1) { if (== (count $vv) 1) {
-value $vv[0] -value $vv[0]
@ -20,9 +20,9 @@ fn -values [v]{
} }
} }
fn -get-id []{ fn -get-id {||
var id = [&] var id = [&]
str:split ' ' (id) | each [e]{ str:split ' ' (id) | each {|e|
var p = [ (str:split '=' $e) ] var p = [ (str:split '=' $e) ]
if (== (count $p) 2) { if (== (count $p) 2) {
set id[$p[0]] = (-values $p[1]) set id[$p[0]] = (-values $p[1])
@ -31,19 +31,19 @@ fn -get-id []{
put $id put $id
} }
fn -get [k]{ fn -get {|k|
var id = (-get-id) var id = (-get-id)
if (has-key $id $k) { if (has-key $id $k) {
put $id[$k] put $id[$k]
} }
} }
fn -is-root []{ fn -is-root {||
== (-get uid)[number] 0 == (-get uid)[number] 0
} }
edit:add-var get-id~ $-get-id~ edit:add-var get-id~ $-get-id~
edit:add-var get-uid~ []{ -get uid } edit:add-var get-uid~ {|| -get uid }
edit:add-var get-gid~ []{ -get gid } edit:add-var get-gid~ {|| -get gid }
edit:add-var get-groups~ []{ -get groups; -get groupes } edit:add-var get-groups~ {|| -get groups; -get groupes }
edit:add-var is-root~ $-is-root~ edit:add-var is-root~ $-is-root~

View File

@ -1,14 +1,14 @@
edit:add-var ipinfo~ [@argv]{ edit:add-var ipinfo~ {|@argv|
use moi/util/ip use moi/util/ip
use str use str
var token = (cat $E:HOME/.config/elvish/private/ipinfo_token) var token = (cat $E:HOME/.config/elvish/private/ipinfo_token)
fn url-of [ip]{ fn url-of {|ip|
str:join '' [ 'ipinfo.io/' $ip '?token=' $token] str:join '' [ 'ipinfo.io/' $ip '?token=' $token]
} }
fn info [ip]{ fn info {|ip|
if (ip:is-ip $ip) { if (ip:is-ip $ip) {
var url = (url-of $ip) var url = (url-of $ip)
echo 'URL: '$url echo 'URL: '$url

View File

@ -1,11 +1,11 @@
edit:add-var ipof~ [@argv]{ edit:add-var ipof~ {|@argv|
use str use str
use moi/util/condition use moi/util/condition
use moi/util/ip use moi/util/ip
fn less [cmp e1 e2]{ < ($cmp $e1 $e2) 0 } fn less {|cmp e1 e2| < ($cmp $e1 $e2) 0 }
fn sort-ip [e1 e2]{ fn sort-ip {|e1 e2|
var i1 = (condition:cset (ip:is-ipv4 $e1) 0 (condition:cset (ip:is-ipv6 $e1) 1 2)) var i1 = (condition:cset (ip:is-ipv4 $e1) 0 (condition:cset (ip:is-ipv6 $e1) 1 2))
var i2 = (condition:cset (ip:is-ipv4 $e2) 0 (condition:cset (ip:is-ipv6 $e2) 1 2)) var i2 = (condition:cset (ip:is-ipv4 $e2) 0 (condition:cset (ip:is-ipv6 $e2) 1 2))
if (!= $i1 $i2) { if (!= $i1 $i2) {
@ -19,18 +19,18 @@ edit:add-var ipof~ [@argv]{
} }
} }
fn dnssolve [d]{ fn dnssolve {|d|
var result = [ var result = [
&ips=[&] &ips=[&]
&resolvers='' &resolvers=''
&txt='' &txt=''
] ]
e:dnscrypt-proxy -config /etc/dnscrypt-proxy/dnscrypt-proxy.toml -resolve (idn -a $d) | peach [l]{ e:dnscrypt-proxy -config /etc/dnscrypt-proxy/dnscrypt-proxy.toml -resolve (idn -a $d) | peach {|l|
var ll = $l var ll = $l
var idx = (+ (str:index $ll :) 1) var idx = (+ (str:index $ll :) 1)
var value = (str:trim-space $ll[$idx".."]) var value = (str:trim-space $ll[$idx".."])
if (or (str:has-prefix $ll 'IPv4 addresses') (str:has-prefix $ll 'IPv6 addresses')) { if (or (str:has-prefix $ll 'IPv4 addresses') (str:has-prefix $ll 'IPv6 addresses')) {
str:split ', ' $value | peach [e]{ str:split ', ' $value | peach {|e|
var ee = $e var ee = $e
if (ip:is-ip $ee) { if (ip:is-ip $ee) {
set result[ips][$ee] = $nil set result[ips][$ee] = $nil
@ -48,13 +48,13 @@ edit:add-var ipof~ [@argv]{
} }
var hosts = [&] var hosts = [&]
cat /etc/hosts | peach [l]{ cat /etc/hosts | peach {|l|
var ll = (str:trim-space $l) var ll = (str:trim-space $l)
if (and (not-eq $ll '') (not-eq $ll[0] '#')) { if (and (not-eq $ll '') (not-eq $ll[0] '#')) {
put $ll put $ll
} }
} | eawk [_ ip @domains]{ } | eawk {|_ ip @domains|
peach [d]{ peach {|d|
var dd = $d var dd = $d
if (has-key $hosts $dd) { if (has-key $hosts $dd) {
set hosts[$dd][$ip] = $nil set hosts[$dd][$ip] = $nil
@ -65,7 +65,7 @@ edit:add-var ipof~ [@argv]{
} }
var remote = [&] var remote = [&]
peach [d]{ peach {|d|
var dd = $d var dd = $d
var solve = (dnssolve $dd) var solve = (dnssolve $dd)
if (> (keys $solve[ips] | count) 0) { if (> (keys $solve[ips] | count) 0) {
@ -73,14 +73,14 @@ edit:add-var ipof~ [@argv]{
} }
} $argv } $argv
each [d]{ each {|d|
var exists = $false var exists = $false
echo (styled 'Resolving '$d'…' bright-green) echo (styled 'Resolving '$d'…' bright-green)
echo echo
if (has-key $hosts $d) { if (has-key $hosts $d) {
set exists = $true set exists = $true
echo (styled ' local:' bright-yellow) echo (styled ' local:' bright-yellow)
keys $hosts[$d] | order &less-than=$sort-ip~ | each [ip]{ keys $hosts[$d] | order &less-than=$sort-ip~ | each {|ip|
echo ' - '$ip echo ' - '$ip
} }
} }
@ -89,7 +89,7 @@ edit:add-var ipof~ [@argv]{
var info = $remote[$d] var info = $remote[$d]
echo (styled ' remote:' bright-yellow) echo (styled ' remote:' bright-yellow)
echo ' IPs:' echo ' IPs:'
keys $info[ips] | order &less-than=$sort-ip~ | each [ip]{ keys $info[ips] | order &less-than=$sort-ip~ | each {|ip|
echo ' - '$ip echo ' - '$ip
} }
echo ' Resolvers: '$info[resolvers] echo ' Resolvers: '$info[resolvers]

View File

@ -1,14 +1,14 @@
fn -ls [@argv]{ fn -ls {|@argv|
e:ls -N --color=auto --group-directories-first --ignore=lost+found '--time-style=+"%Y.%m.%d %H:%M"' --indicator-style=classify $@argv e:ls -N --color=auto --group-directories-first --ignore=lost+found '--time-style=+"%Y.%m.%d %H:%M"' --indicator-style=classify $@argv
} }
fn -go-ls [@argv]{ fn -go-ls {|@argv|
e:go-ls --group-directories-first -T '+%Y.%m.%d %H:%M' --git $@argv e:go-ls --group-directories-first -T '+%Y.%m.%d %H:%M' --git $@argv
} }
var cmd = $-go-ls~ var cmd = $-go-ls~
edit:add-var ls~ [@argv]{ $cmd $@argv } edit:add-var ls~ {|@argv| $cmd $@argv }
edit:add-var la~ [@argv]{ $cmd -a $@argv } edit:add-var la~ {|@argv| $cmd -a $@argv }
edit:add-var ll~ [@argv]{ $cmd -lh $@argv } edit:add-var ll~ {|@argv| $cmd -lh $@argv }
edit:add-var lla~ [@argv]{ $cmd -lha $@argv } edit:add-var lla~ {|@argv| $cmd -lha $@argv }

View File

@ -1,4 +1,4 @@
edit:add-var meteo~ [@argv]{ edit:add-var meteo~ {|@argv|
use str use str
#curl --connect-timeout 2 "wttr.in/"(joins '' $_args)"?lang=fr" #curl --connect-timeout 2 "wttr.in/"(joins '' $_args)"?lang=fr"
e:curlie -s --connect-timeout 2 "wttr.in/"(str:join '' $argv)"?lang=fr" e:curlie -s --connect-timeout 2 "wttr.in/"(str:join '' $argv)"?lang=fr"

View File

@ -1 +1 @@
edit:add-var pacmanhisto~ [@argv]{ e:cat /var/log/pacman.log | e:grep "ALPM" | e:grep $@argv } edit:add-var pacmanhisto~ {|@argv| e:cat /var/log/pacman.log | e:grep "ALPM" | e:grep $@argv }

View File

@ -1,4 +1,4 @@
edit:add-var pdf2grey~ [@argv]{ edit:add-var pdf2grey~ {|@argv|
use re use re
use str use str
var m = [ var m = [
@ -6,7 +6,7 @@ edit:add-var pdf2grey~ [@argv]{
&split=50 &split=50
] ]
fn _help []{ fn _help {||
echo 'Usage: pdf2gray [-hn (<n>)] <input>.pdf <output>.pdf' echo 'Usage: pdf2gray [-hn (<n>)] <input>.pdf <output>.pdf'
echo '' echo ''
echo 'Options:' echo 'Options:'
@ -14,15 +14,15 @@ edit:add-var pdf2grey~ [@argv]{
echo ' -n [0-100] Convertit en noir et blanc. Si un nombre est spécifié, indique le pourcentage de coupure (par défaut: 50)' echo ' -n [0-100] Convertit en noir et blanc. Si un nombre est spécifié, indique le pourcentage de coupure (par défaut: 50)'
} }
fn is_pdf [e]{ fn is_pdf {|e|
re:match '.*\.pdf$' $e re:match '.*\.pdf$' $e
} }
fn is_int [e]{ fn is_int {|e|
re:match '^\d+$' $e re:match '^\d+$' $e
} }
fn is_split [e]{ fn is_split {|e|
and (>= $e 0) (<= $e 100) and (>= $e 0) (<= $e 100)
} }

View File

@ -1,9 +1,9 @@
edit:add-var publicip~ [@argv]{ edit:add-var publicip~ {|@argv|
var f = [t]{ var f = {|t|
use moi/util/condition use moi/util/condition
set t = (condition:cset (eq $t -6) -6 -4) set t = (condition:cset (eq $t -6) -6 -4)
var l = (condition:cset (eq $t -6) 'IPv6: ' 'IPv4: ') var l = (condition:cset (eq $t -6) 'IPv6: ' 'IPv4: ')
curl $t ifconfig.co/ip 2> /dev/null | each [ip]{ echo (styled $l bold yellow) $ip } curl $t ifconfig.co/ip 2> /dev/null | each {|ip| echo (styled $l bold yellow) $ip }
} }
$f -4 $f -4
$f -6 $f -6

View File

@ -1,44 +1,44 @@
use str use str
use re use re
fn -bytes [begin size]{ fn -bytes {|begin size|
str:from-codepoints (range $begin (+ $begin $size)) str:from-codepoints (range $begin (+ $begin $size))
} }
fn -upper []{ fn -upper {||
-bytes 0x41 26 -bytes 0x41 26
} }
fn -lower []{ fn -lower {||
-bytes 0x61 26 -bytes 0x61 26
} }
fn -digit []{ fn -digit {||
-bytes 0x30 10 -bytes 0x30 10
} }
fn -j [@strings]{ fn -j {|@strings|
str:join '' $strings str:join '' $strings
} }
fn -alpha []{ fn -alpha {||
-j (-upper) (-lower) -j (-upper) (-lower)
} }
fn -alphanum []{ fn -alphanum {||
-j (-alpha) (-digit) -j (-alpha) (-digit)
} }
fn -random [chars]{ fn -random {|chars|
var size = (count $chars) var size = (count $chars)
print $chars[(randint 0 $size)] print $chars[(randint 0 $size)]
} }
fn -is-int [arg]{ fn -is-int {|arg|
re:match '^\d+$' $arg re:match '^\d+$' $arg
} }
fn -help []{ fn -help {||
echo 'Génère une chaîne de caractères aléatoires' echo 'Génère une chaîne de caractères aléatoires'
echo 'Usage: genstr [(-auldh)] [<chars>] [<size>]' echo 'Usage: genstr [(-auldh)] [<chars>] [<size>]'
echo '' echo ''
@ -54,9 +54,9 @@ fn -help []{
echo 'Si aucune classe de caractères ou liste de caractères nest fournie, utilise les caractères alphanumériques ([A-Za-z0-9]).' echo 'Si aucune classe de caractères ou liste de caractères nest fournie, utilise les caractères alphanumériques ([A-Za-z0-9]).'
} }
edit:add-var genstr~ [@args]{ edit:add-var genstr~ {|@args|
var len chars h = 0 '' $false var len chars h = 0 '' $false
each [e]{ each {|e|
if (or (eq $e -h) (eq $e --help)) { if (or (eq $e -h) (eq $e --help)) {
set h = $true set h = $true
-help -help
@ -86,7 +86,7 @@ edit:add-var genstr~ [@args]{
set chars = [(str:split '' $chars)] set chars = [(str:split '' $chars)]
range $len | each [_]{ range $len | each {|_|
-random $chars -random $chars
} }
echo echo

View File

@ -1,3 +1,3 @@
edit:add-var riot~ []{ edit:add-var riot~ {||
cat $E:HOME/.config/elvish/private/riot.txt | xclip -selection clipboard cat $E:HOME/.config/elvish/private/riot.txt | xclip -selection clipboard
} }

View File

@ -1,4 +1,4 @@
edit:add-var sendsms~ [@argv]{ edit:add-var sendsms~ {|@argv|
use str use str
var api = https://smsapi.free-mobile.fr/sendmsg var api = https://smsapi.free-mobile.fr/sendmsg

View File

@ -1,2 +1,2 @@
edit:add-var trsen~ [@argv]{ e:rlwrap trans -b -t en $@argv } edit:add-var trsen~ {|@argv| e:rlwrap trans -b -t en $@argv }
edit:add-var trsfr~ [@argv]{ e:rlwrap trans -b -t fr $@argv } edit:add-var trsfr~ {|@argv| e:rlwrap trans -b -t fr $@argv }

View File

@ -1,4 +1,4 @@
edit:add-var upd~ [@args]{ edit:add-var upd~ {|@args|
if ?(mirror-check --fast) { if ?(mirror-check --fast) {
sudo pacman -Syu sudo pacman -Syu
} }

View File

@ -1,10 +1,10 @@
var cmd = $e:vim~ var cmd = $e:vim~
edit:add-var vi~ [@argv]{ $cmd $@argv } edit:add-var vi~ {|@argv| $cmd $@argv }
edit:add-var vb~ [@argv]{ $cmd $@argv ~/.bashrc } edit:add-var vb~ {|@argv| $cmd $@argv ~/.bashrc }
edit:add-var vc~ [@argv]{ $cmd $@argv ~/.vimrc } edit:add-var vc~ {|@argv| $cmd $@argv ~/.vimrc }
edit:add-var ve~ [@argv]{ $cmd $@argv ~/.config/elvish/rc.elv } edit:add-var ve~ {|@argv| $cmd $@argv ~/.config/elvish/rc.elv }
edit:add-var vf~ [@argv]{ $cmd $@argv ~/.config/fish/config.fish } edit:add-var vf~ {|@argv| $cmd $@argv ~/.config/fish/config.fish }
edit:add-var vp~ [@argv]{ $cmd $@argv PKGBUILD } edit:add-var vp~ {|@argv| $cmd $@argv PKGBUILD }
edit:add-var vv~ [@argv]{ $cmd -R $@argv } edit:add-var vv~ {|@argv| $cmd -R $@argv }
edit:add-var vz~ [@argv]{ $cmd $@argv ~/.zshrc } edit:add-var vz~ {|@argv| $cmd $@argv ~/.zshrc }

View File

@ -1,16 +1,16 @@
fn -sync [d1 d2]{ fn -sync {|d1 d2|
rsync -azh --delete --info=progress2,stats ~/.config/$d1/ ~/.config/$d2/ rsync -azh --delete --info=progress2,stats ~/.config/$d1/ ~/.config/$d2/
} }
fn -save [d]{ fn -save {|d|
-sync $d $d.save -sync $d $d.save
} }
fn -restore [d]{ fn -restore {|d|
-sync $d.save $d -sync $d.save $d
} }
edit:add-var viv-save~ []{ -save vivaldi } edit:add-var viv-save~ {|| -save vivaldi }
edit:add-var viv-restore~ []{ -restore vivaldi } edit:add-var viv-restore~ {|| -restore vivaldi }
edit:add-var vivd-save~ []{ -save vivaldi-developer } edit:add-var vivd-save~ {|| -save vivaldi-developer }
edit:add-var vivd-restore~ []{ -restore vivaldi-developer } edit:add-var vivd-restore~ {|| -restore vivaldi-developer }

View File

@ -1,16 +1,16 @@
var cmd = $e:yt-dlp~ var cmd = $e:yt-dlp~
#var cmd = $e:youtube-dl~ #var cmd = $e:youtube-dl~
fn -dl [@argv]{ fn -dl {|@argv|
$cmd --no-check-certificate $@argv $cmd --no-check-certificate $@argv
} }
fn -audio [@argv]{ fn -audio {|@argv|
-dl -x --audio-format best $@argv -dl -x --audio-format best $@argv
} }
edit:add-var youtube-dl~ [@argv]{ -dl $@argv } edit:add-var youtube-dl~ {|@argv| -dl $@argv }
edit:add-var youtube-audio~ [@argv]{ -audio $@argv } edit:add-var youtube-audio~ {|@argv| -audio $@argv }
edit:add-var yt-dl~ [@argv]{ -dl $@argv } edit:add-var yt-dl~ {|@argv| -dl $@argv }
edit:add-var yt-audio~ [@argv]{ -audio $@argv } edit:add-var yt-audio~ {|@argv| -audio $@argv }

View File

@ -13,20 +13,20 @@ var extensions = [ tar bz2 zip gz lz4 sz xz zst rar ]
fn -comp-commands { all $commands } fn -comp-commands { all $commands }
fn -comp-archive-files [m]{ fn -comp-archive-files {|m|
var type = (condition:cset (eq $m '') prefix deep-prefix) var type = (condition:cset (eq $m '') prefix deep-prefix)
file:match-extensions &type=$type $m $@extensions file:match-extensions &type=$type $m $@extensions
} }
fn -comp-inline-files [archive]{ fn -comp-inline-files {|archive|
try { try {
arc ls $archive | eawk [_ @argv]{ put $argv[-1] } arc ls $archive | eawk {|_ @argv| put $argv[-1] }
} except e { } except e {
nop nop
} }
} }
fn complete [@argv]{ fn complete {|@argv|
var c cmd = (count $argv) $argv[1] var c cmd = (count $argv) $argv[1]
if (== $c 2) { if (== $c 2) {
-comp-commands -comp-commands

View File

@ -17,12 +17,12 @@ var description = [
] ]
fn -options { fn -options {
each [o]{ each {|o|
put [&short=$o &desc=$description[$o]] put [&short=$o &desc=$description[$o]]
} $options } $options
} }
fn complete [@argv]{ fn complete {|@argv|
edit:complete-getopt $argv [(-options)] [ $edit:complete-filename~ ...] edit:complete-getopt $argv [(-options)] [ $edit:complete-filename~ ...]
} }

View File

@ -3,7 +3,7 @@ use moi/util/file
var extensions = [ tar bz2 zip gz lz4 sz xz zst rar ] var extensions = [ tar bz2 zip gz lz4 sz xz zst rar ]
fn complete [@argv]{ fn complete {|@argv|
var m = $argv[-1] var m = $argv[-1]
var type = (condition:cset (eq $m '') prefix deep-prefix) var type = (condition:cset (eq $m '') prefix deep-prefix)
file:match-extensions &type=$type $m $@extensions file:match-extensions &type=$type $m $@extensions

View File

@ -29,7 +29,7 @@ var np = [
-V -V
] ]
fn complete [@argv]{ fn complete {|@argv|
var c = (count $argv) var c = (count $argv)
if (== $c 2) { if (== $c 2) {
all $options all $options

View File

@ -35,12 +35,12 @@ var extensions = [
wmx wmx
] ]
fn -files [m]{ fn -files {|m|
var type = (condition:cset (eq $m '') prefix deep-prefix) var type = (condition:cset (eq $m '') prefix deep-prefix)
file:match-extensions &type=$type $m $@extensions file:match-extensions &type=$type $m $@extensions
} }
fn complete [@argv]{ fn complete {|@argv|
var c = (count $argv) var c = (count $argv)
if (== $c 2) { if (== $c 2) {
put --speed put --speed

View File

@ -2,31 +2,31 @@ use moi/util/condition
use moi/util/file use moi/util/file
use moi/util/list use moi/util/list
fn -local-packages { pacman -Q | eawk [_ p @_]{ put $p } } fn -local-packages { pacman -Q | eawk {|_ p @_| put $p } }
fn -repo-packages { fn -repo-packages {
var packages = [(pacman -Ss | list:ploop &step=2 [_ v]{ var packages = [(pacman -Ss | list:ploop &step=2 {|_ v|
put $v put $v
} | eawk [_ p @_]{ } | eawk {|_ p @_|
put $p put $p
})] })]
var spackages = [&] var spackages = [&]
use str use str
peach [p]{ peach {|p|
str:split '/' $p str:split '/' $p
} $packages | peach [e]{ } $packages | peach {|e|
set spackages[$e] = $nil set spackages[$e] = $nil
} }
keys $spackages keys $spackages
all $packages all $packages
} }
fn -downloaded-packages [m]{ fn -downloaded-packages {|m|
var type = (condition:cset (eq $m '') prefix deep-prefix) var type = (condition:cset (eq $m '') prefix deep-prefix)
file:match-extensions &type=$type $m tar.zst tar.xz tar.gz tar.bz2 file:match-extensions &type=$type $m tar.zst tar.xz tar.gz tar.bz2
} }
fn -local-file [m]{ edit:complete-filename $m } fn -local-file {|m| edit:complete-filename $m }
var options = [ var options = [
-h -h
@ -81,7 +81,7 @@ var fpack = [
-Qo -Qo
] ]
fn complete [@argv]{ fn complete {|@argv|
var c = (count $argv) var c = (count $argv)
if (< $c 3) { if (< $c 3) {
all $options all $options

View File

@ -16,15 +16,15 @@ var names = [
] ]
fn -kh { fn -kh {
cat ~/.ssh/known_hosts | peach [l]{ cat ~/.ssh/known_hosts | peach {|l|
put [(str:split ' ' $l)] put [(str:split ' ' $l)]
} | peach [e]{ } | peach {|e|
var domains @_ = $@e var domains @_ = $@e
str:split ',' $domains str:split ',' $domains
} }
} }
fn -port [cmd @argv]{ fn -port {|cmd @argv|
var o = (condition:cset (eq $cmd 'ssh') '-p' '-P') var o = (condition:cset (eq $cmd 'ssh') '-p' '-P')
var margs = (option:map $argv) var margs = (option:map $argv)
var p = (map:value-of $margs $o &default=[]) var p = (map:value-of $margs $o &default=[])
@ -35,24 +35,24 @@ fn -port [cmd @argv]{
} }
} }
fn -complete-names []{ each [n]{ put $n'@' } $names } fn -complete-names {|| each {|n| put $n'@' } $names }
fn -complete-domains [name hosts]{ fn -complete-domains {|name hosts|
each [h]{ each {|h|
put $name@$h put $name@$h
} $hosts } $hosts
} }
fn -complete-remote-dir [port address dir]{ fn -complete-remote-dir {|port address dir|
try { try {
ssh -p $port $address 'ls '$dir | each [f]{ ssh -p $port $address 'ls '$dir | each {|f|
put $address':'$dir$f put $address':'$dir$f
} }
} except e { } except e {
} }
} }
fn -complete-args [hosts cmd @argv]{ fn -complete-args {|hosts cmd @argv|
var arg = $argv[-1] var arg = $argv[-1]
var i = (str:index $arg @) var i = (str:index $arg @)
if (< $i 0) { if (< $i 0) {
@ -87,7 +87,7 @@ fn -complete-args [hosts cmd @argv]{
-complete-domains $n $hosts -complete-domains $n $hosts
} }
fn complete [@argv]{ fn complete {|@argv|
var @hosts = (-kh) var @hosts = (-kh)
var cmd = $argv[0] var cmd = $argv[0]
var is-ssh = (eq $cmd ssh) var is-ssh = (eq $cmd ssh)

View File

@ -1,4 +1,4 @@
fn complete [@argv]{ fn complete {|@argv|
if (and (> (count $argv) 2) (has-key $edit:completion:arg-completer $argv[1])) { if (and (> (count $argv) 2) (has-key $edit:completion:arg-completer $argv[1])) {
$edit:completion:arg-completer[$argv[1]] (all $argv[1:]) $edit:completion:arg-completer[$argv[1]] (all $argv[1:])
} else { } else {

View File

@ -12,13 +12,13 @@ var builtin_modules = [
unix unix
] ]
set edit:completion:arg-completer[use] = [@argv]{ set edit:completion:arg-completer[use] = {|@argv|
use str use str
use path use path
all $builtin_modules all $builtin_modules
put $libdir/**.elv | each [f]{ put $libdir/**.elv | each {|f|
if (path:is-regular $f) { if (path:is-regular $f) {
str:trim-prefix $f $libdir/ str:trim-prefix $f $libdir/
} }
} | each [f]{ str:trim-suffix $f .elv } } | each {|f| str:trim-suffix $f .elv }
} }

View File

@ -1,7 +1,7 @@
edit:add-var ++~ [n]{ + $n 1 } edit:add-var ++~ {|n| + $n 1 }
edit:add-var --~ [n]{ + $n 1 } edit:add-var --~ {|n| + $n 1 }
edit:add-var sign~ [n]{ edit:add-var sign~ {|n|
if (> $n 0) { if (> $n 0) {
put 1 put 1
} elif (< $n 0) { } elif (< $n 0) {
@ -10,8 +10,8 @@ edit:add-var sign~ [n]{
put 0 put 0
} }
} }
edit:add-var neg~ [n]{ * $n -1 } edit:add-var neg~ {|n| * $n -1 }
edit:add-var abs~ [n]{ edit:add-var abs~ {|n|
if (< $n 0) { if (< $n 0) {
neg $n neg $n
} else { } else {
@ -19,18 +19,18 @@ edit:add-var abs~ [n]{
} }
} }
edit:add-var sum~ [@numbers]{ edit:add-var sum~ {|@numbers|
var s = 0 var s = 0
each [e]{ set s = (+ $s e) } (to-list $numbers) each {|e| set s = (+ $s e) } (to-list $numbers)
} }
fn -minmax [t @numbers]{ fn -minmax {|t @numbers|
var l = (to-list $@numbers) var l = (to-list $@numbers)
if (== (count $l) 0) { if (== (count $l) 0) {
return return
} }
var m = $l[0] var m = $l[0]
each [n]{ each {|n|
if ($t $n $m) { if ($t $n $m) {
set m = $n set m = $n
} }
@ -38,5 +38,5 @@ fn -minmax [t @numbers]{
put $m put $m
} }
edit:add-var min~ [@numbers]{ -minmax $'<~' $@numbers } edit:add-var min~ {|@numbers| -minmax $'<~' $@numbers }
edit:add-var max~ [@numbers]{ -minmax $'>~' $@numbers } edit:add-var max~ {|@numbers| -minmax $'>~' $@numbers }

View File

@ -1,4 +1,4 @@
fn cset [c t f]{ fn cset {|c t f|
if $c { if $c {
put $t put $t
} else { } else {
@ -6,11 +6,11 @@ fn cset [c t f]{
} }
} }
fn mset [c t f]{ fn mset {|c t f|
all (cset $c $t $f) all (cset $c $t $f)
} }
fn call [c t f @argv]{ fn call {|c t f @argv|
var v = (cset $c $t $f) var v = (cset $c $t $f)
if (is (kind-of $v) fn) { if (is (kind-of $v) fn) {
$v $@argv $v $@argv

View File

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

View File

@ -1,16 +1,16 @@
use re use re
use str use str
fn is-ipv4 [arg]{ fn is-ipv4 {|arg|
if (not (re:match '^([0-9]{1,3}\.){3}[0-9]{1,3}$' $arg)) { if (not (re:match '^([0-9]{1,3}\.){3}[0-9]{1,3}$' $arg)) {
put $false put $false
} else { } else {
use ./list use ./list
not (str:split '.' $arg | list:contains [p]{ > $p 255 }) not (str:split '.' $arg | list:contains {|p| > $p 255 })
} }
} }
fn is-ipv6 [arg]{ fn is-ipv6 {|arg|
var p = '[0-9a-fA-F]{1,4}' var p = '[0-9a-fA-F]{1,4}'
var g1 g2 = '('$p':)' '(:'$p')' var g1 g2 = '('$p':)' '(:'$p')'
var cases = [ var cases = [
@ -29,27 +29,27 @@ fn is-ipv6 [arg]{
put (re:match $r $arg) put (re:match $r $arg)
} }
fn is-ip [arg]{ or (is-ipv4 $arg) (is-ipv6 $arg) } fn is-ip {|arg| or (is-ipv4 $arg) (is-ipv6 $arg) }
fn -long-part6 [p]{ fn -long-part6 {|p|
use str use str
set p = (str:to-lower $p) set p = (str:to-lower $p)
var c = (- 4 (count $p)) var c = (- 4 (count $p))
put (repeat $c '0' | str:join '')$p put (repeat $c '0' | str:join '')$p
} }
fn -middle-part6 [p]{ fn -middle-part6 {|p|
while (and (> (count $p) 1) (eq $p[0] 0)) { while (and (> (count $p) 1) (eq $p[0] 0)) {
set p = $p[1..] set p = $p[1..]
} }
put $p put $p
} }
fn -find-max0 [parts]{ fn -find-max0 {|parts|
use ./list use ./list
var idx s = -1 1 var idx s = -1 1
var ci cs f = -1 0 $false var ci cs f = -1 0 $false
list:loop [i p]{ list:loop {|i p|
if (eq $p 0) { if (eq $p 0) {
condition:call $f { set cs = (+ $cs 1) } { set ci cs f = $i 1 $true } condition:call $f { set cs = (+ $cs 1) } { set ci cs f = $i 1 $true }
} elif $f { } elif $f {
@ -65,7 +65,7 @@ fn -find-max0 [parts]{
put $idx $s put $idx $s
} }
fn long6 [ip]{ fn long6 {|ip|
if (not (is-ipv6 $ip)) { if (not (is-ipv6 $ip)) {
fail 'Not an IPv6' fail 'Not an IPv6'
} }
@ -82,11 +82,11 @@ fn long6 [ip]{
str:split ':' $ip | each $-long-part6~ | str:join ':' str:split ':' $ip | each $-long-part6~ | str:join ':'
} }
fn middle6 [ip]{ fn middle6 {|ip|
str:split ':' (long6 $ip) | each $-middle-part6~ | str:join ':' str:split ':' (long6 $ip) | each $-middle-part6~ | str:join ':'
} }
fn short6 [ip]{ fn short6 {|ip|
var parts = [(str:split ':' (middle6 $ip))] var parts = [(str:split ':' (middle6 $ip))]
var i s = (-find-max0 $parts) var i s = (-find-max0 $parts)
if (>= $i 0) { if (>= $i 0) {
@ -102,13 +102,13 @@ fn short6 [ip]{
str:join ':' $parts str:join ':' $parts
} }
fn is-ip [arg]{ or (is-ipv4 $arg) (is-ipv6 $arg) } fn is-ip {|arg| or (is-ipv4 $arg) (is-ipv6 $arg) }
fn -cmp [e1 e2]{ fn -cmp {|e1 e2|
use ./list use ./list
use ./condition use ./condition
var c = 0 var c = 0
list:loop [i p1]{ list:loop {|i p1|
var p2 = $e2[$i] var p2 = $e2[$i]
set c = (condition:cset (< $p1 $p2) -1 (condition:cset (> $p1 $p2) 1 0)) set c = (condition:cset (< $p1 $p2) -1 (condition:cset (> $p1 $p2) 1 0))
if (!= $c 0) { if (!= $c 0) {
@ -118,21 +118,21 @@ fn -cmp [e1 e2]{
put $c put $c
} }
fn cmp4 [ip1 ip2]{ fn cmp4 {|ip1 ip2|
if (or (not (is-ipv4 $ip1)) (not (is-ipv4 $ip2))) { if (or (not (is-ipv4 $ip1)) (not (is-ipv4 $ip2))) {
fail 'Not an IPv4 '$ip1' → '$ip2 fail 'Not an IPv4 '$ip1' → '$ip2
} }
-cmp [(str:split . $ip1)] [(str:split . $ip2)] -cmp [(str:split . $ip1)] [(str:split . $ip2)]
} }
fn cmp6 [ip1 ip2]{ fn cmp6 {|ip1 ip2|
if (or (not (is-ipv6 $ip1)) (not (is-ipv6 $ip2))) { if (or (not (is-ipv6 $ip1)) (not (is-ipv6 $ip2))) {
fail 'Not an IPv6: '$ip1' → '$ip2 fail 'Not an IPv6: '$ip1' → '$ip2
} }
-cmp [(str:split : (middle6 $ip1))] [(str:split : (middle6 $ip2))] -cmp [(str:split : (middle6 $ip1))] [(str:split : (middle6 $ip2))]
} }
fn cmp [ip1 ip2]{ fn cmp {|ip1 ip2|
if (is-ipv4 $ip1) { if (is-ipv4 $ip1) {
if (is-ipv4 $ip2) { if (is-ipv4 $ip2) {
cmp4 $ip1 $ip2 cmp4 $ip1 $ip2

View File

@ -1,27 +1,27 @@
use builtin use builtin
use str use str
fn -p [@argv]{ fn -p {|@argv|
var c = (count $argv) var c = (count $argv)
if (== $c 0) { if (== $c 0) {
put [ (all) ] put [ (all) ]
} elif (== $c 1) { } elif (== $c 1) {
put $argv[0] put $argv[0]
} else { } else {
fail '0 or 1 argument needed given '$c' arguments: ['(each [e]{ put '"'$e'"' } $argv | str:join ', ')']' fail '0 or 1 argument needed given '$c' arguments: ['(each {|e| put '"'$e'"' } $argv | str:join ', ')']'
} }
} }
fn -r [step begin end]{ fn -r {|step begin end|
if (> $step 0) { if (> $step 0) {
builtin:range &step=$step $begin (+ $end 1) builtin:range &step=$step $begin (+ $end 1)
} else { } else {
var d = (+ $begin $end) var d = (+ $begin $end)
builtin:range &step=(* $step -1) $end (+ $begin 1) | each [e]{ - $d $e } builtin:range &step=(* $step -1) $end (+ $begin 1) | each {|e| - $d $e }
} }
} }
fn to-list [@argv]{ fn to-list {|@argv|
try { try {
-p $@argv -p $@argv
} except e { } except e {
@ -29,7 +29,7 @@ fn to-list [@argv]{
} }
} }
fn range [&step=1 b @r]{ fn range {|&step=1 b @r|
if (== $step 0) { if (== $step 0) {
fail 'bad value: step must be positive, but is 0' fail 'bad value: step must be positive, but is 0'
} }
@ -48,7 +48,7 @@ fn range [&step=1 b @r]{
-r $step $b $e -r $step $b $e
} }
fn indexes [&from=$false &step=1 @argv]{ fn indexes {|&from=$false &step=1 @argv|
if (== $step 0) { if (== $step 0) {
fail 'bad value: step must be positive, but is 0' fail 'bad value: step must be positive, but is 0'
} }
@ -74,16 +74,16 @@ fn indexes [&from=$false &step=1 @argv]{
} }
} }
fn -for [&from=$false &step=1 @argv]{ fn -for {|&from=$false &step=1 @argv|
var l = (-p $@argv) var l = (-p $@argv)
indexes &from=$from &step=$step $l | each [i]{ put [ $i $l[$i] ] } indexes &from=$from &step=$step $l | each {|i| put [ $i $l[$i] ] }
} }
fn loop [&from=$false &step=1 &end=$false cb @argv]{ fn loop {|&from=$false &step=1 &end=$false cb @argv|
if (not $end) { if (not $end) {
-for &from=$from &step=$step $@argv | each [e]{ $cb $@e } -for &from=$from &step=$step $@argv | each {|e| $cb $@e }
} else { } else {
-for &from=$from &step=$step $@argv | each [e]{ -for &from=$from &step=$step $@argv | each {|e|
var i v = $@e var i v = $@e
if ($end $i $v) { if ($end $i $v) {
break break
@ -92,25 +92,25 @@ fn loop [&from=$false &step=1 &end=$false cb @argv]{
} }
} }
} }
fn ploop [&from=$false &step=1 cb @argv]{ fn ploop {|&from=$false &step=1 cb @argv|
-for &from=$from &step=$step $@argv | peach [e]{ $cb $@e } -for &from=$from &step=$step $@argv | peach {|e| $cb $@e }
} }
fn reverse [@argv]{ loop &step=-1 [_ e]{ put $e } $@argv } fn reverse {|@argv| loop &step=-1 {|_ e| put $e } $@argv }
fn reach [cb @argv]{ reverse $@argv | each [e]{ cb $e } } fn reach {|cb @argv| reverse $@argv | each {|e| cb $e } }
fn empty [@argv]{ == (count (-p $@argv)) 0 } fn empty {|@argv| == (count (-p $@argv)) 0 }
fn not-empty [@argv]{ not (empty $@argv) } fn not-empty {|@argv| not (empty $@argv) }
fn filter [cb @argv]{ fn filter {|cb @argv|
each [v]{ each {|v|
if ($cb $v) { if ($cb $v) {
put $v put $v
} }
} (-p $@argv) } (-p $@argv)
} }
fn filter-not [cb @argv]{ filter [v]{ not ($cb $v) } $@argv } fn filter-not {|cb @argv| filter {|v| not ($cb $v) } $@argv }
fn first [&from=$false &reverse=$false cb @argv]{ fn first {|&from=$false &reverse=$false cb @argv|
var f = [v]{ var f = {|v|
if ($cb $v) { if ($cb $v) {
put $v put $v
break break
@ -148,21 +148,21 @@ fn first [&from=$false &reverse=$false cb @argv]{
} }
} }
fn filter-index [cb @argv]{ fn filter-index {|cb @argv|
loop [i v]{ loop {|i v|
if ($cb $v) { if ($cb $v) {
put $i put $i
} }
} $@argv } $@argv
} }
fn filter-index-not [cb @argv]{ filter-index [v]{ not ($cb $v) } $@argv } fn filter-index-not {|cb @argv| filter-index {|v| not ($cb $v) } $@argv }
fn first-index [&from=$false &reverse=$false cb @argv]{ fn first-index {|&from=$false &reverse=$false cb @argv|
var idx = -1 var idx = -1
var step = 1 var step = 1
if $reverse { if $reverse {
set step = -1 set step = -1
} }
loop &from=$from &step=$step [i v]{ loop &from=$from &step=$step {|i v|
if ($cb $v) { if ($cb $v) {
set idx = $i set idx = $i
break break
@ -171,20 +171,20 @@ fn first-index [&from=$false &reverse=$false cb @argv]{
put $idx put $idx
} }
fn search [cb @argv]{ fn search {|cb @argv|
loop [i v]{ loop {|i v|
if ($cb $i $v) { if ($cb $i $v) {
put $v put $v
} }
} $@argv } $@argv
} }
fn search-not [cb @argv]{ search [i v]{ not ($cb $i $v) } $@argv } fn search-not {|cb @argv| search {|i v| not ($cb $i $v) } $@argv }
fn search-first [&from=$false &reverse=$false cb @argv]{ fn search-first {|&from=$false &reverse=$false cb @argv|
var step = 1 var step = 1
if $reverse { if $reverse {
set step = -1 set step = -1
} }
loop &from=$from &step=$step [i v]{ loop &from=$from &step=$step {|i v|
if ($cb $i $v) { if ($cb $i $v) {
put $v put $v
break break
@ -192,21 +192,21 @@ fn search-first [&from=$false &reverse=$false cb @argv]{
} $@argv } $@argv
} }
fn search-index [cb @argv]{ fn search-index {|cb @argv|
loop [i v]{ loop {|i v|
if ($cb $i $v) { if ($cb $i $v) {
put $i put $i
} }
} $@argv } $@argv
} }
fn search-index-not [cb @argv]{ search-index [i v]{ not ($cb $i $v) } $@argv } fn search-index-not {|cb @argv| search-index {|i v| not ($cb $i $v) } $@argv }
fn search-first-index [&from=$false &reverse=$false cb @argv]{ fn search-first-index {|&from=$false &reverse=$false cb @argv|
var idx = -1 var idx = -1
var step = 1 var step = 1
if $reverse { if $reverse {
set step = -1 set step = -1
} }
loop &from=$from &step=$step [i v]{ loop &from=$from &step=$step {|i v|
if ($cb $i $v) { if ($cb $i $v) {
set idx = $i set idx = $i
break break
@ -215,9 +215,9 @@ fn search-first-index [&from=$false &reverse=$false cb @argv]{
put $idx put $idx
} }
fn contains [cb @argv]{ fn contains {|cb @argv|
var e = $false var e = $false
each [v]{ each {|v|
if ($cb $v) { if ($cb $v) {
set e = $true set e = $true
break break
@ -225,11 +225,11 @@ fn contains [cb @argv]{
} (-p $@argv) } (-p $@argv)
put $e put $e
} }
fn contains-not [cb @argv]{ contains [v]{ not ($cb $v) } $@argv } fn contains-not {|cb @argv| contains {|v| not ($cb $v) } $@argv }
fn exists [cb @argv]{ fn exists {|cb @argv|
var e = $false var e = $false
loop [i v]{ loop {|i v|
if ($cb $i $v) { if ($cb $i $v) {
set e = $true set e = $true
break break
@ -237,38 +237,38 @@ fn exists [cb @argv]{
} $@argv } $@argv
put $e put $e
} }
fn exists-not [cb @argv]{ exists [i v]{ $cb $i $v } $@argv } fn exists-not {|cb @argv| exists {|i v| $cb $i $v } $@argv }
fn includes [v @argv]{ contains [e]{ is $v $e } $@argv } fn includes {|v @argv| contains {|e| is $v $e } $@argv }
fn includes-not [v @argv]{ contains-not [e]{ is $v $e } $@argv } fn includes-not {|v @argv| contains-not {|e| is $v $e } $@argv }
fn reduce [v cb @argv]{ fn reduce {|v cb @argv|
each [e]{ set v = ($cb $v $e) } (to-list $@argv) each {|e| set v = ($cb $v $e) } (to-list $@argv)
put $v put $v
} }
fn remove-duplicate [@argv]{ fn remove-duplicate {|@argv|
var done = [&] var done = [&]
each [v]{ each {|v|
put [&v=$v &e=(has-key $done $v)] put [&v=$v &e=(has-key $done $v)]
set done[$v] = $nil set done[$v] = $nil
} (-p $@argv) | each [v]{ } (-p $@argv) | each {|v|
assoc $v k (not $v[e]) assoc $v k (not $v[e])
} | each [v]{ } | each {|v|
if $v[k] { if $v[k] {
put $v[v] put $v[v]
} }
} }
} }
fn premove-duplicate [@argv]{ fn premove-duplicate {|@argv|
var done = [&] var done = [&]
peach [v]{ peach {|v|
set done[$v] = $nil set done[$v] = $nil
} (-p $@argv) } (-p $@argv)
keys $done keys $done
} }
fn swap [i j @argv]{ fn swap {|i j @argv|
var l = (-p $@argv) var l = (-p $@argv)
var c = (count $l) var c = (count $l)
set i j = (-i $i $c) (-i $j $c) set i j = (-i $i $c) (-i $j $c)
@ -289,8 +289,8 @@ fn swap [i j @argv]{
} }
} }
fn less [cb]{ fn less {|cb|
var l = [a b]{ < ($cb $a $b) 0 } var l = {|a b| < ($cb $a $b) 0 }
put $l put $l
} }
fn sort [cb @argv]{ order &less-than=(less $cb) (-p $@argv) } fn sort {|cb @argv| order &less-than=(less $cb) (-p $@argv) }

View File

@ -1,9 +1,9 @@
fn empty [container]{ == (count $container) 0 } fn empty {|container| == (count $container) 0 }
fn values [container]{ keys $container | each [v]{ put $container[$v] } } fn values {|container| keys $container | each {|v| put $container[$v] } }
fn pvalues [container]{ keys $container | peach [v]{ put $container[$v] } } fn pvalues {|container| keys $container | peach {|v| put $container[$v] } }
fn value-of [container k &default=$nil]{ fn value-of {|container k &default=$nil|
if (has-key $container $k) { if (has-key $container $k) {
put $container[$k] put $container[$k]
} else { } else {
@ -11,54 +11,54 @@ fn value-of [container k &default=$nil]{
} }
} }
fn unzip [container]{ fn unzip {|container|
var lkeys lvalues = [] [] var lkeys lvalues = [] []
keys $container | each [k]{ keys $container | each {|k|
set lkeys = [(all $lkeys) $k] set lkeys = [(all $lkeys) $k]
set lvalues = [(all $lvalues) $container[$k]] set lvalues = [(all $lvalues) $container[$k]]
} }
put $lkeys $lvalues put $lkeys $lvalues
} }
fn zip [lkeys lvalues]{ fn zip {|lkeys lvalues|
var ck cv = (count $lkeys) (count $lvalues) var ck cv = (count $lkeys) (count $lvalues)
var c = $ck var c = $ck
if (> $ck $cv) { if (> $ck $cv) {
set c = $cv set c = $cv
} }
var result = [&] var result = [&]
range $c | peach [i]{ range $c | peach {|i|
put [&k=$lkeys[$i] &i=$i] put [&k=$lkeys[$i] &i=$i]
} | peach [e]{ } | peach {|e|
assoc $e v $lvalues[$e[i]] assoc $e v $lvalues[$e[i]]
} | each [e]{ } | each {|e|
set result[$e[k]] = $e[v] set result[$e[k]] = $e[v]
} }
put $result put $result
} }
fn to-map [@argv]{ fn to-map {|@argv|
use ./list use ./list
var container = (list:to-list $@argv) var container = (list:to-list $@argv)
zip [(range (count $container))] $container zip [(range (count $container))] $container
} }
fn to-set [@argv]{ fn to-set {|@argv|
use ./list use ./list
var m = [&] var m = [&]
each [k]{ set m[$k] = $nil } (list:to-list $@argv) each {|k| set m[$k] = $nil } (list:to-list $@argv)
put $m put $m
} }
fn mdissoc [container @argv]{ fn mdissoc {|container @argv|
each [k]{ each {|k|
set container = (dissoc $container $k) set container = (dissoc $container $k)
} $argv } $argv
put $container put $container
} }
fn massoc [&replace=$true container @argv]{ fn massoc {|&replace=$true container @argv|
each [e]{ each {|e|
var k v = $@e var k v = $@e
if (or $replace (not (has-key $container $k))) { if (or $replace (not (has-key $container $k))) {
set container[$k] = $v set container[$k] = $v
@ -67,7 +67,7 @@ fn massoc [&replace=$true container @argv]{
put $container put $container
} }
fn add [container key @values]{ fn add {|container key @values|
use ./list use ./list
var values = (list:to-list $values) var values = (list:to-list $values)
if (has-key $container $key) { if (has-key $container $key) {
@ -78,17 +78,17 @@ fn add [container key @values]{
put $container put $container
} }
fn join [container container2]{ fn join {|container container2|
keys $container2 | each [k]{ keys $container2 | each {|k|
set container[$k] = $container2[$k] set container[$k] = $container2[$k]
} }
put $container put $container
} }
fn sorted-keys [&reverse=$false &less-than=$nil container]{ fn sorted-keys {|&reverse=$false &less-than=$nil container|
keys $container | order &reverse=$reverse &less-than=$less-than keys $container | order &reverse=$reverse &less-than=$less-than
} }
fn sorted-values [&reverse=$false &less-than=$nil container]{ fn sorted-values {|&reverse=$false &less-than=$nil container|
values $container | order &reverse=$reverse &less-than=$less-than values $container | order &reverse=$reverse &less-than=$less-than
} }

View File

@ -2,14 +2,14 @@ use re
use str use str
use ./list use ./list
fn is-moptions [o]{ re:match '^\-\w{2,}$' $o } fn is-moptions {|o| re:match '^\-\w{2,}$' $o }
fn is-soption [o]{ re:match '^\-\w$' $o } fn is-soption {|o| re:match '^\-\w$' $o }
fn is-loption [o]{ re:match '^\-\-\w+(=.*)?$' $o } fn is-loption {|o| re:match '^\-\-\w+(=.*)?$' $o }
fn is-option [o]{ or (is-soption $o) (is-loption $o) (is-moptions $o) } fn is-option {|o| or (is-soption $o) (is-loption $o) (is-moptions $o) }
fn -splitm [o]{ each [e]{ put -$e } $o[1:] } fn -splitm {|o| each {|e| put -$e } $o[1:] }
fn -splitl [o]{ fn -splitl {|o|
if (str:contains $o '=') { if (str:contains $o '=') {
var opt @value = (str:split '=' $o) var opt @value = (str:split '=' $o)
put $opt put $opt
@ -19,7 +19,7 @@ fn -splitl [o]{
} }
} }
fn split [o]{ fn split {|o|
if (is-moptions $o) { if (is-moptions $o) {
-splitm $o -splitm $o
} elif (is-loption $o) { } elif (is-loption $o) {
@ -29,17 +29,17 @@ fn split [o]{
} }
} }
fn -joins [opt]{ fn -joins {|opt|
var o = (each [o]{ put $o[1:] } $opt | str:join '') var o = (each {|o| put $o[1:] } $opt | str:join '')
if (not-eq $o '') { put -$o } if (not-eq $o '') { put -$o }
} }
fn join [@opt]{ fn join {|@opt|
var opt = (list:-p $@opt) var opt = (list:-p $@opt)
var cb = [o]{ or (is-soption $o) (is-moptions $o) } var cb = {|o| or (is-soption $o) (is-moptions $o) }
var idx = [(list:filter-index-not $cb $opt)] var idx = [(list:filter-index-not $cb $opt)]
var i0 = 0 var i0 = 0
each [i]{ each {|i|
-joins $opt[{$i0}:$i] -joins $opt[{$i0}:$i]
put $opt[$i] put $opt[$i]
set i0 = (+ $i 1) set i0 = (+ $i 1)
@ -47,9 +47,9 @@ fn join [@opt]{
-joins $opt[{$i0}:] -joins $opt[{$i0}:]
} }
fn expand [@argv]{ each $split~ (list:-p $@argv) } fn expand {|@argv| each $split~ (list:-p $@argv) }
fn map [@argv]{ fn map {|@argv|
use ./map use ./map
set argv = [(expand $@argv)] set argv = [(expand $@argv)]
var result i c = [&] 0 (count $argv) var result i c = [&] 0 (count $argv)

View File

@ -1,11 +1,11 @@
use str use str
use ./list use ./list
fn -repeat [n s]{ fn -repeat {|n s|
repeat $n $s | str:join '' repeat $n $s | str:join ''
} }
fn left [size s]{ fn left {|size s|
var ss = (count $s) var ss = (count $s)
var d = (- $size $ss) var d = (- $size $ss)
if (<= $d 0) { if (<= $d 0) {
@ -15,7 +15,7 @@ fn left [size s]{
} }
} }
fn right [size s]{ fn right {|size s|
var ss = (count $s) var ss = (count $s)
var d = (- $size $ss) var d = (- $size $ss)
if (<= $d 0) { if (<= $d 0) {
@ -25,7 +25,7 @@ fn right [size s]{
} }
} }
fn center [size s]{ fn center {|size s|
use math use math
var ss = (count $s) var ss = (count $s)
var d = (- $size $ss) var d = (- $size $ss)
@ -40,12 +40,12 @@ fn center [size s]{
} }
} }
fn -c2map [col]{ fn -c2map {|col|
if (not-eq (kind-of $col) list) { if (not-eq (kind-of $col) list) {
put [] put []
return return
} }
list:loop [i c]{ list:loop {|i c|
var k = (kind-of $c) var k = (kind-of $c)
var cf = [&min=0] var cf = [&min=0]
if (is $k string) { if (is $k string) {
@ -63,19 +63,19 @@ fn -c2map [col]{
put $col put $col
} }
fn -parsecols [col &split=$false lst]{ fn -parsecols {|col &split=$false lst|
set col = (-c2map $col) set col = (-c2map $col)
var cc lines = (count $col) $lst var cc lines = (count $col) $lst
if $split { if $split {
set lines = [ (eawk [_ @args]{ put $args } $lst) ] set lines = [ (eawk {|_ @args| put $args } $lst) ]
} }
each [l]{ each {|l|
var cl = (count $l) var cl = (count $l)
if (> $cl $cc) { if (> $cl $cc) {
set col = [ $@col (range (- $cl $cc) | each [_]{ put [&align=left &min=0] }) ] set col = [ $@col (range (- $cl $cc) | each {|_| put [&align=left &min=0] }) ]
set cc = $cl set cc = $cl
} }
list:loop [i c]{ list:loop {|i c|
var ccl = (+ (count $c) 1) var ccl = (+ (count $c) 1)
if (> $ccl $col[$i][min]) { if (> $ccl $col[$i][min]) {
set col[$i][min] = $ccl set col[$i][min] = $ccl
@ -85,7 +85,7 @@ fn -parsecols [col &split=$false lst]{
put $col put $col
} }
fn -format [f e]{ fn -format {|f e|
var cb = $left~ var cb = $left~
if (has-key $f align) { if (has-key $f align) {
var a = $f[align] var a = $f[align]
@ -98,17 +98,17 @@ fn -format [f e]{
$cb $f[min] $e $cb $f[min] $e
} }
fn -formatline [col line]{ fn -formatline {|col line|
list:loop [i v]{ list:loop {|i v|
-format $col[$i] $v -format $col[$i] $v
} $line | str:join '' } $line | str:join ''
} }
fn format [&col=[] &split=$false @lst]{ fn format {|&col=[] &split=$false @lst|
var lines = (list:-p $@lst) var lines = (list:-p $@lst)
if $split { if $split {
set lines = [ (eawk [l @args]{ put $args } $lines) ] set lines = [ (eawk {|l @args| put $args } $lines) ]
} }
set col = (-parsecols $col $lines) set col = (-parsecols $col $lines)
each [l]{ -formatline $col $l } $lines each {|l| -formatline $col $l } $lines
} }