1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
| #!/usr/bin/perl
use warnings;
use strict;
use utf8;
use encoding 'utf8';
use Tk;
use Tk::Dialog;
use Tk::Photo;
use Tk::LabFrame;
use Tk::ROText;
use File::Spec; # Gestion des noms des chemins sous tous les OS
use Config::Std; # Lecture/ecriture d'un fichier .ini
my $RepBase = 'E:/_APPLI/Regex/';
my $Bulb = 'E:/_APPLI/Regex/Images/Bulb.gif';
my $Plus = 'E:/_APPLI/Regex/Images/Plus.gif';
my $Moins = 'E:/_APPLI/Regex/Images/Moins.gif';
my $Insert = 'E:/_APPLI/Regex/Images/Insert.gif';
my $TexteInput = "";
my $LabMessage = "";
my $LabRegex = "";
my $Reponse = "";
my $Regex = "";
my $RegEdit = "";
my $VarReg = "";
my $NbTrouve = 0;
my $FlagSave = 0;
# Lecture du fichier de configuration
my $fichier_config = File::Spec->catfile( $RepBase, 'Regex.ini' );
my %config;
if ( -e $fichier_config ) {
read_config $fichier_config => %config;
}
else {
%config = (
'GLOBAL' => {
confirm => 'OUI',
curfile => '',
},
'REGEX' => {
'regex' => '.ini ABSENT !'
},
);
}
my $Filepath = $config{GLOBAL}{curfile};
my $LabFile = $Filepath;
my @LesRegex = split (/\n/,$config{REGEX}{regex});
$Reponse = "OUI" if ( $config{GLOBAL}{confirm} eq 'NON');
ActiverAccentsDOS();
my $fen = MainWindow->new;
my $width = $fen->screenwidth();
my $height = $fen->screenheight();
$fen->destroy();
# Creation du widget principal
my $Wm = MainWindow->new(
-width => $width,
-height => $height,
-title => "Regex",
-background => 'white',
);
$width = int($Wm->screenwidth*(8/9));
$height = int($Wm->screenheight*(5/6));
$Wm->configure(-height=>$height, -width=>$width, -title=>"Recherche dans un texte");
$Wm->geometry( $width ."x". $height );
#$Wm->MoveToplevelWindow (100,100);
#$Wm->overrideredirect(1);
#$Wm->packPropagate(0);
#$Wm->raise();
# Configurations
my $couleur_commune = '#E0F0FF';
# Cadre Gestion
my $cadre_gestion = $Wm->LabFrame(
-label => 'Gestion',
-background => $couleur_commune,
-font => '{Simplified Arabic} 10 {bold}',
);
###############################
# Scrolled qui affiche le texte
my $widget_input = $Wm->Scrolled(
'Text',
-scrollbars => 'e',
-takefocus => 0,
-font => '{Simplified Arabic} 14 {bold}',
-height => '6',
-wrap => 'word',
-relief => 'groove',
-background => '#ABFAA3',
);
my $widget_output = $Wm->Scrolled(
'ROText',
-scrollbars => 'e',
-takefocus => 0,
-font => '{Simplified Arabic} 14 {bold}',
-height => '6',
-wrap => 'word',
-relief => 'groove',
-background => '#ABFA30',
);
configurer_tags($widget_input);
configurer_tags($widget_output);
#########################################################
# 1er ordre non respecté pour visibilité dans Regex Moins
##############
# Regex Utiles
#my $Affichage = "Regex";
my $option_regex = $cadre_gestion->Optionmenu(
# -textvariable => \$Affichage,
-variable => \$VarReg,
-options => \@LesRegex,
-command => sub {
$RegEdit = $VarReg;
},
);
# Sauvegarde du fichier de configuration
my $bouton_save = $cadre_gestion->Button(
-text => "Save",
-command => sub {
if ( ! $FlagSave ) {
$LabMessage = "La configuration des regex n'a pas été modifiée, Pas de sauvegarde !";
print "\a";
} else {
confirmation($Wm, "Faut-il sauvegarder la configuration ?") if ( $config{GLOBAL}{confirm} eq 'OUI');
if ($Reponse eq "OUI") {
save_config( $fichier_config, \%config );
$LabMessage = "Fichier $fichier_config sauvegardé";
} else {
$LabMessage = "Configuration non modifiée !";
}
}
},
);
#############
# Regex Moins
$Moins = $Wm->Photo( -file => $Moins );
my $bouton_regMoins = $cadre_gestion->Button(
-image => $Moins,
-text => " - ",
-command => sub {
confirmation($Wm, "Faut-il supprimer la regex $RegEdit de la liste d'option ?") if ( $config{GLOBAL}{confirm} eq 'OUI');
if ($Reponse eq "OUI") {
#push @LesRegex, $RegEdit;
my @RegTemp;
foreach my $Reg ( @LesRegex ) {
if ( $Reg ne $VarReg ) {
push @RegTemp, $Reg;
}
}
@LesRegex = @RegTemp;
$option_regex->configure(
-options => \@LesRegex,
);
$FlagSave++;
$LabMessage = "Regex $RegEdit supprimée !";
} else {
$LabMessage = "Liste des regex non modifiée !";
}
},
);
# Regex Plus
$Plus = $Wm->Photo( -file => $Plus );
my $bouton_regPlus = $cadre_gestion->Button(
-image => $Plus,
#-text => " + ",
-command => sub {
confirmation($Wm, "Faut-il ajouter la regex $RegEdit à la liste d'option ?") if ( $config{GLOBAL}{confirm} eq 'OUI');
if ($Reponse eq "OUI") {
push @LesRegex, $RegEdit;
$option_regex->configure(
-options => \@LesRegex,
);
$FlagSave++;
$LabMessage = "Regex $RegEdit ajoutée !";
} else {
$LabMessage = "Liste des regex non modifiée !";
}
},
);
# Regex Edit
my $RegexEdit = $cadre_gestion->Entry(
-width => 40,
-justify => 'left',
-textvariable => \$RegEdit,
-font => '{Simplified Arabic} 10 {bold}',
);
# Regex Insert
$Insert = $Wm->Photo( -file => $Insert );
my $bouton_regInsert = $cadre_gestion->Button(
#-text => " > ",
-image => $Insert,
-command => sub {
confirmation($Wm, "Faut-il insérer $RegEdit à la Regex ?") if ( $config{GLOBAL}{confirm} eq 'OUI');
if ($Reponse eq "OUI") {
$Regex = $Regex.$VarReg;
$LabMessage = "Regex modifiée !";
} else {
$LabMessage = "Modification de la Regex abandonnée !";
}
},
);
# Choix Fichier
my $bouton_file = $cadre_gestion->Button(
-text => "Fichier",
-command => sub {
get_file();
$LabFile = $Filepath;
$FlagSave ++;
},
);
# Load du fichier dans input
my $bouton_load = $cadre_gestion->Button(
-text => "Load",
-command => sub {
load_file ();
},
);
# Début Input
my $bouton_deb = $cadre_gestion->Button(
-text => "Début Input",
-command => sub {
$widget_input->see( '0.0' );
},
);
# Regex
my $LabelRegex = $cadre_gestion->Label(
-text => "Regex",
-font => '{Simplified Arabic} 10 {bold}',
);
my $RegexSaisie = $cadre_gestion->Entry(
-width => 60,
-justify => 'left',
-textvariable => \$Regex,
-font => '{Simplified Arabic} 10 {bold}',
);
# Start
my $bouton_start = $cadre_gestion->Button(
-text => "Start Regex",
-command => sub {
if ( ! $TexteInput ) {
$LabMessage = "Input est vide !!!";
print "\a\a";
return
}
$NbTrouve = 0;
$LabMessage = "Recherche en cours...";
$widget_output->delete( '0.0', 'end' );
Regex ();
$LabMessage = "Recherche terminée, Trouvé : $NbTrouve";
},
);
# Clear Input
my $bouton_clearInput = $cadre_gestion->Button(
-text => "Clear Input",
-command => sub {
if ($TexteInput) {
confirmation($Wm, "Faut-il effacer le contenu de Input ?") if ( $config{GLOBAL}{confirm} eq 'OUI');
if ($Reponse eq "OUI") {
$widget_input->delete( '0.0', 'end' );
$LabRegex = "";
$TexteInput = "";
$LabMessage = "Contenu de Input effacé !";
} else {
$LabMessage = "Contenu de Input conservé !";
}
} else {
$LabMessage = "Input déjà effacé !";
print "\a";
}
},
);
# Clear Tags Input
$Bulb = $Wm->Photo( -file => $Bulb );
my $bouton_clearTagInput = $cadre_gestion->Button(
-image => $Bulb,
#-text => "SurB",
-command => sub {
if ($TexteInput) {
confirmation($Wm, "Faut-il effacer le surlignement dans Input ?") if ( $config{GLOBAL}{confirm} eq 'OUI');
if ($Reponse eq "OUI") {
$widget_input->tagDelete( 'g14' );
$LabMessage = "Surbrillances dans Input effacées !";
} else {
$LabMessage = "Surbrillances dans Input conservées !";
}
}
configurer_tags($widget_input);
},
);
# Clear Output
my $bouton_clearOutput = $cadre_gestion->Button(
-text => "Clear Output",
-command => sub {
confirmation($Wm, "Faut-il effacer le contenu de Output ?") if ( $config{GLOBAL}{confirm} eq 'OUI');
if ($Reponse eq "OUI") {
$widget_output->delete( '0.0', 'end' );
$LabRegex = "";
$LabMessage = "Contenu de Output effacé !";
} else {
$LabMessage = "Contenu de Output conservé !";
}
},
);
# Exit
my $bouton_exit = $cadre_gestion->Button(
-text => "Exit",
-command => [ \&fermer, ],
);
####################################
# Cadre pour les messages de service
my $cadre_messages = $Wm->Frame( -background => $couleur_commune, );
# Label qui affichera le répertoire courant
my $label_repertoire = $cadre_messages->Label(
-textvariable => \$LabFile,
-width => 15,
-background => '#FFE0D0',
-relief => 'groove',
);
# Label qui affichera la Regex, en bas
my $label_regex = $cadre_messages->Label(
-textvariable => \$LabRegex,
-background => '#FFE0D0',
-relief => 'groove',
);
# Label qui affichera les messages, en bas
my $label_message = $cadre_messages->Label(
-textvariable => \$LabMessage,
-background => '#FFE0D0',
-relief => 'groove',
);
####################################
# Gestion de l'affichage des widgets
#
$cadre_messages->pack(qw / -side bottom -fill both -expand 0 /);
$label_repertoire->pack(qw / -side left -fill both -expand 1 /);
$label_regex->pack(qw / -side left -fill both -expand 1 /);
$label_message->pack(qw / -side left -fill both -expand 1 /);
$widget_output->pack(qw / -side bottom -fill both -expand 1 /);
$widget_input->pack(qw / -side bottom -fill both -expand 1 /);
$cadre_gestion->pack(qw / -side right -fill both -expand 1 /);
$bouton_save->grid( $bouton_regMoins, $option_regex, $bouton_regPlus, $RegexEdit,
$bouton_regInsert, $bouton_file, $bouton_load, $bouton_deb,
$LabelRegex, $RegexSaisie, $bouton_start, $bouton_clearInput,
$bouton_clearTagInput, $bouton_clearOutput, $bouton_exit,
qw / -padx 2 -pady 5 -sticky nsew /
);
# Centrer la fenêtre
centrer_widget($Wm);
# Invalidation fermeture -> obligation de passer par exit
$Wm->protocol( "WM_DELETE_WINDOW", sub {
fermer();
}
);
MainLoop;
#############################################################################
sub Regex {
if ( !$Regex ) {
$LabRegex = "Regex vide !";
$LabMessage = "Regex vide !";
print "\a\a";
return;
}
$TexteInput = $widget_input->get( '1.0', 'end' );
my $Pattern = $Regex;
$Pattern =~ s/ /\\ /go;
$LabRegex = $Pattern;
my $i = 0;
my @Array = ($TexteInput =~ m/(\b.{0,40}$Pattern.{0,40}\b)/gx);
$NbTrouve = @Array;
if ($NbTrouve == 1){
my @NewArray = split /$Pattern/, $TexteInput;
my @ChainePref = ( $Array[0] =~ m/($Pattern)/gx);
$widget_output->insert( 'end', $NewArray[0] );
$widget_output->see( 'end' );
$widget_output->update;
$widget_output->tagAdd( 'MARK', 'end -1 chars' );
my @Mark = $widget_output->tagRanges( 'MARK' );
my ($SelA, $SelB) = split /\./, $Mark[0];
$SelB = $SelB + length($ChainePref[0]);
my $SelLast = $SelA.".".$SelB;
$widget_input->see( $Mark[0] );
$widget_output->insert( 'end', $ChainePref[0], "g14");
# pb tagAdd sur tag déjà effacé !
$widget_input->tagAdd( 'g14', $Mark[0], $SelLast);
$widget_input->update;
$widget_output->insert( 'end', $NewArray[1] );
$LabRegex = $Pattern;
} else {
foreach my $Ligne ( @Array ) {
$widget_output->insert( 'end', ++$i .": ".$Ligne."\n" );
}
}
}
#############################################################################
# Fermer l'appli
#
sub fermer {
if ($FlagSave) {
confirmation($Wm, "Faut-il sauvegarder la configuration ?") if ( $config{GLOBAL}{confirm} eq 'OUI');
if ($Reponse eq "OUI") {
save_config( $fichier_config, \%config );
$LabMessage = "Fichier $fichier_config sauvegardé";
} else {
$LabMessage = "Configuration non modifiée !";
}
}
exit (0);
}
########################
# Recherche d'un fichier
#
sub get_file {
my @types =
(["Log files", [qw/.txt .log/]],
["All files", '*'],
);
$Filepath = $Wm->getOpenFile(-filetypes => \@types) or return();
$LabMessage = "Fichier en cours : $Filepath";
return($Filepath);
}
#########################
# Chargement d'un fichier
#
sub load_file {
my $IN_Fic = $Filepath;
if ( ! $Filepath ){
$LabMessage = "ERREUR : Absence de fichier à charger";
print "\a";
return;
}
unless (open (IN, "<", $IN_Fic)) {
$LabMessage = "ERREUR : Impossible d'ouvrir $IN_Fic - $! - $@\n";
$Wm->Label->update();
return;
}
if ($TexteInput) {
confirmation($Wm, "Faut-il conserver le texte présent, et donc ajouter celui du fichier $Filepath à la suite ?") if ( $config{GLOBAL}{confirm} eq 'OUI');
if ($Reponse eq "OUI") {
$widget_input->insert( 'end', "\n= = = = = = = = = = = = = = = = = = = = =\n\n", "c28");
$LabMessage = "Ajout de $Filepath en cours...";
} else {
$widget_input->delete( '1.0', 'end' );
$LabMessage = "Chargement $Filepath en cours...";
}
}
while ( my $Ligne = <IN> ) {
$widget_input->insert( 'end', $Ligne );
}
$widget_input->update;
$LabMessage = "$Filepath chargé !";
$TexteInput = $widget_input->get( '1.0', 'end' );
close $IN_Fic;
}
######################
# Fenêtre Confirmation
#
sub confirmation {
my $widget = shift;
my $Question = shift;
my $dialog = $widget->Dialog(
-title => 'Confirmation',
-buttons => [ 'OUI', 'NON' ],
-background => '#FFE0D0',
-text => $Question,
);
# Configuration des boutons
$dialog->Subwidget("B_OUI")->configure( -background => '#68C963', );
$dialog->Subwidget("B_NON")->configure( -background => '#CF0063', );
$Reponse = $dialog->Show();
}
#########################################
# Configuration des polices de caractères
#
sub configurer_tags {
my $widget_text = shift;
for my $police ( 12 .. 72 ) {
if ( $police % 2 == 0 ) {
$widget_text->tagConfigure(
"g$police",
-background => '#ABFAA3',
-foreground => '#FF0000',
-font => "{Simplified Arabic} $police {bold}",
-justify => 'left',
);
$widget_text->tagConfigure(
"c$police",
-background => '#ABFAA3',
-foreground => '#FF0000',
-font => "{Simplified Arabic} $police {bold}",
-justify => 'center',
);
$widget_text->tagConfigure(
"d$police",
-background => '#ABFAA3',
-foreground => '#FF0000',
-font => "{Simplified Arabic} $police {bold}",
-justify => 'right',
);
}
}
return;
}
##########################
# Sauvegarde configuration
#
sub save_config {
my ( $fichier, $ref_config ) = @_;
$config{GLOBAL}{curfile} = $Filepath;
$config{REGEX}{regex} = join("\n",@LesRegex);
write_config %{$ref_config}, $fichier;
$FlagSave = 0;
return;
}
#===========================================================================================
# But : Centrer un widget automatiquement
# http://perl.developpez.com/faq/tk/?page=Les-Fenetres-de-premier-niveau-MainWindow-Toplevel
#===========================================================================================
sub centrer_widget {
my ( $widget ) = @_;
# Height and width of the screen
my $largeur_ecran = $widget->screenwidth();
my $hauteur_ecran = $widget->screenheight();
# update le widget pour récupérer les vraies dimensions
$widget->update;
my $largeur_widget = $widget->width;
my $hauteur_widget = $widget->height;
# On centre le widget en fonction de la taille de l'écran
my $nouvelle_largeur = int( ( $largeur_ecran - $largeur_widget ) / 2 );
my $nouvelle_hauteur = int( ( $hauteur_ecran - $hauteur_widget ) / 2 );
$widget->geometry($largeur_widget . "x" . $hauteur_widget
. "+$nouvelle_largeur+$nouvelle_hauteur");
$widget->update;
return;
}
#==============================================================
# Pour avoir les accents sur la console DOS
# http://perl.developpez.com/faq/perl/?page=Terminal#AccentsDOS
#==============================================================
sub ActiverAccentsDOS {
my $encodage;
# Windows
if ( lc($^O ) eq 'mswin32') {
eval {
my ($codepage) = ( `chcp` =~ m/:\s+(\d+)/ );
$encodage = "cp$codepage";
foreach my $h ( \*STDOUT, \*STDERR, \*STDIN, ) {
binmode $h, ":encoding($encodage)";
}
};
}
else {
$encodage = `locale charmap`;
eval {
foreach my $h ( \*STDOUT, \*STDERR, \*STDIN, ) {
binmode $h, ":encoding($encodage)";
}
};
}
return $encodage;
}
=pod
Sinon :
ü \x81 à \x85 è \x8A
é \x82 ç \x87 ï \x8B
â \x83 ê \x88 î \x8C
ä \x84 ë \x89
=pod
Widget TEXT :
3.end The newline at the end of line 3.
1.0 + 3 chars Three characters past the start of line 1.
2.end -1 chars The last character before the new line in line 2.
end -1 chars The newline that Tk always adds at the end of the text.
end -2 chars The actual last character of the text.
end -1 lines The start of the last actual line of text.
2.2 + 2 lines The third character (index 2) of the fourth line of text.
2.5 linestart The first character of line 2.
2.5 lineend The position of the newline at the end of line 2.
2.5 wordstart The first character of the word containing the character at index 2.5.
2.5 wordend The first character just past the last character of the word containing index 2.5.
#my $words = qr/\s*(?:\w+\s+){0,5}/;
#$TexteInput =~ m/(\b.{0,40}$Pattern.{0,40}\b)/gx); |
Partager