Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to get the current path of pty #4

Closed
supersuraccoon opened this issue Feb 1, 2019 · 3 comments
Closed

[Question] How to get the current path of pty #4

supersuraccoon opened this issue Feb 1, 2019 · 3 comments

Comments

@supersuraccoon
Copy link

Thanks for your great work now I am trying to learn and build something cool based on your project.

And now I am facing a problem that I can not solve myself after doing a lot researches...

Let's say the starting path is /User/Example/App

and then the command input sequence from web side is :

cd ..
cd ..

And now the current path should be at /User but how can I get this path using code ?

Any advice will be appreciated, thanks :)

@cs01
Copy link
Owner

cs01 commented Feb 1, 2019

When you say “get this path” what do you mean?

I suppose you could run ‘pwd’ In the terminal.

@supersuraccoon
Copy link
Author

thanks for your quick reply

Let me put my question in a better context:

When I type ls I can get a list of file in the web terminal and now I want to open image files when user click it in the web terminal.

I have achieved this using xterm.js's registerLinkMatcher function like this:

term.registerLinkMatcher(/\w+.png/, function (mouseEvent, imageFile) {
  	console.log(imageFile);
  	socketPfile.emit("pfile-input", imageFile);
});

When user click say cat.png the registerLinkMatcher will triggered and then I will send a command to python.

But the problem is the value of imageFile here is only the name of the file cat.png (not path) so on the python side with the code:

@socketio.on("pfile-input", namespace="/pfile")
def pimage_input(data):
    result = os.popen(data["input"])
    with open(data, 'rb') as f:
        image_data = f.read()
        socketio.emit("pfile-output", {"output": image_data}, namespace="/pfile")

I need the full path of the input data cat.png (the user on the web terminal side might have typed cd xxx , cd xxx to change directory several times) which means I need to find a way to know the current path of the pty (I don't know how to say it in a proper way ....) .

@cs01
Copy link
Owner

cs01 commented Feb 1, 2019

Ah I see. I don't think there is a way to do that as you described because bash is running as a subprocess, and only it knows which directory it's in (the pty doesn't even know what bash is doing or which directory the user navigated to, it's just running bash as a subprocess and waiting for it to exit. It could be running any other subprocess like zsh, gdb, python, cat, less, etc).

I would suggest registering a link matcher on the full path rather than the filename. That way when the link matcher is triggered it's always to a full path and you can open it on the server in a more straightforward way.

@cs01 cs01 closed this as completed Feb 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants