libvips / nip2

A spreadsheet-like GUI for libvips.
https://libvips.github.io/libvips/
GNU General Public License v2.0
362 stars 13 forks source link

Allow reading script from stdin #53

Open denilsonsa opened 9 years ago

denilsonsa commented 9 years ago

My usage:

generate_nip2_script | nip2 -s - -o output.png

But it does not work because nip2 -s requires a filename.

As workaround, I can use nip2 -s /dev/stdin

jcupitt commented 9 years ago

Hi again, try nip --help. The flags you need are maybe:

  -w, --stdin-ws                  load stdin as a workspace
  -d, --stdin-def                 load stdin as a set of definitions

I guess -d. For -w you need to make the XML for a nip2 save file, which is annoying and fiddly.

denilsonsa commented 9 years ago

Thank you for the answer! I did some experiments.

-s, --script=FILE               load FILE as a set of definitions
-o, --output=FILE               write value of 'main' to FILE
-b, --batch                     run in batch mode
-w, --stdin-ws                  load stdin as a workspace
-d, --stdin-def                 load stdin as a set of definitions

These two behave the same (they do not fire up the GUI):

generate_nip2_script | nip2 -d -b -o output.png
generate_nip2_script | nip2 -s /dev/stdin -o output.png

However, passing just -d will fire up the GUI, and will write the output file only after the GUI is closed:

generate_nip2_script | nip2 -d -o output.png

Maybe this is a bit non-intuitive, or maybe -s should not disable the GUI automatically (requiring -b, as does -d). This change may break scripts currently running in servers.

Maybe the help text should mention which arguments will disable the GUI as well.

denilsonsa commented 9 years ago

When I was writing the nip2 script, I was wondering why the hell I couldn't use nip2 built-in toolkit definitions in my script (i.e. all those in Toolkits → Edit). But now I understand! Using -s implicitly enables a bunch of other options, which was very unexpected for me.

$ echo 'main = Image_join_item.join_lr;' | nip2 -d -b -p

$ echo 'main = Image_join_item.join_lr;' | nip2 -s /dev/stdin
error calculating "root.main"
Not found.
Symbol Image_join_item is not defined.
Image_join_item is referred to by: main (/dev/stdin:1) 

The easiest "fix" that does not break any currently deployed script is changing the help text:

-s, --script=FILE               load FILE as a set of definition (implies -b -m -a -p)
⋮
(likewise for the other options)

Alternatively, it is possible to change the behavior; but it may cause trouble for some people.

jcupitt commented 9 years ago

There's a chapter in the docs on the command-line switches, did you find it?

http://www.vips.ecs.soton.ac.uk/supported/7.42/doc/html/nipguide/nipguidese13.html#x21-390004.5

The idea is that -s and -e are the main two batch mode settings, with -s being the one for using nip2 as a script interpreter, and -e being the one for immediate calculations. Neither of them actually does anything in themselves, they are just shorthand for a useful combination of arguments.

You can mix up your own batch mode settings by combining the (many) other switches.

jcupitt commented 9 years ago

Ooop, forgot, the other idea is that nip2 definitions are in two layers.

The menu definitions are not useful for writing batch scripts (they assume there's a workspace visible), so they are not loaded in batch mode. The menu defs are not supposed to have any actual functionality in them, they just call into the layer below.

The things in _stdenv.def put a nicer wrapper around the basic libvips operations. This is where the brains live, and these defs are always loaded.

So ... in your batch script, don't use Image_join_item, that's got UI in, instead use join_lr and join_tb. Though now I look at Image_join_item I see it annoyingly defines its own version of join_lr with quite a few more options.

I've not really touched nip2 in five years, I've been focused on vips8. Now that's done, I ought to go back to nip2 and update it as well. It needs a new gtk3 GUI, and it needs this sort of issue sorting out. A new parser would be nice too. And the programming language should be broken out into a separate library and run in its own thread. Hey ho.