Last active
August 29, 2015 14:03
-
-
Save fabienhinault/0ed0dccef5b21022fc7a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# typerandom | |
# | |
# generate a .typ file made of random sequences of characters | |
# and call gtypist to run this file | |
# | |
# Fabien Hinault <fabien-dot-hinault-at-free-dot-fr> | |
# | |
# This program borrows heavily from typefortune | |
# | |
# 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 2 | |
# of the License, or (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
use strict qw (subs vars refs); | |
use Carp; | |
use File::Temp qw (tempfile); | |
use Getopt::Std; | |
# subroutines | |
# this creates a B:-command (centered on 66 columns because | |
# "gtypist <version>" is in the right corner) | |
sub getBanner($) | |
{ | |
my $banner = $_[0]; | |
# remove whitespace at the beginning ... | |
$banner =~ s/^\s*//; | |
# ... and at the end | |
$banner =~ s/\s*$//; | |
return "B:" . " " x (33 - int((length($banner) / 2))) . $banner . "\n"; | |
} | |
my %opts; | |
if (!getopts ("dslhn:o:", \%opts) || $opts{h}) { | |
print "SYNTAX: typefortune [-dslh] [-n count] [-o gtypist_opts]\n"; | |
print "-n <count>: practice <count> times (default=1)\n"; | |
print "See the gtypist manual (info gtypist or info '(gtypist)') " . | |
"for details.\n"; | |
if ($opts{h}) { | |
exit 0; | |
} else { | |
exit 1; | |
} | |
} | |
# operate on command-line arguments | |
my $exercise_type = "S:"; | |
if ($opts{d}) { $exercise_type = "D:"; } | |
my $max_lines = 22; | |
if ($exercise_type eq "D:" || $exercise_type eq "d:") { $max_lines = 11; } | |
if ($opts{s} && $opts{l}) { die "-s and -l cannot be used together"; } | |
my $fortune_command = ""; | |
if ($opts{s}) { $fortune_command .= " -s"; } | |
if ($opts{l}) { $fortune_command .= " -l"; } | |
my $count = 1; | |
if (defined ($opts{n})) { $count = $opts{n}; } | |
# create option-list for gtypist | |
my $gtypist_options = ""; | |
if (!defined ($opts{o})) { | |
# avoid warning | |
$opts{o} = ""; | |
} | |
foreach my $opt (split (/\s+/, $opts{o})) | |
{ | |
my $sepidx = index ($opt, ","); | |
if ($sepidx == -1) { | |
# boolean option | |
if (length ($opt) == 1) { # short option | |
$gtypist_options .= "-" . $opt; | |
} else { # long option | |
$gtypist_options .= "--" . $opt; | |
} | |
} else { | |
# option with value | |
if ($sepidx == 1) { # short option | |
$gtypist_options .= "-" . substr ($opt, 0, 1); | |
} else { # long option | |
$gtypist_options .= "--" . substr ($opt, 0, $sepidx - 1); | |
} | |
$gtypist_options .= " " . substr ($opt, $sepidx + 1); | |
} | |
$gtypist_options .= " "; | |
} | |
my @characters = ('a'..'z','A'..'Z','1'..'9','0','!','@','#','$','%','^','&','*','(',')','-','_','=','+','[',']','{','}',';',':', '"' , '\'' , ',' , '<','.','>','/','?','`','~'); | |
my $line_length = 79; | |
my $word_length = 3; | |
my $nb_word = int($line_length / ($word_length + 1)); | |
my $i; | |
my $j; | |
my $ichar; | |
my $line; | |
my ($typfile, $typfilename); | |
eval { | |
($typfile, $typfilename) = | |
tempfile("typerandom.XXXXXX", DIR => "/tmp", UNLINK => 1); | |
}; | |
if ($@) { | |
croak "Couldn't create temporary file in /tmp"; | |
} | |
print $typfile "# temporary file created by typerandom\n"; | |
print $typfile "K:12:END\n"; | |
for ($i = 0; $i < $count; $i++) | |
{ | |
$line = ''; | |
for ($j = 0; $j < $nb_word; $j++) | |
{ | |
for ($ichar = 0; $ichar < $word_length; $ichar++) | |
{ | |
$line = $line . $characters[int(rand($#characters + 1))]; | |
} | |
$line = $line . " "; | |
} | |
# maybe insert banner | |
print $typfile getBanner($1); | |
print $typfile "I:random ". ($i + 1) . "/" . $count . "\n"; | |
# print line | |
print $typfile "$exercise_type"; | |
print $typfile $line; | |
print $typfile "\n"; | |
} | |
print $typfile getBanner("Practice complete"); | |
print $typfile "T:\n :\n :\n : Congratulations: " . | |
"you successfully completed the $count " . | |
"random lessons !\n"; | |
print $typfile "*:END\nX:\n"; | |
close($typfile) || die "Couldn't close $typfilename: $!"; | |
# call gtypist | |
system("gtypist $gtypist_options $typfilename"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment