From fe31b630e995854cc1dcd57de311796f08868d41 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Wed, 24 Sep 2025 10:50:25 -0700 Subject: [PATCH] applets/pbot-vm: Multiple improvements - host/lib/SplitLine: preserve \t tabs - host/lib/InteractiveEdit: encode history as JSON to preserve newlines - host/lib/Languages/_default: strip joined space following newlines --- applets/pbot-vm/host/lib/InteractiveEdit.pm | 39 +++++++++++++++---- .../pbot-vm/host/lib/Languages/_default.pm | 3 +- applets/pbot-vm/host/lib/SplitLine.pm | 2 +- lib/PBot/VERSION.pm | 4 +- 4 files changed, 36 insertions(+), 12 deletions(-) diff --git a/applets/pbot-vm/host/lib/InteractiveEdit.pm b/applets/pbot-vm/host/lib/InteractiveEdit.pm index 822a5fa0..89830701 100644 --- a/applets/pbot-vm/host/lib/InteractiveEdit.pm +++ b/applets/pbot-vm/host/lib/InteractiveEdit.pm @@ -13,6 +13,7 @@ no warnings qw(experimental::smartmatch experimental::signatures deprecated); use LWP::UserAgent; use FindBin qw($RealBin); use Text::Balanced qw(extract_delimited); +use JSON::XS; use parent qw(Exporter); our @EXPORT = qw(interactive_edit); @@ -31,7 +32,7 @@ sub interactive_edit($self) { my @code = load_history("$RealBin/../history/$copy-$self->{lang}.hist"); - $copy_code = $code[0]; + $copy_code = decode_entry($code[0]); if (not defined $copy_code) { print "No history for $copy.\n"; @@ -49,18 +50,18 @@ sub interactive_edit($self) { my @last_code = load_history("$RealBin/../history/$self->{channel}-$self->{lang}.hist"); - unshift @last_code, $copy_code if defined $copy_code; + unshift @last_code, encode_entry($copy_code) if defined $copy_code; if ($subcode =~ m/^\s*(?:and\s+)?show(?:\s+\S+)?\s*$/i) { if (defined $last_code[0]) { - print "$last_code[0]\n"; + print decode_entry($last_code[0]) . "\n"; } else { print "No recent code to show.\n" } exit 0; } - my $prevchange = $last_code[0]; + my $prevchange = decode_entry($last_code[0]); my @replacements; my $got_changes = 0; my $got_sub = 0; @@ -74,8 +75,9 @@ sub interactive_edit($self) { print "No more undos remaining.\n"; exit 0; } else { - $code = $last_code[0]; - $prevchange = $last_code[0]; + my $last = decode_entry($last_code[0]); + $code = $last; + $prevchange = $last; $got_undo = 1; } } @@ -464,7 +466,7 @@ sub interactive_edit($self) { unless (($self->{got_run} or $got_diff) and not $got_changes) { if ($unshift_last_code) { - unshift @last_code, $code; + unshift @last_code, encode_entry($code); } save_history("$RealBin/../history/$self->{channel}-$self->{lang}.hist", \@last_code, $self->{max_history}); @@ -475,7 +477,9 @@ sub interactive_edit($self) { print "Not enough recent code to diff.\n" } else { use Text::WordDiff; - my $diff = word_diff(\$last_code[1], \$last_code[0], { STYLE => 'Diff' }); + my $last1 = decode_entry($last_code[1]); + my $last0 = decode_entry($last_code[0]); + my $diff = word_diff(\$last1, \$last0, { STYLE => 'Diff' }); if ($diff !~ /(?:|)/) { $diff = "No difference."; @@ -525,4 +529,23 @@ sub load_history($path) { return @lines; } +sub decode_entry($entry) { + return undef if not defined $entry; + my $result = eval { + my $data = decode_json($entry); + return $data->{code}; + }; + + if ($@) { + return $entry; + } else { + return $result; + } +} + +sub encode_entry($entry) { + my $data = { code => $entry }; + return encode_json($data); +} + 1; diff --git a/applets/pbot-vm/host/lib/Languages/_default.pm b/applets/pbot-vm/host/lib/Languages/_default.pm index 3f1a9ea3..b2b7d5f3 100755 --- a/applets/pbot-vm/host/lib/Languages/_default.pm +++ b/applets/pbot-vm/host/lib/Languages/_default.pm @@ -94,6 +94,7 @@ sub process_standard_options($self) { } $self->{code} = join ' ', @opt_args; + $self->{code} =~ s/\n /\n/msg; if ($self->{code} =~ s/-stdin[ =]?(.*)$//) { $self->add_option("-stdin", $1); @@ -136,7 +137,7 @@ sub preprocess_code($self, %opts) { unless($self->{got_run} and $self->{copy_code}) { $self->debug("---- preprocess\n"); - $self->debug("$self->{nick} $self->{channel}: [$self->{arguments}] $self->{cmdline_options} $self->{code}\n", 0); + $self->debug("$self->{nick} $self->{channel}: [$self->{arguments}] $self->{cmdline_options}\n$self->{code}\n", 0); } # replace \n outside of quotes with literal newline diff --git a/applets/pbot-vm/host/lib/SplitLine.pm b/applets/pbot-vm/host/lib/SplitLine.pm index 9ff4cc7e..26de23b5 100644 --- a/applets/pbot-vm/host/lib/SplitLine.pm +++ b/applets/pbot-vm/host/lib/SplitLine.pm @@ -128,7 +128,7 @@ sub split_line($line, %opts) { $token .= $ch; next; } else { - if ($opts{keep_spaces} && $ch eq "\n") { + if ($opts{keep_spaces} && ($ch eq "\n" || $ch eq "\t")) { $token .= $ch; } diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index 3a123953..a55314ee 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,8 +25,8 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4890, - BUILD_DATE => "2025-09-14", + BUILD_REVISION => 4891, + BUILD_DATE => "2025-09-24", }; sub initialize {}