From 31e894411a74c4cddfb1edcfb6fa5c3769d87fb6 Mon Sep 17 00:00:00 2001 From: jake Date: Mon, 31 Jan 2022 17:50:52 -0500 Subject: Save history to disk. also clear history command --- gmi.pl | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'gmi.pl') diff --git a/gmi.pl b/gmi.pl index d61353c..0ff093c 100755 --- a/gmi.pl +++ b/gmi.pl @@ -45,6 +45,7 @@ my %config = ( 'auto_redirect' => 3, 'pager' => 'less -R', 'doc_out' => 1, + 'keep_history' => 0, ); my @doc; my @links; @@ -142,7 +143,7 @@ my %commands = ( 'help' => [sub { help($_[0]); }, 'Use help before a command to get extra info.' . '( 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."], + 'exit' => [sub {begin_exit();}, "This exits the program with a status code of 0."], 'autoredirection' => [sub { value_number($config{auto_redirect},$_[0],'autoredirect') }, "The amount of times this program is allowed to auto redirect. 0 = none."], @@ -159,6 +160,10 @@ my %commands = ( 'cert-use' => [sub { cert_use() }, "Use a cert. Interactive.\n" . "If a cert has already been loaded, it will unload it."], 'cert-del' => [sub {cert_del() }, "Delete a cert. Interactive." ], + + 'keep-history' => [sub {toggle($config{keep_history})}, 'Toggle whether to save history to disk or not,' . + ' after \'exit\'. By default it does not.' ], + 'clear-history' => [sub { undef(@history) }, 'Clears your history. (The file itself is very parsable)' ], ); my @completions = keys %commands; @@ -648,7 +653,9 @@ sub get_links { sub hist { my $hist; my $counter = 1; + ### @history for my $item (@history) { + ### $item $hist .= "[$counter] ". colored("$$item[0]", 'underline') ." $$item[1]\n"; $counter++; } @@ -1123,4 +1130,25 @@ sub load_config { for (keys %$config) { $config{$_} = %$config{$_}; } + if (-e "$xdg_data/history") { + open(my $FH, '<', "$xdg_data/history"); + while (<$FH>) { + my @h = split(' ',$_); + push(@history, [@h]); + } + close $FH; + } + ### @history +} + +sub begin_exit { + if ($config{keep_history}) { + # over writes the history with old and new + open(my $FH, '>', "$xdg_data/history"); + for my $item (@history) { + print $FH "$$item[0] $$item[1]\n"; + } + close $FH; + } + exit 0; } -- cgit v1.2.3