Giter Site home page Giter Site logo

Comments (15)

kovidgoyal avatar kovidgoyal commented on June 15, 2024

kitten @ launch --type=background sh -c "stdout_and_stderr=$(some_script 2>&1)

from kitty.

amosbird avatar amosbird commented on June 15, 2024

kitten @ launch --type=background sh -c "stdout_and_stderr=$(some_script 2>&1)

It seems I didn't describe the FR clearly. I'd like to have the stdout/stderr redirects to the tty which executes the kitten command. For instance:

Current behavior:

$ kitten @ launch --type=background echo hello world
0   <---  currently it prints the exit status zero

Expected behavior:

$ kitten @ launch --type=background echo hello world
hello world   <---  I'd like to get the stdout here

from kitty.

kovidgoyal avatar kovidgoyal commented on June 15, 2024

Run

echo hello world &

from kitty.

amosbird avatar amosbird commented on June 15, 2024

echo hello world &

Sorry I don't quite follow. How does this help?

from kitty.

kovidgoyal avatar kovidgoyal commented on June 15, 2024

from kitty.

amosbird avatar amosbird commented on June 15, 2024

The echo hello world is just a demo script used to describe the FR. Real scripts will be only available on host machine and the kitten command will run on remote machine.

from kitty.

kovidgoyal avatar kovidgoyal commented on June 15, 2024

If you had said so in the first place, it would have saved some time.

Run:

kitten @ launch --type=background sh -c "exec whatever > /tmp/output 2> /tmp/output" && kitten transfer --direction=upload /tmp/output temp-output-file && cat temp-output-file && rm temp-output-file

You will get a permission prompt from the transfer kitten, read its docs
to see how to avoid that.

from kitty.

amosbird avatar amosbird commented on June 15, 2024

Sorry for wasting your time. I'll do more preparation before submit FR next time :)

While the provided solution addresses certain scenarios, it falls short in cases where synchronization is necessary. I understand that additional "hacks," such as waiting for temporary result files to appear in the remote shell, can be implemented. However, wouldn't it be more straightforward to extend rc.Launch.response_from_kitty with is_asynchronous = True and utilize subprocess.Popen.communicate() to wait for the output? I did some tests, and it seems working.

from kitty.

kovidgoyal avatar kovidgoyal commented on June 15, 2024

You are most welcome to send a PR to add that functionality, but note
that to be acceptable it has to:

  1. Allow sending stdin from the kitten invocation as stdin to the
    background process. See @ send-text for an example of how to implement
    this. You can possibly make it simpler to implement by only doing it
    when stdin is not a tty.

  2. Return the stdout, stderr and exit status in the response which the
    kitten should then forward to its stdout, stderr and exit status.

from kitty.

kovidgoyal avatar kovidgoyal commented on June 15, 2024

Also note that you cannot use subprocess.communicate() as that will
block the main thread of kitty. Instead you need to run the background
process redirecting the output to unnamed temp files and in your
response read from them. There is also the question of what to do when
there is a lot of output, I dont recall if the kitten is able to read
arbitrary sized responses from kitty, you will need to test.

from kitty.

kovidgoyal avatar kovidgoyal commented on June 15, 2024

Also, in the --no-response case it should work as it does currently,
with no response.

from kitty.

kovidgoyal avatar kovidgoyal commented on June 15, 2024

Oh and just for completeness, your synchronized usecase can be
implemented like this:

kitten @ launch --no-response --type=background --allow-remote-control sh -c "echo -e 'hello\nworld\nEOF' | kitten @ send-text --stdin --match id:$KITTY_WINDOW_ID"; stty_orig=`stty -g`; stty raw -echo; python -c "import sys; [sys.exit(0) if line.rstrip() == 'EOF' else print(line, end='\r') for line in sys.stdin]"; stty "$stty_orig"

In production you should of course use something more unique than EOF to
signify end of input.

from kitty.

kovidgoyal avatar kovidgoyal commented on June 15, 2024

And because I'm on a roll, here's a version without EOF and stty

kitten @ launch --no-response --type=background --allow-remote-control sh -c "echo -e 'hello\nworld\nagain' | kitten @ send-text --stdin --match id:$KITTY_WINDOW_ID; kitten @ signal-child --match id:$KITTY_WINDOW_ID"; python -c "import sys, signal, os, tty, termios; attr=termios.tcgetattr(1); tty.setraw(1); signal.signal(signal.SIGINT, lambda *a: (termios.tcsetattr(1, termios.TCSANOW, attr), os._exit(0))); [print(line, end='\r') for line in sys.stdin]";

This should be completely robust and synchronous. It will even stream
data from your command to STDOUT, not reading everything in a batch.
Which your proposed changes to --type=background wont do.

You can make it not line buffered by replacing the python one-liner with a
proper script that reads from stdin in non-blocking mode and writes to
stdout, replacing all newlines by \n\r.

from kitty.

amosbird avatar amosbird commented on June 15, 2024

kitten @ launch --no-response --type=background --allow-remote-control sh -c "echo -e 'hello\nworld\nagain' | kitten @ send-text --stdin --match id:$KITTY_WINDOW_ID; kitten @ signal-child --match id:$KITTY_WINDOW_ID"; python -c "import sys, signal, os, tty, termios; attr=termios.tcgetattr(1); tty.setraw(1); signal.signal(signal.SIGINT, lambda *a: (termios.tcsetattr(1, termios.TCSANOW, attr), os._exit(0))); [print(line, end='\r') for line in sys.stdin]";

Interesting. But this doesn't work when kitten is running inside an ssh session. Signaling ssh will directly kill the connection instead of passing it to the underlying foreground process.

from kitty.

kovidgoyal avatar kovidgoyal commented on June 15, 2024

Then use a terminator like EOF.

from kitty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.