diff options
Diffstat (limited to 'gmi.pl')
-rwxr-xr-x | gmi.pl | 110 |
1 files changed, 70 insertions, 40 deletions
@@ -10,7 +10,7 @@ use warnings; use feature qw(refaliasing); no warnings qw(experimental::refaliasing); -our $VERSION = 'v0.0.31'; +our $VERSION = 'v0.0.32'; # TODO: # back() only works once; should fix this @@ -28,33 +28,36 @@ use Term::ReadLine; # CPAN use Smart::Comments; # CPAN use URI::Encode qw(uri_encode); # CPAN use IO::Socket::SSL::Utils; # CPAN -use File::Slurper qw(read_dir); # CPAN +use File::Slurper qw(read_dir read_text); # CPAN use Data::Dumper; -use File::BaseDir qw(xdg_config_home xdg_data_home xdg_cache_home); +use File::BaseDir qw(xdg_config_home xdg_data_home xdg_cache_home); # CPAN +use TOML qw(from_toml to_toml); # CPAN # sudo cpanm IO::Socket::SSL URL::XS IO::Pager Text::Wraper Term::ReadKey Path::Naive Text::ParseWords Term::ReadLine Smart::Comments URI::Encode IO::Socket::SSL::Utils File::Slurper File::BaseDir -my $wrapper = Text::Wrapper->new(columns=>70, body_start=>''); -#$ENV{PAGER} = 'most'; -$ENV{PAGER} = 'less -R'; -my $use_pager = 1; -my $pager_text_wrap_auto = 1; -my $doc_out = 1; # display doc for human consumption? +my %config = ( + 'use_pager' => 1, + 'pager_text_wrap_auto' => 1, + 'textwrap' => 70, # not used if pager_text_warp_auto is 1 + 'timeout' => 10, + 'pretty_links' => 1, + 'pretty_headers' => 1, + 'auto_redirect' => 3, + 'pager' => 'less -R', + 'doc_out' => 1, +); +$ENV{PAGER} = $config{pager}; my @doc; my @links; my $current_url = ""; my @history; my $prompt = ""; -my $pretty_links = 1; -my $pretty_headers = 1; my $pre_block = 0; my @status_code; my $status_code1; my $status_code2; -my $auto_redirect = 2; my $auto_redirect_count = 0; my $sleep; -my $timeout = 3; my $use_cert = 0; my $cert; my $key; @@ -77,6 +80,9 @@ if (! -e $xdg_cache) { ### $xdg_data ### $xdg_cache +load_config(); +my $wrapper = Text::Wrapper->new(columns=>$config{textwrap}, body_start=>''); + my $term=new Term::ReadLine "hmm, what goes here?"; my $OUT = $term->OUT || \*STDOUT; @@ -124,21 +130,23 @@ my %commands = ( 'display' => [sub { display() }, 'Display the current page.' ], 'd' => [sub { display() }, 'Alias of `display\'.' ], - 'usepager' => [sub { toggle($use_pager) }, 'This toggles pager use. 1 = use pager, 0 = stdout.' ], - 'pretty_links' => [sub { toggle($pretty_links) }, "0 = do nothing, 1 = pretty."], - 'pretty_headers' => [sub { toggle($pretty_headers) }, "0 = do nothing, 1 = pretty."], + 'usepager' => [sub { toggle($config{use_pager}) }, 'This toggles pager use. 1 = use pager, 0 = stdout.' ], + 'pretty_links' => [sub { toggle($config{pretty_links}) }, "0 = do nothing, 1 = pretty."], + 'pretty_headers' => [sub { toggle($config{pretty_headers}) }, "0 = do nothing, 1 = pretty."], 'textwrap' => [sub { textwrap($_[0]) }, "Set textwrap length.\nGiving 'auto' will " . "automatically determine the appropriate length."], - 'pager' => [sub { pager($_[0]) }, "Set which pager to use. (currently $ENV{PAGER})"], - 'save' => [sub { ; }, 'Save the config settings. (Not yet implimented)'], + 'pager' => [sub { pager($_[0]) }, "Set which pager to use. (currently $config{pager})"], + 'save-config' => [sub { save_config() }, 'Save the config settings.'], + 'load-config' => [sub { load_config() }, "Load the config settings.\n" . + 'This is not needed as the configs are loaded when the program is started.' ], 'help' => [sub { help($_[0]); }, 'Use help before a command to get extra info.' . '(<tab><tab> to see commands)'], 'ver' => [sub {print $OUT "$VERSION\n"}, "Returns the version ($VERSION)"], 'exit' => [sub {exit 0;}, "This exits the program with a status code of 0."], - 'autoredirection' => [sub { value_number($auto_redirect,$_[0],'autoredirect') }, + 'autoredirection' => [sub { value_number($config{auto_redirect},$_[0],'autoredirect') }, "The amount of times this program is allowed to auto redirect. 0 = none."], - 'timeout' => [sub { value_number($timeout,$_[0],'timeout') }, + 'timeout' => [sub { value_number($config{timeout},$_[0],'timeout') }, "The client will time out when the server takes too long." ], 'clearquery' => [sub { clear_query() }, "Removes query from current URL"], @@ -217,7 +225,7 @@ sub url { }; if ($@) { print $OUT "$@\n"; - return 1; + return 0; } ### $url @@ -266,7 +274,7 @@ sub url { eval { local $SIG{ALRM} = sub {close ($cl); unshift(@doc,""); die "TIMEOUT";}; - alarm $timeout; + alarm $config{timeout}; # gemini spec: <URL><CR><LF> # <URL> is an absolute path @@ -280,7 +288,7 @@ sub url { }; if ($@) { - print $OUT "Timed out after $timeout seconds - server is taking too long.\n"; + print $OUT "Timed out after $config{timeout} seconds - server is taking too long.\n"; update_history( ["$current_url", "timed out"] ); ### @doc } else { @@ -386,10 +394,10 @@ sub follow_status_code { shift(@doc); # user probably doesn't want to see the 20 */* every time # at some point deal with image/* and other media get_links(); - pretty_links() if ($pretty_links); - pretty_headers() if ($pretty_headers); + pretty_links() if ($config{pretty_links}); + pretty_headers() if ($config{pretty_headers}); - if ($doc_out) { + if ($config{doc_out}) { display(); } } @@ -416,9 +424,9 @@ sub follow_status_code { } elsif ($status_code1 == 3) { print $bad_code if ($status_code2 !~ m/^0|1$/); # not worrying about permanent/temporary yet print $OUT "Redirecting anyway.\n" if ($status_code2 !~ m/^0|1$/); - if ($auto_redirect) { + if ($config{auto_redirect}) { $auto_redirect_count++; - if ($auto_redirect_count < $auto_redirect) { + if ($auto_redirect_count < $config{auto_redirect}) { print $OUT "Redirection...\n"; ### @status_code access_resource($status_code[1]); # redirection can be './hello.gmi' @@ -767,9 +775,9 @@ sub back { ### @_ ### $display - my $dok_out = $doc_out; + my $dok_out = $config{doc_out}; - $doc_out = 0 if ($display == 1); + $config{doc_out} = 0 if ($display == 1); if (@history >= 2) { url("$history[-2][0]"); @@ -779,15 +787,15 @@ sub back { } ### @history - $doc_out = $dok_out if ($display == 1); + $config{doc_out} = $dok_out if ($display == 1); } sub display { - if ($pager_text_wrap_auto) { + if ($config{pager_text_wrap_auto}) { my ($wc) = GetTerminalSize(); $wrapper->columns($wc); } - if ($use_pager) { + if ($config{use_pager}) { ### opening IO Pager eval { # catch it or big files ( >1000 lines ) will load but the script itself dies for some reason @@ -835,18 +843,19 @@ sub toggle { sub textwrap { my ($c) = @_; if ($c and $c =~ m/^\d+$/) { + $config{textwrap} = $c; $wrapper->columns($c); - $pager_text_wrap_auto = 0; + $config{pager_text_wrap_auto} = 0; } elsif ($c and $c eq 'auto') { - $pager_text_wrap_auto = 1; + $config{pager_text_wrap_auto} = 1; } else { - if ($pager_text_wrap_auto) { + if ($config{pager_text_wrap_auto}) { my ($c) = GetTerminalSize(); print("[AUTO] $c\n"); } else { - print("$wrapper->{columns}\n"); + print "$config{textwrap}\n"; } } } @@ -854,11 +863,11 @@ sub textwrap { sub pager { my ($p) = @_; if ($p) { - $ENV{pager} = $p; - 0; + $config{pager} = $p; + print $OUT "save-config, and restart the program for change to take effect.\n"; } else { - print("$ENV{pager}\n"); + print $OUT "$config{pager}\n"; } } @@ -1087,3 +1096,24 @@ sub cert_del { print $OUT "Nothing is deleted.\n"; } } + +sub save_config { + my $toml = to_toml(\%config); + open(my $FH, '>', "$xdg_config/config.toml"); + print $FH $toml; + close $FH; +} + +sub load_config { + my ($config, $err); + if (-e "$xdg_config/config.toml") { + ($config, $err) = from_toml(read_text("$xdg_config/config.toml")); + unless ($config) { + print $OUT "Error parsing toml: $err\n"; + } + } + ### $config + for (keys %$config) { + $config{$_} = %$config{$_}; + } +} |