elvish_config/aliases/pdf2grey.elv

83 lines
1.8 KiB
Plaintext
Raw Normal View History

2021-12-11 21:09:02 +00:00
edit:add-var pdf2grey~ {|@argv|
2021-08-24 18:45:25 +00:00
use re
use str
var m = [
2021-08-24 18:45:25 +00:00
&nb=$false
&split=50
]
2021-12-11 21:09:02 +00:00
fn _help {||
2021-08-24 18:45:25 +00:00
echo 'Usage: pdf2gray [-hn (<n>)] <input>.pdf <output>.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)'
}
2021-12-11 21:09:02 +00:00
fn is_pdf {|e|
2021-08-24 18:45:25 +00:00
re:match '.*\.pdf$' $e
}
2021-12-11 21:09:02 +00:00
fn is_int {|e|
2021-08-24 18:45:25 +00:00
re:match '^\d+$' $e
}
2021-12-11 21:09:02 +00:00
fn is_split {|e|
2021-08-24 18:45:25 +00:00
and (>= $e 0) (<= $e 100)
}
while (> (count $argv) 0) {
var e @argv = $@argv
2021-08-24 18:45:25 +00:00
if (or (eq $e '-h') (eq $e '--help')) {
_help
return
} elif (eq $e -n) {
2021-10-13 18:43:40 +00:00
set m[nb] = $true
2021-08-24 18:45:25 +00:00
if (== (count $e) 0) {
echo 'Il manque des arguments après -s'
_help
return
} elif (is_int $argv[0]) {
2021-10-13 18:43:40 +00:00
set e @argv = $@argv
2021-08-24 18:45:25 +00:00
if (is_split $e) {
2021-10-13 18:43:40 +00:00
set m[split] = $e
2021-08-24 18:45:25 +00:00
} else {
echo 'Largument (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)) {
2021-10-13 18:43:40 +00:00
set m[input] = $e
2021-08-24 18:45:25 +00:00
} else {
2021-10-13 18:43:40 +00:00
set m[output] = $e
2021-08-24 18:45:25 +00:00
if (> (count $argv) 0) {
echo 'Trop darguments: '$@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
}
var params = [-colorspace gray]
2021-08-24 18:45:25 +00:00
if $m[nb] {
2021-10-13 18:43:40 +00:00
set @params = (all $params) -threshold $m[split]'%'
2021-08-24 18:45:25 +00:00
}
2021-10-13 18:43:40 +00:00
set @params = $@params $m[input] $m[output]
2021-08-24 18:45:25 +00:00
echo 'Commande: convert '(str:join ' ' $params)
convert $@params
2021-08-27 20:20:17 +00:00
}