| 1 | package Text::SpellChecker::GUI::Curses; | ||
| 2 | |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use ZConf::GUI; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Curses::UI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use String::ShellQuote; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Text::SpellChecker::GUI::Curses - Implements the Curses backend to Text::SpellChecker::GUI. | ||
| 12 | |||
| 13 | =head1 VERSION | ||
| 14 | |||
| 15 | Version 0.0.0 | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | our $VERSION = '0.0.0'; | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | use Text::SpellChecker::GUI::Curses; | ||
| 24 | |||
| 25 | my $sg = Text::SpellChecker::GUI::Curses->new(); | ||
| 26 | ... | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =head2 new | ||
| 31 | |||
| 32 | This initilizes the object. | ||
| 33 | |||
| 34 | =head3 args hash | ||
| 35 | |||
| 36 | =head4 useX | ||
| 37 | |||
| 38 | This is if it should try to use X or not. If it is not defined, | ||
| 39 | ZConf::GUI->useX is used. | ||
| 40 | |||
| 41 | =head4 zconf | ||
| 42 | |||
| 43 | This is the ZConf object to use. If it is not specified the one in the | ||
| 44 | object for zcrunner will be used. If neither zconf or zcrunner is specified, | ||
| 45 | a new one is created. | ||
| 46 | |||
| 47 | =head4 zcgui | ||
| 48 | |||
| 49 | This is the ZConf::GUI object. A new one will be created if it is | ||
| 50 | |||
| 51 | =cut | ||
| 52 | |||
| 53 | sub new{ | ||
| 54 | my %args; | ||
| 55 | if(defined($_[1])){ | ||
| 56 | %args= %{$_[1]}; | ||
| 57 | } | ||
| 58 | |||
| 59 | my $self={error=>undef, errorString=>''}; | ||
| 60 | bless $self; | ||
| 61 | |||
| 62 | #initilize the ZConf::GUI object if needed | ||
| 63 | if (!defined($args{zcgui})) { | ||
| 64 | if (!defined($args{zconf})) { | ||
| 65 | $self->{zg}=ZConf::GUI->new(); | ||
| 66 | }else { | ||
| 67 | $self->{zg}=ZConf::GUI->new({zconf=>$args{zconf}}); | ||
| 68 | } | ||
| 69 | |||
| 70 | if ($self->{zg}->{error}) { | ||
| 71 | $self->{errorString}='Failed to initilize the ZConf::GUI object. error="'. | ||
| 72 | $self->{zg}->{error}.'" errorString="' | ||
| 73 | .$self->{zg}->{errorString}.'"'; | ||
| 74 | $self->{error}=1; | ||
| 75 | warn('Text-SpellChecker-GUI new:1: '.$self->{errorString}); | ||
| 76 | return $self; | ||
| 77 | } | ||
| 78 | }else { | ||
| 79 | $self->{zg}=$args{zcgui}; | ||
| 80 | } | ||
| 81 | |||
| 82 | #initilize the ZConf object if needed | ||
| 83 | if (!defined($args{zconf})) { | ||
| 84 | $self->{zconf}=$args{zconf}; | ||
| 85 | }else { | ||
| 86 | $self->{zconf}=$self->{zg}->{zconf}; | ||
| 87 | } | ||
| 88 | |||
| 89 | #as the GUI stuff is right below this module, use the module above this one | ||
| 90 | $self->{useX}=$self->{zg}->useX('Text::SpellChecker'); | ||
| 91 | |||
| 92 | $self->{terminal}='xterm -rv -e '; | ||
| 93 | #if the enviromental variable 'TERMINAL' is set, use | ||
| 94 | if(defined($ENV{TERMINAL})){ | ||
| 95 | $self->{terminal}=$ENV{TERMINAL}; | ||
| 96 | } | ||
| 97 | |||
| 98 | return $self; | ||
| 99 | } | ||
| 100 | |||
| 101 | =head2 check | ||
| 102 | |||
| 103 | This checks the specified the specified text for spelling errors. | ||
| 104 | |||
| 105 | Only one option is taken and that is the text to be checked. | ||
| 106 | |||
| 107 | The returned value is a hash. | ||
| 108 | |||
| 109 | =head3 returned hash | ||
| 110 | |||
| 111 | =head4 text | ||
| 112 | |||
| 113 | This is the the resulting text. | ||
| 114 | |||
| 115 | =head4 cancel | ||
| 116 | |||
| 117 | If this is defined and true, then the user canceled the check and any changes. | ||
| 118 | |||
| 119 | my %returned=$sg->check($text); | ||
| 120 | if($sg->{error}){ | ||
| 121 | print "Error!\n"; | ||
| 122 | }else{ | ||
| 123 | if($returned{cancel}){ | ||
| 124 | print "The check was canceled."; | ||
| 125 | }else{ | ||
| 126 | $text=$returned{text}; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub check{ | ||
| 133 | my $self=$_[0]; | ||
| 134 | my $text=$_[1]; | ||
| 135 | |||
| 136 | $self->errorblank; | ||
| 137 | |||
| 138 | my %toreturn; | ||
| 139 | |||
| 140 | #dump this | ||
| 141 | my $file='/tmp/'.rand().rand().rand(); | ||
| 142 | open(CHECKWRITE, '>', $file); | ||
| 143 | print CHECKWRITE $text; | ||
| 144 | close(CHECKWRITE); | ||
| 145 | |||
| 146 | #the command to be run | ||
| 147 | my $command='curses-textspellchecker -f '.$file; | ||
| 148 | if ($self->{useX}) { | ||
| 149 | $command=$self->{terminal}.shell_quote($command); | ||
| 150 | } | ||
| 151 | |||
| 152 | system($command); | ||
| 153 | my $exitcode=$? >> 8; | ||
| 154 | |||
| 155 | #error if it got a -1... not in path | ||
| 156 | if ($? == -1) { | ||
| 157 | $self->{error}=2; | ||
| 158 | $self->{errorString}='"curses-textspellchecker" or the terminal is not in the path.'; | ||
| 159 | warn('Text-SpellChecker-GUI-Curses check:2: '.$self->{errorString}); | ||
| 160 | $toreturn{text}=$text; | ||
| 161 | $toreturn{code}=2; | ||
| 162 | return %toreturn; | ||
| 163 | } | ||
| 164 | |||
| 165 | if ($? == 0) { | ||
| 166 | if (open(READFH, $file)){ | ||
| 167 | my @textA=<READFH>; | ||
| 168 | close(READFH); | ||
| 169 | $toreturn{text}=join('', @textA); | ||
| 170 | }else { | ||
| 171 | $toreturn{text}=$text; | ||
| 172 | $toreturn{code}=3; | ||
| 173 | } | ||
| 174 | |||
| 175 | return %toreturn; | ||
| 176 | } | ||
| 177 | |||
| 178 | if ($exitcode == 230) { | ||
| 179 | $toreturn{text}=$text; | ||
| 180 | $toreturn{exitcode}=$exitcode; | ||
| 181 | $toreturn{cancel}=1; | ||
| 182 | return %toreturn; | ||
| 183 | } | ||
| 184 | |||
| 185 | #error if it is something other than 0 | ||
| 186 | if (!($? == 0)) { | ||
| 187 | $toreturn{text}=$text; | ||
| 188 | $toreturn{code}=$exitcode; | ||
| 189 | |||
| 190 | $self->{error}=3; | ||
| 191 | $self->{errorString}='The backend script exited with a non-zero, "'.$exitcode.'"'; | ||
| 192 | warn('Text-SpellChecker-GUI-Curses check:3: '.$self->{errorString}); | ||
| 193 | return %toreturn; | ||
| 194 | } | ||
| 195 | |||
| 196 | return %toreturn; | ||
| 197 | } | ||
| 198 | |||
| 199 | =head2 dialogs | ||
| 200 | |||
| 201 | This returns the available dailogs. | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub dialogs{ | ||
| 206 | return ('check'); | ||
| 207 | } | ||
| 208 | |||
| 209 | =head2 windows | ||
| 210 | |||
| 211 | This returns a list of available windows. | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | sub windows{ | ||
| 216 | return undef; | ||
| 217 | } | ||
| 218 | |||
| 219 | =head2 errorblank | ||
| 220 | |||
| 221 | This blanks the error storage and is only meant for internal usage. | ||
| 222 | |||
| 223 | It does the following. | ||
| 224 | |||
| 225 | $self->{error}=undef; | ||
| 226 | $self->{errorString}=""; | ||
| 227 | |||
| 228 | =cut | ||
| 229 | |||
| 230 | #blanks the error flags | ||
| 231 | sub errorblank{ | ||
| 232 | my $self=$_[0]; | ||
| 233 | |||
| 234 | $self->{error}=undef; | ||
| 235 | $self->{errorString}=""; | ||
| 236 | |||
| 237 | return 1; | ||
| 238 | } | ||
| 239 | |||
| 240 | =head1 ERROR CODES | ||
| 241 | |||
| 242 | =head2 1 | ||
| 243 | |||
| 244 | Failed to initiate ZConf::GUI. | ||
| 245 | |||
| 246 | =head2 2 | ||
| 247 | |||
| 248 | Failed to find the required commands in the path. | ||
| 249 | |||
| 250 | =head2 3 | ||
| 251 | |||
| 252 | Failed to read temp file. | ||
| 253 | |||
| 254 | =head1 AUTHOR | ||
| 255 | |||
| 256 | Zane C. Bowers, C<< <vvelox at vvelox.net> >> | ||
| 257 | |||
| 258 | =head1 BUGS | ||
| 259 | |||
| 260 | Please report any bugs or feature requests to C<bug-text-spellchecker-gui at rt.cpan.org>, or through | ||
| 261 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-SpellChecker-GUI>. I will be notified, and then you'll | ||
| 262 | automatically be notified of progress on your bug as I make changes. | ||
| 263 | |||
| 264 | |||
| 265 | |||
| 266 | |||
| 267 | =head1 SUPPORT | ||
| 268 | |||
| 269 | You can find documentation for this module with the perldoc command. | ||
| 270 | |||
| 271 | perldoc Text::SpellChecker::GUI | ||
| 272 | |||
| 273 | |||
| 274 | You can also look for information at: | ||
| 275 | |||
| 276 | =over 4 | ||
| 277 | |||
| 278 | =item * RT: CPAN's request tracker | ||
| 279 | |||
| 280 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-SpellChecker-GUI> | ||
| 281 | |||
| 282 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 283 | |||
| 284 | L<http://annocpan.org/dist/Text-SpellChecker-GUI> | ||
| 285 | |||
| 286 | =item * CPAN Ratings | ||
| 287 | |||
| 288 | L<http://cpanratings.perl.org/d/Text-SpellChecker-GUI> | ||
| 289 | |||
| 290 | =item * Search CPAN | ||
| 291 | |||
| 292 | L<http://search.cpan.org/dist/Text-SpellChecker-GUI/> | ||
| 293 | |||
| 294 | =back | ||
| 295 | |||
| 296 | |||
| 297 | =head1 ACKNOWLEDGEMENTS | ||
| 298 | |||
| 299 | |||
| 300 | =head1 COPYRIGHT & LICENSE | ||
| 301 | |||
| 302 | Copyright 2009 Zane C. Bowers, all rights reserved. | ||
| 303 | |||
| 304 | This program is free software; you can redistribute it and/or modify it | ||
| 305 | under the same terms as Perl itself. | ||
| 306 | |||
| 307 | |||
| 308 | =cut | ||
| 309 | |||
| 310 | 1; # End of Text::SpellChecker::GUI::Curses | ||