-
Notifications
You must be signed in to change notification settings - Fork 34
ct bash: make ct follow pwd of bash? #94
Comments
I think I understand what you mean. Terminal on macOS does the same thing; by default, if you open a new tab, it'll use the same Interestingly, the In addition, I'm using |
If you use
The reason the directory isn't being tracked on After some digging and with the help of this answer, the spawned shell is |
In term of environment propagation, this seems to be working properly, meaning that execvp seems to spawn the process (i.e. bash) in the right directory. However I'm working under Linux so maybe the behavior is different under macOS? I mean in my case there doesn't seem to be any issue with PATH. I'll attempt to illustrate a bit the issue that I'm facing, just to be clearer: So this works:
Then going further, this doesn't work:
It's not a bug, but for this kind of case, it would be convenient to make "ct" periodically perform a chdir or something to update its own cwd with the cwd of its process (bash here). Don't know if that makes more sense? Regarding --login maybe there is something to do as well, I'm not really familiar with this. Thank you, |
The path is updated because of an ANSI escape sequence that the terminal emulator processes. It's basically:
After every command, the shell will run whatever function in With your current setup, what do you get when running Also, what's the OS you're running? To save you from all of this back-and-forth, I'll set up a virtual machine to test. |
I installed Fedora KDE and currently testing. The |
On my side I have this (Centos 8), with and without --login (I double-checked that one case was in login and the other in non-login):
Just for the sake of testing I did this to simulate ct synchronizing its chdir to the one of its child process: With move_chdir.sh (performing a chdir from gdb):
I ran this on the pid of "ct" to another path, and then Konsole properly duplicated the tab to this new path, so that confirmed the theory that it seems to read the cwd of the command it runs and not the prompt (not sure if this is related to --login or not then). |
Small screenshots to make sure we are aligned, in case this helps: At startup, things are similar, Konsole detects the cwd of ct. Here Konsole didn't detect the change of pwd (probably because it reads ct's cwd that didn't move, since it doesn't follow bash's one and since bash cannot update its parent cwd). Simulating the change of ct's cwd to follow bash's one: now it's fine, and duplicating the tab will run properly on ~/scripts. Thank you! |
Thanks for the detailed response. Yea, Konsole is indeed reading the Polling the |
I guess that the only way is to do exactly what Konsole does in the source that you linked, i.e. reading the symlink of /proc/pid/cwd? Since you forked, from the parent you probably have access to the pid of the child to check this? I guess that would have to be done periodically to avoid to harm performances, if this is something you are willing to support! |
I'm not strictly against it, though, as with Konsole, that solution is platform dependent; macOS, for instance, doesn't even have I'm currently experimenting with having the slave part of the fork be ChromaTerm. Seems to work so far, though still gotta experiment a bit more. |
Do you mean that you wish to exec in the parent and not in the child part of the fork? I guess this could work very well, however you might end up with "ct" becoming a zombie due to the parent not doing waitpid() on the child? |
Yea, that's pretty much what I ran into. I'm slowly opening up to the idea of having a thread that does what you described: periodically update |
I wonder if there might be some ways to deal with this by forking twice and closing so the parent of ct becomes 1, removing the need to waitpid() and preventing zombies, not sure if that could work, especially since you are probably using pipes. Regarding EDIT: still looking at Konsole, for macOS they seem to be using proc_pidinfo for MacProcessInfo, that might work and be better than using lsof. Not sure how this can be achieved in Python, maybe with ctypes. |
I remembered the I'm already using diff --git a/chromaterm/__main__.py b/chromaterm/__main__.py
index 872dce6..d25b262 100644
--- a/chromaterm/__main__.py
+++ b/chromaterm/__main__.py
@@ -23,6 +23,9 @@ CONFIG_LOCATIONS = [
'/etc/chromaterm/chromaterm',
]
+# The frequency to check the child process' `cwd` and update our own
+CWD_UPDATE_INTERVAL = 1
+
# ChromaTerm cannot determine if it's processing data faster than input rate or
# if the input has finished. Therefore, ChromaTerm waits before processing the
# last chunk in the buffer. The waiting is stopped if data becomes ready.
@@ -361,6 +364,7 @@ def run_program(program_args):
import fcntl
import pty
import termios
+ import threading
import tty
try:
@@ -398,6 +402,27 @@ def run_program(program_args):
signal.signal(signal.SIGWINCH, window_resize_handler)
window_resize_handler()
+ # Some terminals update their titles based on `cwd` (see #94)
+ def update_cwd():
+ import psutil
+ import time
+
+ try:
+ child_process = psutil.Process(pid)
+ except (psutil.AccessDenied, psutil.NoSuchProcess):
+ return
+
+ while True:
+ time.sleep(CWD_UPDATE_INTERVAL)
+
+ try:
+ os.chdir(child_process.cwd())
+ except OSError:
+ pass
+
+ # Daemonized to exit immediately with ChromaTerm
+ threading.Thread(target=update_cwd, daemon=True).start()
+
return master_fd |
Just tested quickly, it seems to be working! :) Indeed regarding psutil, that's way better! |
v0.7.5 released. Thanks a lot for your help on this one. |
Perfect, thank you for your time and valuable work! |
Hello,
First of all, thanks for this tool, this is really great and very easy to customize!
Just a small enhancement request in case you find this useful: certain terms such as Konsole from KDE seem to use the current pwd of the running app (bash/etc) to spawn another tab on the same pwd. I found it useful to do "ct bash" instead of bash to have colors but since ct doesn't follow bash's pwd, Konsole doesn't see the right pwd.
One way to fix that would be to periodically perform a chdir on the current pwd of the running process? To get it, one way could be to check /proc/child pid/cwd, but there might be better ways.
Don't know if this makes much sense, I can attempt to be more precise, but that would be helpful!
Thanks,
Louis
The text was updated successfully, but these errors were encountered: