Kephra / lib / run.pl

#!usr/bin/perl
use v5.12;
use warnings;

MinimalEditor->new->MainLoop;

package MinimalEditor;
use Wx qw/ :everything /;
use base qw(Wx::App);
use Wx::STC;
use Encode::Guess;

sub OnInit {
	my $app   = shift;
	my $frame = Wx::Frame->new( undef, wxDEFAULT, __PACKAGE__. 'Kephra xp testbed step 1', [-1,-1], [1000,800]);
	my $ed = Wx::StyledTextCtrl->new($frame, -1);
	my ($file, $encoding);

	Wx::Event::EVT_KEY_DOWN($ed , sub {
		my ($ed, $event) = @_;
		my $code = $event->GetUnicodeKey;
		if ($code == 79 and $event->ControlDown){
			$file = Wx::FileSelector('Open File ...', '.', '', '', '(*)|*', &Wx::wxFD_OPEN, $frame);
			return unless $file and -r $file;
			CORE::open my $FH, '<', $file;
			binmode($FH);
			my $content = do { local $/; <$FH> };
			if ($content) {
				my $guess = Encode::Guess::guess_encoding( $content, qw/utf-8 iso8859-1 latin1/ );
				if ( ref($guess) and ref($guess) =~ m/^Encode::/ ) { $encoding = $guess->name }
				elsif                   ( $guess =~ m/utf8/ )      { $encoding = 'utf-8' }
				elsif                   ( $guess =~ m/or/ )        { $encoding = ( split(/\sor\s/, $guess) )[0] } 
				else { $encoding = 'utf-8' }
				$ed->SetText( Encode::decode( $encoding,  $content ) );
			}
		}
		elsif ($code == 81 and $event->ControlDown){ $frame->Close }
		elsif ($code == 83 and $event->ControlDown){
			return unless $file and -w $file;
			CORE::open my $FH, '> :raw :encoding('.$encoding.')', $file;
			print $FH $ed->GetText();
		} 
		else { $event->Skip }
	});

	$frame->Show(1);
	$app->SetTopWindow($frame);
	1;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.