From 6169229b129ad6c65629114a394241468828d9e2 Mon Sep 17 00:00:00 2001
From: jake <jake@jakes-mail.top>
Date: Sat, 15 Jan 2022 08:25:04 -0500
Subject: Add help, some commands, and aliases. Made do_command() less dumb

---
 gmi.pl | 99 ++++++++++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 73 insertions(+), 26 deletions(-)

diff --git a/gmi.pl b/gmi.pl
index c397eb0..2de0eb8 100755
--- a/gmi.pl
+++ b/gmi.pl
@@ -9,7 +9,7 @@ use bytes;
 use feature qw(refaliasing);
 no warnings qw(experimental::refaliasing);
 
-our $VERSION = 'v0.0.13';
+our $VERSION = 'v0.0.14';
 
 # TODO: 
 # back() only works once; should fix this
@@ -37,24 +37,50 @@ 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 { ; },
+	'url' => [sub { url($_[0]) }, 'Go to the specified URL.'],
+	'u' => [sub { url($_[0]) }, 'Alias of `url\''],
+
+	'cwu' => [sub { cwu() }, 'Returns the current working URL.'],
+	'c' => [sub { cwu() }, 'Alias of `cwu\'.'],
+
+	'links' => [sub { links() }, 'Returns the links on the current page.'],
+	'l' => [sub { links() }, 'Alias of `links\'.'],
+
+	'nav' => [sub { nav(0, $_[0]) }, 'Navigate to a link that is on the current page (use links)'],
+	'n' => [sub { nav(0, $_[0]) }, 'Alias of `nav\'.'],
+
+	'navh' => [sub { nav(1, $_[0]) }, 'Navigate to a URL specified from history (use hist)'],
+	'nh' => [sub { nav(1, $_[0]) }, 'Alias of `navh\'.'],
+
+	'root' => [sub { root() }, 'Go to the root of the current URL.\n' .
+		'Exp: gemini://capsule.com/blog/2020112.gmi -> gemini://capsule.com/' ],
+
+	'hist' => [sub { hist() }, 'Returns history.' ],
+	'h' => [sub { hist() }, 'Alias of `hist\'.' ],
+
+	'back' => [sub { back(0) }, 'Go back in history.' ],
+	'b' => [sub { back(0) }, 'Alias of `back\'.' ],
+
+	'backnodisplay' => [sub { back(1) }, 'Go back in history but do not display the page' .
+		'(still accesses the resource).' ],
+	'bd' => [sub { back(1) }, 'Alias of `backnodisplay\'.'],
+
+	'display' => [sub { display() }, 'Display the current page.' ],
+	'd' => [sub { display() }, 'Alias of `display\'.' ],
+
+	'aaa' => [sub { aaa() }, "This command returns `aaa'." ],
+	'usepager' => [sub { toggle('pager') }, 'This toggles pager use. 1 = use pager, 0 = stdout.' ],
+	'textwrap' => [sub { textwrap($_[0]) }, 'Set textwrap length.\nGiving \'auto\' will ' .
+		"automatically determine the appropriate length. (Currently $wrapper->{columns})"],
+	'pager' => [sub { pager($_[0]) }, "Set which pager to use. (currently $ENV{pager})"],
+	'save' => [sub { ; }, 'Save the config settings. (Not yet implimented)'],
+	'help' => [sub { help($_[0]); }, 'Use help before a command to get extra info.'],
+	'cmds' => [sub {cmds()}, 'Returns the available commands.'],
+	'ver' => [sub {print "$VERSION\n"}, "Returns the version ($VERSION)"],
+	'exit' => [sub {exit 0;}, "This exits the program with a status code of 0."],
 );
 
+print "Type 'cmds' to see available commands.\n";
 while () {
 
 	print "$prompt";
@@ -62,31 +88,44 @@ while () {
 
 	### $input
 	if ($input) {
-		my ($command, $option) = split(/\s/, $input);
-		do_command(\$command, \%commands, \$option);
+		my ($command, $detail) = split(/\s/, $input);
+		do_command(\$command, \$detail);
 	}
 }
 
 sub do_command {
-	# referenced command, referenced options, referenced option
-	my ($rCmd, $rhCommands, $rOption) = @_; 
+	# referenced command, referenced detail 
+	my ($rCmd, $rDetail) = @_; 
 
-	# checking if options even has the command
-	if (exists $rhCommands->{$$rCmd}) {
+	### @_
+	# checking if %commands even has the command
+	if (exists $commands{$$rCmd}) {
 
 		# assigning a reference to a subroutine to the value
-		my $rsub = $rhCommands->{$$rCmd};
+		my $rsub = $commands{$$rCmd}[0];
 
 		#running the subroutine
-		&$rsub($$rOption);
+		&$rsub($$rDetail);
 	}
 	# options doesn't have the command
 	else {
-		### @_
 		print "Command is invalid.\n";
 	}	
 }
 
+sub help {
+	my ($cmd) = @_;
+	### @_
+	if ($cmd and exists $commands{$cmd}) {
+		print "$commands{$cmd}[1]\n";
+	} elsif ($cmd) {
+		print "`$cmd' isn't an avaliable command.\n";
+	}
+	else {
+		print "$commands{help}[1]\n"
+	}
+}
+
 sub url {
 	my ($url) = @_;
 	if (!$url) { print("Example: [gemini://]capsule.com/\n"); return 0;}
@@ -342,3 +381,11 @@ sub pager {
 		print("$ENV{pager}\n");
 	}
 }
+
+sub cmds {
+	my $c;
+	for (sort keys %commands) {
+		$c .= "$_ ";
+	}
+	print $wrapper->wrap($c);
+}
-- 
cgit v1.2.3