diff options
Diffstat (limited to 'gmi.pl')
-rwxr-xr-x | gmi.pl | 144 |
1 files changed, 66 insertions, 78 deletions
@@ -9,22 +9,21 @@ use bytes; use feature qw(refaliasing); no warnings qw(experimental::refaliasing); -our $VERSION = 'v0.0.12'; +our $VERSION = 'v0.0.13'; # TODO: # back() only works once; should fix this # url() should also handle relative paths # Modules -use IO::Socket::SSL; # CPAN +use IO::Socket::SSL; # CPAN use URL::XS qw(parse_url split_url_path parse_url_query); # CPAN -use Term::TUI qw(:all); # CPAN -use IO::Pager; # CPAN -require Text::Wrapper; # CPAN -use Term::ReadKey; # CPAN -use Term::ANSIColor; # Core -use Path::Naive qw(normalize_path); # CPAN -use Smart::Comments; # CPAN +use IO::Pager; # CPAN +require Text::Wrapper; # CPAN +use Term::ReadKey; # CPAN +use Term::ANSIColor; # Core +use Path::Naive qw(normalize_path); # CPAN +use Smart::Comments; # CPAN my $wrapper = Text::Wrapper->new(columns=>70, body_start=>''); $ENV{pager} = 'less'; @@ -35,73 +34,62 @@ my $doc; my @links; my $current_url = ""; my @history; +my $prompt = "gmi> "; + +my %commands = ( + 'url' => sub { url($_[0]) }, + 'cwu' => sub { cwu() }, + 'links' => sub { links() }, + 'nav' => sub { nav(0, $_[0]) }, + 'navh' => sub { nav(1, $_[0]) }, + 'root' => sub { root() }, + 'hist' => sub { hist() }, + 'back' => sub { back(0) }, + 'backnodisplay' => sub { back(1) }, + 'display' => sub { display() }, + 'aaa' => sub { aaa() }, + 'usepager' => sub { toggle('pager') }, + 'textwrap' => sub { textwrap($_[0]) }, + 'pager' => sub { pager($_[0]) }, + 'save' => sub { ; }, + 'help' => sub { ; }, +); -# The way Term::TUI handles the data makes it impossible -# to just give it a variable, but it does support stdin -my %modes = -(".HELP" => "Super basic gemini glient.\nVersion: $VERSION\n" . - "Type 'go' to enter 'go' mode. You can also: 'go url capsule.com' without being in 'go' mode.", - - "go" => {".HELP" => "going to browse", - - "url" => [ "Make a request to a URL\nThis will set the current working URL" . - "\nExample: [gemini://]capsule.com/", \&url ], - "u" => ["Alias of `url'", \&url ], - - "cwu" => ["Returns current working URL", \&cwu ], - "c" => ["Alias of `cwu'", \&cwu ], - - "links" => ["Current working URL's links", \&links ], - "l" => ["Alias of `links'", \&links ], - - "nav" => ["Follow a link.\nSets current working URL", \&nav, 0 ], - "n" => ["Alias of `nav'", \&nav, 0 ], - - "navh" => ["Follow a link from history.\nSets current working URL", \&nav, 1 ], - "nh" => ["Alias of `navh'", \&nav, 1 ], - - "root" => ["Navigate to current working URL root\n" . - "E.G. gemini://capsule.com/blog/20220112.gmi -> gemini://capsule.com/", \&root ], - - "hist" => ["Returns history.", \&hist ], - "h" => ["Alias of `hist'", \&hist ], - - "back" => ["Go back to the previous URL (uses history)", \&back, 0 ], - "b" => ["Alias of `back'", \&back, 0 ], +while () { - "backnodisplay" => ["Go back to the previous URL (uses history), ". - "but do not display the page.", \&back, 1 ], - "bd" => ["Alias of `backnodisplay'.", \&back, 1 ], + print "$prompt"; + chomp(my $input = <STDIN>); - "display" => ["Display the current working URL.", \&display] , - "d" => ["Alias of `display'", \&display ], + ### $input + if ($input) { + my ($command, $option) = split(/\s/, $input); + do_command(\$command, \%commands, \$option); + } +} - }, - "cert" => {".HELP" => "Manage certs", - "aaa" => [ "This command returns `aaa'.", \&aaa ], - }, - "config" => {".HELP" => "Config settings to your liking.", - "usepager" => ["This toggles pager use.\n" . - "1 = use pager, 0 = stdout", \&toggle, 'pager'], +sub do_command { + # referenced command, referenced options, referenced option + my ($rCmd, $rhCommands, $rOption) = @_; - "textwrap" => ["Input must be an interger or 'auto'. No\n". - "input returns the current text-wrap.", \&textwrap], + # checking if options even has the command + if (exists $rhCommands->{$$rCmd}) { - "pager" => ["Set which pager to use.", \&pager], + # assigning a reference to a subroutine to the value + my $rsub = $rhCommands->{$$rCmd}; - "save" => ["Save your settings (not implimented yet)", \&asdf], + #running the subroutine + &$rsub($$rOption); } -); - -my $flag; -$flag=TUI_Run("gc",\%modes); -print "*** ABORT ***\n" if ($flag); - -#TUI_Script(\%modes,"/config pager;"); + # options doesn't have the command + else { + ### @_ + print "Command is invalid.\n"; + } +} sub url { my ($url) = @_; - if (!$url) { TUI_Out("Example: [gemini://]capsule.com/\n"); return 0;} + if (!$url) { print("Example: [gemini://]capsule.com/\n"); return 0;} if ( $url !~ m|^.*://.*|) { $url = "gemini://$url"; } @@ -150,7 +138,7 @@ sub url { } } else { - TUI_Out("error=$!, ssl_error=$SSL_ERROR\n"); + print("error=$!, ssl_error=$SSL_ERROR\n"); } } @@ -178,10 +166,10 @@ sub hist { $counter++; } if ($hist) { - TUI_Out($hist); + print($hist); } else { - TUI_Out("\n"); + print("\n"); } } @@ -198,16 +186,16 @@ sub links { $counter++; } if ($d) { - TUI_Out($d); + print($d); } else { - TUI_Out("No links.\n"); + print("No links.\n"); } 0; } sub cwu { - TUI_Out("$current_url\n"); + print("$current_url\n"); 0; } @@ -219,7 +207,7 @@ sub nav { ### $setting ### @_ if (! $n or $n !~ /\d+/) { - TUI_Out("Try 'links' or 'hist' then 'nav x', where x is the number.\n"); + print("Try 'links' or 'hist' then 'nav x', where x is the number.\n"); return 1; } $n -= 1; @@ -232,7 +220,7 @@ sub nav { $link = $history[$n][0]; } else { - TUI_Out("'$n' isn't available.\n"); + print("'$n' isn't available.\n"); return 1; } @@ -289,7 +277,7 @@ sub back { } sub aaa { - TUI_Out("aaa\n"); + print("aaa\n"); } sub display { @@ -302,7 +290,7 @@ sub display { print $FH $wrapper->wrap($doc); } else { - TUI_Out($wrapper->wrap($doc)); + print($wrapper->wrap($doc)); } } @@ -336,10 +324,10 @@ sub textwrap { else { if ($pager_text_wrap_auto) { my ($c) = GetTerminalSize(); - TUI_Out("[AUTO] $c\n"); + print("[AUTO] $c\n"); } else { - TUI_Out("$wrapper->{columns}\n"); + print("$wrapper->{columns}\n"); } } } @@ -351,6 +339,6 @@ sub pager { 0; } else { - TUI_Out("$ENV{pager}\n"); + print("$ENV{pager}\n"); } } |