Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mdom committed Jan 28, 2019
1 parent 802503d commit 4e93d03
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ App::termpub - read epubs in the terminal

# SYNOPSIS

termpub _file_...
termpub _file_

# DESCRIPTION

termpub is a _terminal_ viewer for epubs. At startup termpub displays
the first chapter with real content if possible.
termpub is a _terminal_ viewer for epubs.

At startup termpub displays the first chapter with real content if
possible. It will save your reading position and restore it.

# KEY BINDINGS

Expand All @@ -30,6 +32,10 @@ the first chapter with real content if possible.

Jump to the table of contents.

- \[num\] %

Go to a line N percent into the chapter.

- \[num\] g

Go to line _num_ in the chapter, defaults to 1.
Expand Down
6 changes: 3 additions & 3 deletions lib/App/termpub/Epub.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ has archive => sub {
my $filename = $self->filename;
my $archive = Archive::Zip->new;
if ( eval { $archive->read($filename) } != AZ_OK ) {
die "Can't read $filename\n";
die "$filename can't be unzipped (is it an epub file?)\n";
}
my $mimetype = $archive->contents('mimetype');
$mimetype =~ s/[\r\n]+//;
if ( !$mimetype ) {
die "Missing mimetype for $filename\n";
die "Missing mimetype for $filename (is it an epub file?)\n";
}
if ( $mimetype ne 'application/epub+zip' ) {
die "Unknown mimetype $mimetype for $filename\n";
die "Unknown mimetype $mimetype for $filename (is it an epub file?)\n";
}
return $archive;
};
Expand Down
35 changes: 25 additions & 10 deletions script/termpub
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,35 @@ use Curses;
use lib 'lib';
use App::termpub;
use App::termpub::Epub;
use Pod::Usage;

initscr;
noecho;
cbreak;
curs_set(0);

for my $filename (@ARGV) {
my $filename = shift;

my $epub = eval { App::termpub::Epub->new( filename => $filename ) };
if ( !$filename ) {
endwin;
pod2usage(
-message => 'Missing file',
-exitstatus => 1,
-verbose => 0,
);
}

my $epub = App::termpub::Epub->new( filename => $filename );

if ( !$epub ) {
warn "$@\n";
next;
}
eval { App::termpub->new( epub => $epub )->run };

App::termpub->new( epub => $epub )->run;
if ($@) {
endwin;
pod2usage(
-message => $@,
-exitstatus => 1,
-verbose => 0,
);
}

END {
Expand All @@ -40,12 +53,14 @@ App::termpub - read epubs in the terminal
=head1 SYNOPSIS
termpub I<file>...
termpub I<file>
=head1 DESCRIPTION
termpub is a I<terminal> viewer for epubs. At startup termpub displays
the first chapter with real content if possible.
termpub is a I<terminal> viewer for epubs.
At startup termpub displays the first chapter with real content if
possible. It will save your reading position and restore it.
=head1 KEY BINDINGS
Expand Down

0 comments on commit 4e93d03

Please sign in to comment.