Export / import previous state to / from env

* State will be exported to the GPROMPT_STATE environment variable
  * Using the prevstate argument will load the previous state
  * Ctrl+L now redraws based on the previous state
This commit is contained in:
Emmanuel BENOîT 2022-10-29 15:03:52 +02:00
parent 2a1cf31d4d
commit a4e6b8b4a0

View file

@ -14,6 +14,7 @@ use utf8;
use open ':std', ':encoding(UTF-8)'; use open ':std', ':encoding(UTF-8)';
use POSIX qw(strftime :termios_h); use POSIX qw(strftime :termios_h);
use Cwd qw(abs_path getcwd); use Cwd qw(abs_path getcwd);
use Data::Dumper;
# DEFAULT CONFIGURATION ====================================================={{{ # DEFAULT CONFIGURATION ====================================================={{{
@ -863,9 +864,9 @@ gprompt_command() {
} }
_gprompt_clear() { _gprompt_clear() {
clear -x clear -x
eval "\$( perl \Q$gpPath\E prevstate )"
echo -ne "\${PS1\@P}\\r"
_gprompt_set_return "\${_GPROMPT_PREV_STATUS:-0}" _gprompt_set_return "\${_GPROMPT_PREV_STATUS:-0}"
gprompt_command
echo -n "\${PS1\@P}\r"
} }
shopt -s checkwinsize shopt -s checkwinsize
if [[ \$PROMPT_COMMAND != *"gprompt_command"* ]]; then if [[ \$PROMPT_COMMAND != *"gprompt_command"* ]]; then
@ -880,20 +881,29 @@ EOF
sub readArguments sub readArguments
{ {
printBashInit if @ARGV == 1 && $ARGV[0] eq 'init'; printBashInit if @ARGV == 1 && $ARGV[0] eq 'init';
my $state = {};
if (@ARGV == 1 && $ARGV[0] =~ /^\d+$/) { if (@ARGV == 1 && $ARGV[0] =~ /^\d+$/) {
# Backward compatibility # Backward compatibility
$INPUT{rc} = $ARGV[0]; $INPUT{rc} = $ARGV[0];
} else { } else {
if (@ARGV && $ARGV[0] eq 'prevstate') {
shift @ARGV;
$state = eval $ENV{GPROMPT_STATE};
$state = {} if $!;
}
foreach my $arg (@ARGV) { foreach my $arg (@ARGV) {
next unless $arg =~ /^([a-z]+):(.*)$/; next unless $arg =~ /^([a-z]+):(.*)$/;
$INPUT{$1} = $2; $INPUT{$1} = $2;
} }
} }
return $state;
} }
sub main sub main
{ {
readArguments; my $state = readArguments;
$HASCWD = defined( getcwd ); $HASCWD = defined( getcwd );
chdir '/' unless $HASCWD; chdir '/' unless $HASCWD;
@ -903,7 +913,6 @@ sub main
$RESET = $CONFIG{cfg_sgr0_reset} ? tput_sequence( 'sgr0' ) : "\033[0m"; $RESET = $CONFIG{cfg_sgr0_reset} ? tput_sequence( 'sgr0' ) : "\033[0m";
$RESET = '\\[' . $RESET . '\\]'; $RESET = '\\[' . $RESET . '\\]';
%TLEN = compute_trans_lengths; %TLEN = compute_trans_lengths;
my $state = {};
my $pg = gen_term_title( $state ); my $pg = gen_term_title( $state );
my $ps1 = $pg; my $ps1 = $pg;
$ps1 .= gen_empty_line( $state ); $ps1 .= gen_empty_line( $state );
@ -911,7 +920,10 @@ sub main
my ( $ill , $ilt ) = gen_input_line( $state ); my ( $ill , $ilt ) = gen_input_line( $state );
$ps1 .= $ilt; $ps1 .= $ilt;
my $ps2 = $pg . gen_ps2( $ill ); my $ps2 = $pg . gen_ps2( $ill );
print "export PS1=\"$ps1\" PS2=\"$ps2\"\n"; $Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 0;
$state = Dumper($state);
print "export PS1=\"$ps1\" PS2=\"$ps2\" GPROMPT_STATE=\Q$state\E\n";
} }
main; main;