From b27290df23a60c08949ee1dce5898f43afd6ebd4 Mon Sep 17 00:00:00 2001 From: Benjamin VAUDOUR Date: Fri, 19 Nov 2021 18:37:08 +0100 Subject: [PATCH] =?UTF-8?q?G=C3=A9n=C3=A9rateur=20de=20caract=C3=A8res=20a?= =?UTF-8?q?l=C3=A9atoires?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aliases/random.elv | 94 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 aliases/random.elv diff --git a/aliases/random.elv b/aliases/random.elv new file mode 100644 index 0000000..18bc5d6 --- /dev/null +++ b/aliases/random.elv @@ -0,0 +1,94 @@ +use str +use re + +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 -j [@strings]{ + str:join '' $strings +} + +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)] [] []' + 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 ' Liste de caractères possibles' + echo ' Longueur de la chaîne (par défaut: 10)' + echo '' + echo 'Si aucune classe de caractères ou liste de caractères n’est fournie, utilise les caractères alphanumériques ([A-Za-z0-9]).' +} + +edit:add-var genstr~ [@args]{ + 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) + } + } $args + + 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 + } +}