Skip to content

Commit

Permalink
CHANGED: scripting API
Browse files Browse the repository at this point in the history
  • Loading branch information
nkh committed Nov 28, 2023
1 parent a791ed5 commit 3ee6020
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 6 deletions.
1 change: 1 addition & 0 deletions Build.PL
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
27 changes: 27 additions & 0 deletions documentation/mdbook_asciio/src/for_developers/scripting_api.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Simplified Scripting API

## create_undo_snapshot

Creates an undo snapshot.

```
create_undo_snapshot ;
```

## add

Adds a named element:
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/App/Asciio/Actions/Ruler.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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} },
)
) ;
Expand Down
2 changes: 1 addition & 1 deletion lib/App/Asciio/Elements.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
{
Expand Down
36 changes: 34 additions & 2 deletions lib/App/Asciio/Scripting.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require Exporter ;
@ISA = qw(Exporter) ;
@EXPORT = qw(
update_display
create_undo_snapshot
add
add_type
Expand All @@ -25,6 +26,9 @@ require Exporter ;
connect_elements
optimize
delete_all_ruler_lines
add_ruler_line
save_to
to_ascii
ascii_out
Expand Down Expand Up @@ -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 @_ ; }

Expand All @@ -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 ;

Expand Down Expand Up @@ -133,6 +140,11 @@ new_script_asciio() ;

#--------------------------------------------------------------------------------------------

sub create_undo_snapshot
{
$script_asciio->create_undo_snapshot() ;
}

sub add
{
my ($name, $element, $x, $y) = @_ ;
Expand Down Expand Up @@ -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() ; }
Expand Down
2 changes: 1 addition & 1 deletion lib/App/Asciio/Server.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
}
Expand Down
2 changes: 1 addition & 1 deletion script/stdin_to_asciio_web
Original file line number Diff line number Diff line change
@@ -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)" "$@"

0 comments on commit 3ee6020

Please sign in to comment.