-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Set ALACRITTY_TERM_TITLE when executing hint commands.
#8721
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
Conversation
This allows the command run to be passed context about which terminal
the hint has been run from.
For example in my shell, I set the title of the terminal to:
```
[<user>@<host> <running command> <cwd>]
```
with:
```
chpwd() {
[[ -t 1 ]] || return
print -Pn "\e]2;[%n@%m %~]\a"
}
```
so whenever I change directory, the terminal title is changed.
When I click on a hint, that title is sent to the hint command. I then
extract the cwd to find out what directory that terminal was in
(replacing `~` with `$HOME` because `%~` in the shell above pretty
prints `$HOME` as `~`:
```
term_dir=$(echo "$ALACRITTY_TERM_TITLE" | sed -E "s/.* (.*)\]/\\1/g" | sed -E "s|^~|${HOME}|g")
```
With that knowledge, I can then do things like drive different neovims
based on `$term_dir/.nvim_server`. Specifically, I have a hint which
matches `file:line:col`, so when I click on an error in a backtrace,
the right neovim switches to `file` (at `line:col`), even if I have
multiple neovims running in different directories across my machine.
The script I execute (eliding boring details) looks roughly as follows:
```
term_dir=$(echo "$ALACRITTY_TERM_TITLE" | sed -E "s/.* (.*)\]/\\1/g" | sed -E "s|^~|${HOME}|g")
if [ -e "$term_dir/.nvim_server" ]; then
cd $term_dir
path=$(echo "$1" | cut -d ":" -f 1)
line=$(echo "$1" | cut -d ":" -f 2)
col=$(echo "$1" | cut -d ":" -f 3)
nvim --server .nvim_server --remote-tab "${path}"
nvim --server .nvim_server --remote-send ":call cursor(${line},${col})<CR>"
fi
```
|
@chrisduerr Is there an alternative that might be acceptable? This does solve a real problem, as I hope I proved above: there is no problem with the fact that alacritty is in daemon mode. It really does work! |
|
This is a poor solution even to your problem. |
|
@chrisduerr I welcome your thoughts as to a better solution! |
|
I think the idea of setting some environment variables to identify the run is reasonable. I don't think putting the alacritty title in the env is though. I'd say the window ID would be good, but I'm not sure there's a good cross platform value for that... |
|
@nixpulvis For me, the window ID would be more than sufficient. It would obviously have some platform-specific aspect, though I don't know if every platform has a concept of "window ID"; still, I guess such platforms would probably have another notion that could be used? As a possible alternative, could one invent -- I don't think the existing ones are sufficient -- a new OSC sequence that would be something like "set terminal ID" where the shell could tell the terminal how it wants to be identified? In my case, I would set that ID to the current directory every time it's changed. |
If a simple numeric ID is sufficient for your needs, there's an existing sequence for that. The Tertiary Device Attributes report ( On the original hardware terminals the ID wasn't generally intended to be changed, so the value would typically have been locked. But nobody relies on it being a unique identifier anymore, so some modern terminals do allow it be set with |
It's already set, see |
|
@j4james An 8 digit hex value would be fine, and I'd be fine generating random IDs, setting them, and leaving a cookie per-ID in a directory of my choosing! I admit that my DA skills are a bit lacking, so I might not have got quite the right shell code to query + print out the ID. It looks right now that all Alacritty windows from the same process might return the same |
|
@kchibisov When I do |
|
Yeah, it's a variable used for our Though, I believe that you can not use this ID outside of Though, adding |
|
Based on @kchibisov's suggestion I have force pushed a commit to my branch (ltratt@097ffd8; I suppose it doesn't show up here while the PR is set as closed) which sets |
Yeah, but won't work on |
|
@kchibisov Personally, I will probably move to a "cookie" approach where I have If ltratt@097ffd8 is acceptable to folk, how would you like to proceed. Reopen this PR or a new PR or ... ? |
|
I was saying that there's nothing similar to |
While this is accurate, the motivation behind this patch is certainly much less sensible and reliable. This is not a good mechanism to communicate hint data to neovim. |
|
@chrisduerr Reasonable people can differ on such matters IMHO! Still, I hope that you can imagine use cases which you would find sensible and reliable based on my patch? If so, would reopening this PR or making a new one be most acceptable to the Alacritty maintainers? |
|
Well this patch in its current form certainly won't be merged. |
|
@ltratt Sorry, I didn't mean to suggest that you could already use |
|
@chrisduerr Are you referring to ltratt@097ffd8 ? If that is unacceptable, can I ask you to explain what would be acceptable to you, bearing in mind that I'm an idiot and need things spelt out? In essence, all ltratt@097ffd8 does is set |
I was referring to the current state of the PR, but this patch isn't much better. It sets I get that this is a hacky workaround to fix your issue, but there isn't any system to it as far as I can tell. It's just a quick hack to get your script working, which is fine for a personal fork, but not fit for upstream. |
|
@chrisduerr Would you prefer the commit to set I think, perhaps, I might not have explained my setup in a way that makes sense to you -- mea culpa! Let me try again. I'm running (various) neovim-qts. I run commands (in various ways) in various terminals e.g. various compilers. With the patch I've sent, I can click on filename:line:no from a compiler and have that open in the right neovim-qt -- provided I can tell which shell the hint command relates to. This seems rather principled to me. How that relation happens is another matter: basically there just needs to be a way for the shell and the hint command to be related to each other. If e.g. the hint command could tell me the PID of the shell in the window clicked on, that would also be perfectly acceptable. Does this make more sense? |
|
FWIW, I have outlined in more detail (a) why such a feature is useful (b) how I now implement it (using cookie files rather than |
|
I'm not reading a blog post to understand why you want a feature, but your comment did remind me that commands spawned from a process inherit the current terminal's foreground process working directory. If you just want the working directory, can't you just rely on that? |
|
@chrisduerr AFAICS |
The following works for me: |
|
@chrisduerr Alas, even |
I just checked your blog and it looks like Linux, that's what I use. Is this not the case? Do you have a working directory set in your config/cli to launch Alacritty? |
|
@chrisduerr I'm using OpenBSD. You're right that the directory I'm seeing reflects the working directory at launch (I normally launch Alacritty from rofi; if I run from |
Ah okay I guess that explains it. See the code we use to do this: alacritty/alacritty/src/daemon.rs Lines 90 to 113 in 1399545
OpenBSD falls into the Linux case here, which I'd assume doesn't work. If you have an implementation that works, it should be a simple patch to get merged. |
|
@chrisduerr You're right! Please see #8736. |
This allows the command run to be passed context about which terminal the hint has been run from.
For example in my shell, I set the title of the terminal to:
with:
so whenever I change directory, the terminal title is changed.
When I click on a hint, that title is sent to the hint command. I then extract the cwd to find out what directory that terminal was in (replacing
~with$HOMEbecause%~in the shell above pretty prints$HOMEas~:With that knowledge, I can then do things like drive different neovims based on
$term_dir/.nvim_server. Specifically, I have a hint which matchesfile:line:col, so when I click on an error in a backtrace, the right neovim switches tofile(atline:col), even if I have multiple neovims running in different directories across my machine. The script I execute (eliding boring details) looks roughly as follows: