From d3805b57c536f02df37d4981a21fd9901d412a4a Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Wed, 13 Mar 2024 09:14:21 +0100 Subject: [PATCH] Option lang --- bin/say | 190 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 178 insertions(+), 12 deletions(-) diff --git a/bin/say b/bin/say index 945e0de0..fc5601d3 100755 --- a/bin/say +++ b/bin/say @@ -1,18 +1,35 @@ #!/usr/bin/env perl use strict; -my %E = ( +use warnings; + +############################################################################### +# +# VERSION +# +############################################################################### +my $VERSION = '0.0.1'; +my ($NAME) = $0 =~ m,([^/]+)$,; +my $VERBOSE = $main::VERBOSE = 0; +my $DEBUG = $main::DEBUG = 0; +# NB 22.01.24 +# - create script + +############################################################################### +# +# ARGS +# +############################################################################### +my %Opt = ( + 'lang' => 'en-gb', ); +get_options(\%Opt); +$main::_DATA_ = undef; -sub espeak { - local $_ = $_[0]; - #s/[\r\n]//g; - s@(\.)@$1@g; - #die "<$_>"; - s@(;)@$1@g; - s@(,)@$1@g; - s@(:)@$1@g; - return $_; -} +############################################################################### +# +# BEGIN +# +############################################################################### #$_ = qq{ #It was day. @@ -21,7 +38,7 @@ sub espeak { #die $_; #open(SAY,"| espeak -v mb-en1+f5 -s 140 -m 2>/dev/null") or die $!; -open(SAY,"| espeak -m --stdout 2>/dev/null | aplay -q") or die $!; +open(SAY,"| espeak -v $Opt{lang} -m --stdout 2>/dev/null | aplay -q") or die $!; if (@ARGV or -t STDIN) { $_ = join(" ",@ARGV); @@ -33,3 +50,152 @@ $_ = espeak($_); printf SAY $_; close SAY; + +############################################################################### +# +# END +# +############################################################################### +exit 0; + +############################################################################### +# +# Functions +# +############################################################################### +sub espeak { + local $_ = $_[0]; + #s/[\r\n]//g; + s@(\.)@$1@g; + #die "<$_>"; + s@(;)@$1@g; + s@(,)@$1@g; + s@(:)@$1@g; + return $_; +} + +sub help { +#------------------------------------------------------------------------------ +# Print help and exit +#------------------------------------------------------------------------------ + + require 'Pod/Usage.pm' unless $INC{'Pod/Usage.pm'}; + + # Env Substitutions + sub pod_env { + my $v = ''; + eval '$v = ref(\\'.$_[0].') eq "ARRAY" ? join(" ",'.$_[0].') : '.$_[0].'; return defined $v ? $v : qq|UNDEF|;'; + return $v; + } + + $main::_DATA_ =~ s/([@\$][A-Z_a-z\{\}]+)/pod_env($1)/eg; + + # Print help + my $in; + open($in,'<',\$main::_DATA_); + open(STDOUT,"|perl -pe 's/\.$$//g'".(($ENV{PAGER}||'') eq 'less' ? "|less -FRi" : "")); + my $opts = { + -input => $in, + -ouput => \*STDOUT, + -exitval => 'noexit', + -sections => [qw(SYNOPSIS DESCRIPTION OPTIONS)], + -verbose => ($Opt{'help'} ? 99 : 3), + }; + + Pod::Usage::pod2usage($opts); + close $in; + close STDOUT; + + exit 0; +} + +#------------------------------------------------------------------------------ +# Print version and exit +#------------------------------------------------------------------------------ +sub version { print "$NAME: version [$VERSION]\n"; exit 0; } + +#------------------------------------------------------------------------------ +# Get options from pod +#------------------------------------------------------------------------------ +sub get_options { + + use Getopt::Long qw(:config no_ignore_case no_auto_abbrev); + + my @Opt; + + # Clean extra param to string option inside braquets + sub pod_opt { + local $_; + my $o = shift; + $o =~ s/(=.|[\+\-\!]$)//; + $o = join(", ",map{"-$_"} split(/[\|,:;]/,$o)); + return "$o"; + } + + # Var to get max option's length + $main::_LEN_ = 0; + while () + { + s/option\[([^\]]+)\]/push(@Opt,$1) and &pod_opt($1).' _LEN_'/eg; + $main::_DATA_ .= $_; + # Get max length options + $main::_LEN_ = $_ if $1 and $_ = length(&pod_opt($1)) and $main::_LEN_ < $_; + } + $main::_DATA_ =~ s/(--.*?)_LEN_ */$1.' 'x(2+($main::_LEN_ - length($1)))/eg; + + GetOptions($_[0],@Opt) || exit -1; + + help() if $_[0]{'help'} or $_[0]{'man'}; + version() if $_[0]{'version'}; + + $main::VERBOSE = $VERBOSE = $_[0]{'verbose'} if defined $_[0]{'verbose'}; + $main::DEBUG = $DEBUG = $_[0]{'debug'} if defined $_[0]{'debug'}; + +} + +__DATA__ + +=head1 NAME + +$NAME TEXT + +=head1 DESCRIPTION + +Say some text + +=head1 OPTIONS + + -option[lang=s] Lang (default: $Opt{lang}) + -option[verbose|v+] Verbose mode: increase the verbosity level. + -option[debug+] Debug mode: increase the debug level. + -option[version|V] Print version (default: $VERSION) + -option[help|h|?] Print a brief help message and exits. + -option[man] Print the manual page and exits. + +=cut + +=head1 EXAMPLES + +... + +=head1 REQUIRES + +Getopt::Std, Pod::Usage + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2024 Nicolas Boisselier + +This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + +See . + +=head1 SEE ALSO + +perl(1), http://perldoc.perl.org/perlpodstyle.html + +=head1 AUTHOR + +Nicolas Boisselier + +=cut -- 2.47.3