tynanbe / shellout

🐢 A Gleam library for cross-platform shell operations
Apache License 2.0
44 stars 1 forks source link

`stdin` support #4

Open PgBiel opened 7 months ago

PgBiel commented 7 months ago

Hello, Thanks for the great library! I'm new to Gleam, and this seems very useful to program abstractions around shell commands and such.

I'd like to ask, are there any plans for tools to work with stdin? In particular:

  1. Being able to provide a certain string (or bytes) as the full standard input to a program;
  2. Being able to redirect all stdin received by the Gleam program to the input of the program being run (something akin to LetBeStdin);
  3. Being able to pipe a program's stdout to another program's stdin in an efficient manner (could be platform-restricted if needed). This could require a deeper rework (to e.g. use some sort of Process type), but I still think that could be very handy when working with e.g. lots of data in the shell.

Thanks in advance!

tynanbe commented 6 months ago

Thanks, @PgBiel, those are some great ideas.

For my thoughts about the future of this library, check https://github.com/tynanbe/shellout/issues/5#issuecomment-1969342494

As for stdin, shellout should already be as transparent as possible about it, meaning it should be possible to pipe stdin to a command already in many cases. In other words, LetBeStdin shouldn't be necessary, but Erlang itself has some limitations with stdin, as I recall. Potentially in the future, I'll provide a NIF (possibly with a number of precompiled binaries available) I've been toying with that has solved my previous frustrations with Erlang.

I'll definitely consider your other suggestions when I start working on shellout v2.0.

For the time being, I think your first and third ideas might be currently doable as, e.g.

shellout.command(run: "sh", with: ["-euc", "
  echo '" <> my_string <> "' \\
    | cat
"], in: ".", opt: [LetBeStderr, LetBeStdout])