From 3ee6020f40dffe04fd69bc09ba3e1c1d3cbe0e92 Mon Sep 17 00:00:00 2001 From: Nadim Khemir Date: Tue, 28 Nov 2023 15:10:47 +0100 Subject: [PATCH] CHANGED: scripting API --- Build.PL | 1 + .../src/for_developers/scripting_api.md | 27 ++++++++++++++ lib/App/Asciio/Actions/Ruler.pm | 2 +- lib/App/Asciio/Elements.pm | 2 +- lib/App/Asciio/Scripting.pm | 36 +++++++++++++++++-- lib/App/Asciio/Server.pm | 2 +- script/stdin_to_asciio_web | 2 +- 7 files changed, 66 insertions(+), 6 deletions(-) diff --git a/Build.PL b/Build.PL index b829047d..e9f9bb4c 100644 --- a/Build.PL +++ b/Build.PL @@ -273,6 +273,7 @@ my $build = $class->new 'Cwd' => 0, 'Data::Compare' => 0, 'Data::TreeDumper' => 0.41, + 'Digest::MD5' => 0, 'Eval::Context' => 0, 'ExtUtils::PkgConfig' => 0, 'File::Basename' => 0, diff --git a/documentation/mdbook_asciio/src/for_developers/scripting_api.md b/documentation/mdbook_asciio/src/for_developers/scripting_api.md index c8072835..c6b1a9d6 100644 --- a/documentation/mdbook_asciio/src/for_developers/scripting_api.md +++ b/documentation/mdbook_asciio/src/for_developers/scripting_api.md @@ -1,5 +1,13 @@ # Simplified Scripting API +## create_undo_snapshot + +Creates an undo snapshot. + +``` +create_undo_snapshot ; +``` + ## add Adds a named element: @@ -139,6 +147,25 @@ Optimizes the connections. optimize ``` +## delete_all_ruler_lines + +Deletes all ruler lines. + +``` +delete_all_ruler_lines ; +``` + +## add_ruler_line + +Adds a ruler line. + +- axis: 'vertical' or 'horizontal' +- position + +``` +add_ruler_line 'vertical, 10 ; +``` + ## save_to Saves the diagram to a file in Asciio's native format. diff --git a/lib/App/Asciio/Actions/Ruler.pm b/lib/App/Asciio/Actions/Ruler.pm index e69d7131..59969cd0 100644 --- a/lib/App/Asciio/Actions/Ruler.pm +++ b/lib/App/Asciio/Actions/Ruler.pm @@ -44,7 +44,7 @@ $self->remove_ruler_lines ( $to_remove // ( - { TYPE => 'VERTICAL', POSITION => $self->{MOUSE_X} }, + { TYPE => 'VERTICAL', POSITION => $self->{MOUSE_X} }, { TYPE => 'HORIZONTAL', POSITION => $self->{MOUSE_Y} }, ) ) ; diff --git a/lib/App/Asciio/Elements.pm b/lib/App/Asciio/Elements.pm index bdd304bc..15a3bc98 100644 --- a/lib/App/Asciio/Elements.pm +++ b/lib/App/Asciio/Elements.pm @@ -62,7 +62,7 @@ for my $ruler_line_to_remove (@ruler_lines_to_remove) { if ( - $ruler_line->{TYPE} eq $ruler_line_to_remove->{TYPE} + $ruler_line->{TYPE} eq $ruler_line_to_remove->{TYPE} && $ruler_line->{POSITION} == $ruler_line_to_remove->{POSITION} ) { diff --git a/lib/App/Asciio/Scripting.pm b/lib/App/Asciio/Scripting.pm index d3c96f9c..d06f363e 100644 --- a/lib/App/Asciio/Scripting.pm +++ b/lib/App/Asciio/Scripting.pm @@ -5,6 +5,7 @@ require Exporter ; @ISA = qw(Exporter) ; @EXPORT = qw( update_display + create_undo_snapshot add add_type @@ -25,6 +26,9 @@ require Exporter ; connect_elements optimize + delete_all_ruler_lines + add_ruler_line + save_to to_ascii ascii_out @@ -55,6 +59,8 @@ use App::Asciio::stripes::section_wirl_arrow ; use App::Asciio::stripes::stripes ; use App::Asciio::stripes::wirl_arrow ; +use Digest::MD5 qw(md5_hex) ; + use Data::TreeDumper ; sub ddt { print DumpTree @_ ; } @@ -69,11 +75,12 @@ my %name_to_element ; sub run_external_script_text { -my ($asciio, $script) = @_ ; +my ($asciio, $script, $show_script) = @_ ; if(defined $script) { - print "Asciio: script: $script\n" ; + print "Asciio: script: " . md5_hex($script) . "\n" ; + print "$script\n" if $show_script ; $script_asciio = $asciio ; @@ -133,6 +140,11 @@ new_script_asciio() ; #-------------------------------------------------------------------------------------------- +sub create_undo_snapshot +{ +$script_asciio->create_undo_snapshot() ; +} + sub add { my ($name, $element, $x, $y) = @_ ; @@ -200,6 +212,26 @@ sub select_all_script_elements { $script_asciio->select_elements(1, values %na sub deselect_all_elements { $script_asciio->deselect_all_elements() ; } sub deselect_all_script_elements { $script_asciio->select_elements(0, values %name_to_element) ; } +sub delete_all_ruler_lines { delete $script_asciio->{RULER_LINES} ; } ; + +sub add_ruler_line +{ +my ($axis, $position) = @_ ; + +my $data ; + +if($axis eq 'vertical') + { + $data = {TYPE => 'VERTICAL', POSITION => $position} ; + } +else + { + $data = {TYPE => 'hORIZONTAL', POSITION => $position} ; + } + +$script_asciio->add_ruler_lines({NAME => 'from script', %{$data}}) ; +} + sub save_to { $script_asciio->save_with_type(undef, 'asciio', $_[0]) ; } sub to_ascii { $script_asciio->transform_elements_to_ascii_buffer() ; } sub ascii_out { print $script_asciio->transform_elements_to_ascii_buffer() ; } diff --git a/lib/App/Asciio/Server.pm b/lib/App/Asciio/Server.pm index c8fb8cf2..da1edaca 100644 --- a/lib/App/Asciio/Server.pm +++ b/lib/App/Asciio/Server.pm @@ -186,7 +186,7 @@ my $path = $request->{PATH} ; my $parameters = $request->{PARAMETERS} ; '/script_file' eq $path && App::Asciio::Scripting::run_external_script($asciio, $parameters->{script} // '') ; -'/script' eq $path && App::Asciio::Scripting::run_external_script_text($asciio, $parameters->{script} // '') ; +'/script' eq $path && App::Asciio::Scripting::run_external_script_text($asciio, $parameters->{script} // '', $parameters->{show_script}) ; return 1 ; } diff --git a/script/stdin_to_asciio_web b/script/stdin_to_asciio_web index f453eec5..2f9111be 100755 --- a/script/stdin_to_asciio_web +++ b/script/stdin_to_asciio_web @@ -1,2 +1,2 @@ -xh --ignore-stdin -f POST http://localhost:4444/script script="$(cat)" +xh --ignore-stdin -f POST http://localhost:4444/script script="$(cat)" "$@"