From 4e93d03af656c7e13e83c29d623f0191f1140693 Mon Sep 17 00:00:00 2001 From: Mario Domgoergen Date: Mon, 28 Jan 2019 20:58:05 +0100 Subject: [PATCH] Improve error handling --- README.md | 12 +++++++++--- lib/App/termpub/Epub.pm | 6 +++--- script/termpub | 35 +++++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 6050489..82a502e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/lib/App/termpub/Epub.pm b/lib/App/termpub/Epub.pm index d295041..86f3857 100644 --- a/lib/App/termpub/Epub.pm +++ b/lib/App/termpub/Epub.pm @@ -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; }; diff --git a/script/termpub b/script/termpub index 064a171..b7569de 100755 --- a/script/termpub +++ b/script/termpub @@ -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 { @@ -40,12 +53,14 @@ App::termpub - read epubs in the terminal =head1 SYNOPSIS -termpub I... +termpub I =head1 DESCRIPTION -termpub is a I viewer for epubs. At startup termpub displays -the first chapter with real content if possible. +termpub is a I 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