elvish_config/aliases/random.elv

86 lines
2.1 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

edit:add-var gen-str~ {|@argv|
use re
use str
fn j {|@strings| str:join '' $strings}
fn bytes {|begin size| str:from-codepoints (range $begin (+ $begin $size))}
fn upper { bytes 0x41 26 }
fn lower { bytes 0x61 26 }
fn digit { bytes 0x30 10 }
fn alpha { j (upper) (lower) }
fn alphanum { j (alpha) (digit) }
fn random {|chars|
var size = (count $chars)
print $chars[(randint 0 $size)]
}
fn is-int {|arg| re:match '^\d+$' $arg}
fn help {||
echo 'Génère une chaîne de caractères aléatoires'
echo 'Usage: genstr [(-auldh)] [<chars>] [<size>]'
echo ''
echo 'Options:'
echo ' -a Uniquement des lettres ([A-Za-z])'
echo ' -u Uniquement des lettres majuscules ([A-Z])'
echo ' -l Uniquement des lettres minuscules ([a-z])'
echo ' -d Uniquement des chiffres ([0-9])'
echo ''
echo '<chars> Liste de caractères possibles'
echo '<size> Longueur de la chaîne (par défaut: 10)'
echo ''
echo 'Si aucune classe de caractères ou liste de caractères nest fournie, utilise les caractères alphanumériques ([A-Za-z0-9]).'
}
var len chars h = 0 '' $false
each {|e|
if (or (eq $e -h) (eq $e --help)) {
set h = $true
help
break
} elif (eq $e -a) {
set chars = (j $chars (alpha))
} elif (eq $e -u) {
set chars = (j $chars (upper))
} elif (eq $e -l) {
set chars = (j $chars (lower))
} elif (eq $e -d) {
set chars = (j $chars (digit))
} elif (and (is-int $e) (> $e 0)) {
set len = $e
} else {
set chars = (j $chars $e)
}
} $argv
if (not $h) {
if (== $len 0) {
set len = 10
}
if (eq $chars '') {
set chars = (alphanum)
}
set @chars = (str:split '' $chars)
range $len | each {|_| random $chars}
echo
}
}
edit:add-var grawlix~ {|@argv|
use str
var @chars = (str:split '' '#$%&*+=^~£§¶±÷≠Ω√∆◊')
var nbChars = (count $chars)
var length = (shuf -i 4-7 -n 1)
var graw = []
range $length | each {|_|
var ci = (shuf -i 1-$nbChars -n 1)
set @graw = $@graw $chars[(- $ci 1)]
}
set @graw = $@graw '!'
echo (str:join '' $graw)
}