I've just implemented this in my embedding of Terminator in Eclipse.
This uses a small patch to the PtyProcess code to expose the process
id. I then use this in Java to find out the cwd from /proc/$pid/cwd. I
don't know a cross-platform way do this so this will only work on Linux
and whatever UNIX platforms support /proc.
Is this a feature you would be interested in for Terminator proper?
Even without cross-platform support? If so then I'd be happy to discuss
how best to implement it and come up with a patch. If not, please
consider applying the attached patch to just expose the pid. Please be
brutal about my C++/JNI (not that there's much of it), it's been a fair
while since I've written any C/C++ and this is my first JNI code.
Thanks,
Matt.
not using tabs, i have no opinion. maybe others do.
> If not, please consider
> applying the attached patch to just expose the pid. Please be brutal
> about my C++/JNI (not that there's much of it), it's been a fair while
> since I've written any C/C++ and this is my first JNI code.
you don't need this patch. in your calling code just replace "getPid" with
"getProcessId" --- we already offer what you want, just not with the name
you expected.
i wouldn't be against changing getProcessId to getPid, depending on what
others think. (we're seriously considering moving PtyProcess to
salma-hayek and moving away from Sun's Process everywhere.)
--
Elliott Hughes, http://www.jessies.org/~enh/
As Martin notes it applies to new windows too.
>> If not, please consider
>> applying the attached patch to just expose the pid. Please be brutal
>> about my C++/JNI (not that there's much of it), it's been a fair while
>> since I've written any C/C++ and this is my first JNI code.
>>
>
> you don't need this patch. in your calling code just replace "getPid" with
> "getProcessId" --- we already offer what you want, just not with the name
> you expected.
>
Oops! I think getPid is more natural, but I'm embarrassed to have
missed getProcessId.
Matt.
I can see it might make sense do push it into JNI if it was to be a
terminator feature and present a uniform interface, e.g.
PtyProcess.getCwd, across all platforms, even if it had to be described
as 'best effort' and always failed for some platforms.
My googling suggests lsof -p $pid will give the cwd on OSX. I can't
confirm. Perhaps we can find out how it does it. I'll investigate
further later in the week.
Matt.
in GNOME Terminal, but we don't have to copy that. if we do, i'd
definitely want a preference to turn it off. why would i be opening a new
window if i wasn't about to do something new?
(i can perhaps see the use of a separate "clone" action, but i fear the
only time i'd want it would be when i'm actually SSHed into another
machine, and it wouldn't actually help me there.)
in fact, i could almost do with the opposite functionality, that noticed
when i cd into a directory another shell's already in and pops the other
one to the front, reminding me that i've lost it under my other windows
;-)
not wanting to distract my main "i don't use two shells in the same
directory at the same time" point before, i didn't mention that there is a
very rare occasion when i do, and that's when i've messed about with my
environment and i want to compare against a clean environment. duplicating
the environment would drive me mad.
back to the idea of duplicating the cwd, another thing i don't like about
the idea is that it ruins the muscle memory you have from always starting
in the same directory. ruins the history's ability to take you to your
usual haunts, too, unless you only ever use absolute paths (another thing
the idea of a home directory was meant to save us the hassle of).
A little script to diff the /proc entries (in some useable format probably not involving nulls) between two processes seems likely to be of more general use than cloning the environment to a new shell.
Perhaps we should export the pid of the cloned terminal's "shell", then a small script could use that to cd or slurp the environment as it chooses.
>
> On Apr 5, 12:56 am, "Elliott Hughes" <e...@jessies.org> wrote:
>> On Sat, April 4, 2009 15:42, Chris Reece wrote:
>>>> I still don't have any objection. Others may now have.
>>
>>> Unless I've overlooked some part of the conversation, this is only
>>> going to work for local processes. I spend most of my time
>>> operating
>>> remotely.
>
> There's still a "current directory" on the local system (from before
> launching ssh).
Yes, I discounted that because it's not useful to me. Is it useful
to you?
>>> My most commonly used "New" function is "New Command Tab",
>>> followed by typing a hostname, Tab, Return. Matt's use case
>>> matches my
>>> own needs, except that the environment I want to duplicate will
>>> be a shell
>>> with the same cwd, on the same remote machine.
>
> Even if terminator found out the machine and the remote directory, it
> may
> not be able to drive ssh to duplicate it e.g. if it went through a
> third machine
> to get there.
Yes, that's what I was thinking when I drifted from title-scraping to
using the tab or window's initiating command. If the hostnames
matched, the directory gained from scraping the title would be
useful. If they didn't, then simply repeating the tab's initial
command would be useful to me, because it'd get me one hop closer.
But as Elliott pointed out, his usage (start a shell, then ssh) is
different from mine, making the window title his only source of
information.
> I think the behavior should be the same regardless of whether one uses
> tabs or separate windows (unless by "no tabs" you mean one single
> shell window).
This would certainly be less confusing.
Anyone else share this muscle memory? Personally, if I'm thinking
"from ${HOME}, go FOO", I type "cd" first, without stopping to think
whether I'm in my home directory already.
First a little background I omitted before: my Eclipse embedding of
terminator doesn't just default the cwd for new tabs based on the
currently selected tab. If another element in the Eclipse UI is focused
(for example an editor) and a terminal is spawned, it uses the directory
associated with the UI element (so the directory containing the file
being edited). The consistency of using the cwd when a terminal is
focused makes sense to me. Plus, as I said before, I'm addicted to it
from gnome-terminal.
I don't really have the muscle memory issue. I do find myself typing
cd, or cd ~/... often but that's cheap, whereas it's often awkward to
get back to a path deep within a Java source tree (an average path from
/ to a leaf package has ~9 segments for me).
I rarely intentionally use multiple windows. Others have very different
work styles and I can see cwd preserving could be irritating if new
shell almost always means new task. Perhaps this is a case for a
preference, though as I say don't see the cost of cd or cd ~/... as
high, but that's my habit and the change might be very annoying to
someone used to always starting in their home directory.
I don't really use ssh often enough to comment on cloning remote
sessions. I just connect to one machine and use screen(1) all the while.
Exposing the pid of the spawning tab's command in the new tab's
environment for scripting is a neat idea but pretty much hides the
feature from the average user.
More generally, I raised this because I find this feature essential and,
because I'd got some way to implementing it myself, it seemed
straightforward enough to add to terminator. However, I don't really
have much invested in any decision because I don't actually use
terminator day-to-day other than embedded in Eclipse, which handles
creating a new tab differently and now behaves as I prefer. I might
benefit in terms of cross platform support if getCwd ended up on
PtyProcess though I'm only worrying about Linux for now.
It's not clear to me that there's a consensus.
Matt.
>> I'd use the popup menu anyway for the with-cwd case
> having it on the menu wouldn't get in anyone's way.
That sounds like a good starter then.
> remind me where we can't do this?
We don't have code to do it on Mac or, I guess, Solaris. The Linux method will work on Cygwin, if we inspect /proc from Cygwin.
-----Original Message-----
From: terminat...@googlegroups.com
[mailto:terminat...@googlegroups.com]
On Behalf Of Elliott Hughes
Yeah. I don't have a Mac handy. Does an OSX standard install have
lsof? On Linux
/usr/sbin/lsof -a -p $PID -d cwd -F0n
seems to run quickly and be fairly easy to parse.
Matt.
> Martin Dorey wrote:
>
>> Just Mac then.
>>
>>
>
> Yeah. I don't have a Mac handy. Does an OSX standard install have
> lsof? On Linux
>
> /usr/sbin/lsof -a -p $PID -d cwd -F0n
My 10.4 iBook does:
dahmer:/var/tmp car$ /usr/sbin/lsof -a -p 18316 -d cwd -F0n
p18316
n/private/var/tmp
However, in case we were hoping for a one-stop shop, Solaris doesn't.
Available from Blastwave or OpenCSW.