diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..285352f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +lib/github.com +private diff --git a/aliases/addbadips.elv b/aliases/addbadips.elv new file mode 100644 index 0000000..2f7624d --- /dev/null +++ b/aliases/addbadips.elv @@ -0,0 +1,26 @@ +edit:add-var addbadips~ [@argv]{ + use moi/util/ip + use moi/util/list + use moi/util/map + use str + + local:f = ~/bad_ips.txt + local:ips = [ (cat $f) ] + local:m = (map:to-set $ips) + local:c = 0 + each [e]{ + local:cond = (and (not (has-key $m $e)) (ip:is-ipv4 $e)) + if $cond { + echo $e ajoutée + c = (+ $c 1) + m[$e] = $nil + ips = [ (all $ips) $e ] + } + } $argv + if (> $c 0) { + ips = [ (list:sort $ip:cmp4~ $ips) ] + } + local:result = (str:join "\n" $ips) + echo $result > $f + echo $c' IPs ajoutées ('(count $ips)' au total)' +} diff --git a/aliases/adguard.elv b/aliases/adguard.elv new file mode 100644 index 0000000..9d4d915 --- /dev/null +++ b/aliases/adguard.elv @@ -0,0 +1,11 @@ +edit:add-var adguard~ [@argv]{ + use str + sudo chattr -i /etc/resolv.conf + local:beg = 's|^.*\(192\.168\.0\.25\)|' + local:end = 'nameserver \1|' + if (and (> (count $argv) 0) (str:has-prefix $argv[0] u)) { + end = '# '$end + } + sudo sed -i $beg$end /etc/resolv.conf + sudo chattr +i /etc/resolv.conf +} diff --git a/aliases/aliases.elv b/aliases/aliases.elv new file mode 100644 index 0000000..5c36e35 --- /dev/null +++ b/aliases/aliases.elv @@ -0,0 +1,16 @@ +edit:add-var cat~ [@argv]{ e:bat -pp --tabs 2 $@argv } +edit:add-var cp~ [@argv]{ e:cp -iv $@argv } +edit:add-var curl~ $e:curlie~ +edit:add-var df~ $e:duf~ +edit:add-var diff~ $e:colordiff~ +edit:add-var dig~ [@argv]{ e:dig +noall +answer $@argv } +edit:add-var dogall~ [@argv]{ e:dog A AAAA CNAME $@argv } +edit:add-var du~ [@argv]{ e:du -h $@argv } +edit:add-var git~ $e:hub~ +edit:add-var grep~ [@argv]{ e:grep --color=tty -d skip $@argv } +edit:add-var mv~ [@argv]{ e:mv -iv $@argv } +edit:add-var pf~ [@argv]{ e:pkgfile -vri $@argv } +edit:add-var rm~ [@argv]{ e:rm -Iv $@argv } +edit:add-var rename~ $e:go-rename~ +edit:add-var timidity~ [@argv]{ e:timidity -a -K 1 $@argv } +edit:add-var tree~ [@argv]{ e:tree -puhC --dirsfirst --du -D $@argv } diff --git a/aliases/cleankh.elv b/aliases/cleankh.elv new file mode 100644 index 0000000..9a64ec9 --- /dev/null +++ b/aliases/cleankh.elv @@ -0,0 +1,36 @@ +edit:add-var cleankh~ [@argv]{ + use str + use moi/util/condition + use moi/util/list + use moi/util/ip + fn less [cmp e1 e2]{ < ($cmp $e1 $e2) 0 } + fn sort-host [e1 e2]{ + local:i1 = (condition:cset (ip:is-ip $e1) 1 0) + local:i2 = (condition:cset (ip:is-ip $e2) 1 0) + if (!= $i1 $i2) { + < $i1 $i2 + } elif (== $i1 0) { + ~/.ssh/known_hosts +} + diff --git a/aliases/commitkcp.elv b/aliases/commitkcp.elv new file mode 100644 index 0000000..235621e --- /dev/null +++ b/aliases/commitkcp.elv @@ -0,0 +1,20 @@ +edit:add-var commitkcp~ [@argv]{ + use str + local:m = [ + &name= + &ver= + ] + + keys $m | each [k]{ + local:p = 'pkg'$k'=' + m[$k] = (str:trim-prefix (cat PKGBUILD | grep $p | take 1) $p) + } + + local:msg = $m[name]' '$m[ver] + if (> (count $argv) 0) { + msg = $msg' - '(str:join ' ' $argv) + } + + echo "git commit -am '"$msg"' ; git push" + git commit -am $msg; git push +} diff --git a/aliases/get-uid.elv b/aliases/get-uid.elv new file mode 100644 index 0000000..6a09c4c --- /dev/null +++ b/aliases/get-uid.elv @@ -0,0 +1,49 @@ +use re +use str + +fn -value [v]{ + local:p = (re:find '^(\d+)\((\w+)\)$' $v) + if (and (has-key $p groups) (== (count $p[groups]) 3)) { + put [ + &number=$p[groups][1][text] + &text=$p[groups][2][text] + ] + } +} + +fn -values [v]{ + local:vv = [ (str:split ',' $v) ] + if (== (count $vv) 1) { + -value $vv[0] + } else { + put [ (each $-value~ $vv) ] + } +} + +fn -get-id []{ + local:id = [&] + str:split ' ' (id) | each [e]{ + local:p = [ (str:split '=' $e) ] + if (== (count $p) 2) { + id[$p[0]] = (-values $p[1]) + } + } + put $id +} + +fn -get [k]{ + local:id = (-get-id) + if (has-key $id $k) { + put $id[$k] + } +} + +fn -is-root []{ + == (-get uid)[number] 0 +} + +edit:add-var get-id~ $-get-id~ +edit:add-var get-uid~ []{ -get uid } +edit:add-var get-gid~ []{ -get gid } +edit:add-var get-groups~ []{ -get groups; -get groupes } +edit:add-var is-root~ $-is-root~ diff --git a/aliases/ipinfo.elv b/aliases/ipinfo.elv new file mode 100644 index 0000000..077d912 --- /dev/null +++ b/aliases/ipinfo.elv @@ -0,0 +1,19 @@ +edit:add-var ipinfo~ [@argv]{ + use moi/util/ip + use str + + local:token = (cat $E:HOME/.config/elvish/private/ipinfo_token) + + fn url-of [ip]{ + str:join '' [ 'ipinfo.io/' $ip '?token=' $token] + } + + fn info [ip]{ + if (ip:is-ip $ip) { + local:url = (url-of $ip) + echo 'URL: '$url + e:curlie $url + } + } + each $info~ $argv +} diff --git a/aliases/ipof.elv b/aliases/ipof.elv new file mode 100644 index 0000000..8577ca9 --- /dev/null +++ b/aliases/ipof.elv @@ -0,0 +1,104 @@ +edit:add-var ipof~ [@argv]{ + use str + use moi/util/condition + use moi/util/ip + + fn less [cmp e1 e2]{ < ($cmp $e1 $e2) 0 } + + fn sort-ip [e1 e2]{ + local:i1 = (condition:cset (ip:is-ipv4 $e1) 0 (condition:cset (ip:is-ipv6 $e1) 1 2)) + local:i2 = (condition:cset (ip:is-ipv4 $e2) 0 (condition:cset (ip:is-ipv6 $e2) 1 2)) + if (!= $i1 $i2) { + < $i1 $i2 + } elif (== $i1 2) { + (keys $solve[ips] | count) 0) { + remote[$d] = $solve + } + } $argv + + each [d]{ + local:exists = $false + echo (styled 'Resolving '$d'…' bright-green) + echo + if (has-key $hosts $d) { + exists = $true + echo (styled ' local:' bright-yellow) + keys $hosts[$d] | order &less-than=$sort-ip~ | each [ip]{ + echo ' - '$ip + } + } + if (and (has-key $remote $d) $remote[$d][remote]) { + exists = $true + local:info = $remote[$d] + echo (styled ' remote:' bright-yellow) + echo ' IPs:' + keys $info[ips] | order &less-than=$sort-ip~ | each [ip]{ + echo ' - '$ip + } + echo ' Resolvers: '$info[resolvers] + echo ' TXT records: '$info[txt] + } + if (not $exists) { + echo (styled 'No info found' bright-red) + } + echo + } $argv +} diff --git a/aliases/listarchiver.elv b/aliases/listarchiver.elv new file mode 100644 index 0000000..c394c3a --- /dev/null +++ b/aliases/listarchiver.elv @@ -0,0 +1,6 @@ +edit:add-var listarchiver~ [@argv]{ + peach [f]{ + local:f = $f + e:arc ls $f + } $argv +} diff --git a/aliases/pdf2grey.elv b/aliases/pdf2grey.elv new file mode 100644 index 0000000..c70e555 --- /dev/null +++ b/aliases/pdf2grey.elv @@ -0,0 +1,82 @@ +edit:add-var pdf2grey~ [@argv]{ + use re + use str + local:m = [ + &nb=$false + &split=50 + ] + + fn _help []{ + echo 'Usage: pdf2gray [-hn ()] .pdf .pdf' + echo '' + echo 'Options:' + echo ' -h Affiche cette aide' + 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]{ + re:match '.*\.pdf$' $e + } + + fn is_int [e]{ + re:match '^\d+$' $e + } + + fn is_split [e]{ + and (>= $e 0) (<= $e 100) + } + + while (> (count $argv) 0) { + local:e @argv = $@argv + if (or (eq $e '-h') (eq $e '--help')) { + _help + return + } elif (eq $e -n) { + m[nb] = $true + if (== (count $e) 0) { + echo 'Il manque des arguments après -s' + _help + return + } elif (is_int $argv[0]) { + e @argv = $@argv + if (is_split $e) { + m[split] = $e + } else { + echo 'L’argument (optionnel) après -s doit être compris entre 0 et 100' + _help + return + } + } + } elif (not (is_pdf $e)) { + echo 'Argument non reconnu: '$e + _help + return + } elif (not (has-key $m input)) { + m[input] = $e + } else { + m[output] = $e + if (> (count $argv) 0) { + echo 'Trop d’arguments: '$@argv + _help + return + } + } + } + if (not (has-key $m input)) { + echo 'Manque le input' + _help + return + } elif (not (has-key $m output)) { + echo 'Manque le output' + _help + return + } + + local:params = [-colorspace gray] + if $m[nb] { + @params = (all $params) -threshold $m[split]'%' + } + @params = $@params $m[input] $m[output] + echo 'Commande: convert '(str:join ' ' $params) + convert $@params +} diff --git a/aliases/sendsms.elv b/aliases/sendsms.elv new file mode 100644 index 0000000..f480868 --- /dev/null +++ b/aliases/sendsms.elv @@ -0,0 +1,11 @@ +edit:add-var sendsms~ [@argv]{ + use str + + local:api = https://smsapi.free-mobile.fr/sendmsg + local:param = (cat $E:HOME/.config/elvish/private/freesms.json | from-json) + local:msg = (str:join '%20' $argv) + + local:url = $api'?user='$param[user]'&pass='$param[password]'&msg='$msg + #put $url + curl $url +} diff --git a/aliases/translate.elv b/aliases/translate.elv new file mode 100644 index 0000000..6b3d8eb --- /dev/null +++ b/aliases/translate.elv @@ -0,0 +1,2 @@ +edit:add-var trsen~ [@argv]{ e:rlwrap trans -b -t en $@argv } +edit:add-var trsfr~ [@argv]{ e:rlwrap trans -b -t fr $@argv } diff --git a/aliases/vivaldiprofile.elv b/aliases/vivaldiprofile.elv new file mode 100644 index 0000000..9c424a5 --- /dev/null +++ b/aliases/vivaldiprofile.elv @@ -0,0 +1,16 @@ +fn -sync [d1 d2]{ + rsync -azh --delete --info=progress2,stats ~/.config/$d1/ ~/.config/$d2/ +} + +fn -save [d]{ + -sync $d $d.save +} + +fn -restore [d]{ + -sync $d.save $d +} + +edit:add-var viv-save~ []{ -save vivaldi } +edit:add-var viv-restore~ []{ -restore vivaldi } +edit:add-var vivd-save~ []{ -save vivaldi-developer } +edit:add-var vivd-restore~ []{ -restore vivaldi-developer } diff --git a/lib/moi/alias.elv b/lib/moi/alias.elv new file mode 100644 index 0000000..7983a7c --- /dev/null +++ b/lib/moi/alias.elv @@ -0,0 +1,6 @@ +local:dir = ~/.config/elvish/aliases + +for file [(_ = ?(put $dir/*.elv))] { + content = (cat $file | slurp) + eval $content +} diff --git a/lib/moi/completion/use.elv b/lib/moi/completion/use.elv new file mode 100644 index 0000000..b0f9923 --- /dev/null +++ b/lib/moi/completion/use.elv @@ -0,0 +1,24 @@ +local:libdir = $E:HOME/.config/elvish/lib +local:builtin_modules = [ + builtin + epm + file + math + path + re + readline-binding + store + str + unix +] + +edit:completion:arg-completer[use] = [@argv]{ + use str + use path + all $builtin_modules + put $libdir/**.elv | each [f]{ + if (path:is-regular $f) { + str:trim-prefix $f $libdir/ + } + } | each [f]{ str:trim-suffix $f .elv } +} diff --git a/lib/moi/util/common.elv b/lib/moi/util/common.elv new file mode 100644 index 0000000..4db8ef6 --- /dev/null +++ b/lib/moi/util/common.elv @@ -0,0 +1,42 @@ +edit:add-var ++~ [n]{ + $n 1 } +edit:add-var --~ [n]{ + $n 1 } + +edit:add-var sign~ [n]{ + if (> $n 0) { + put 1 + } elif (< $n 0) { + put -1 + } else { + put 0 + } +} +edit:add-var neg~ [n]{ * $n -1 } +edit:add-var abs~ [n]{ + if (< $n 0) { + neg $n + } else { + put $n + } +} + +edit:add-var sum~ [@numbers]{ + local:s = 0 + each [e]{ s = (+ $s e) } (to-list $numbers) +} + +fn -minmax [t @numbers]{ + local:l = (to-list $@numbers) + if (== (count $l) 0) { + return + } + local:m = $l[0] + each [n]{ + if ($t $n $m) { + m = $n + } + } $l[1..] + put $m +} + +edit:add-var min~ [@numbers]{ -minmax $'<~' $@numbers } +edit:add-var max~ [@numbers]{ -minmax $'>~' $@numbers }