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

document how to use OSC 7 on Windows #146

Closed
God-damnit-all opened this issue Feb 8, 2020 · 13 comments
Closed

document how to use OSC 7 on Windows #146

God-damnit-all opened this issue Feb 8, 2020 · 13 comments
Labels
bug Something isn't working

Comments

@God-damnit-all
Copy link

God-damnit-all commented Feb 8, 2020

As described in #114, I should be able to open wezterm, navigate up a directory cd .., and then press the plus button to open a new tab, which should open a new cmd in C:\Users, but instead I get my default C:\Users\{Username} directory.

This might not be limited to Windows.

@God-damnit-all God-damnit-all added the bug Something isn't working label Feb 8, 2020
@wez
Copy link
Member

wez commented Feb 8, 2020

That feature requires that the shell (or some other application) emit an escape sequence each time it changes the current directory. That is in the default for bash and zsh on the Fedora system that jsgf used when building that feature, and I have a slightly different version of that in my personal shell setup.

While the escape sequence should logically be emitted when the working directory changes, the typical approach is to have the shell emit it just before it prints the prompt.

The same technique should work on windows, but you'll need to translate it to your shell; I don't think that is possible with CMD, but it should be possible with powershell. For CMD, you might be wizard enough to cook up an alias or something that emits the escape sequence when you run cd.

I've attached the shell snippet from Fedora to this comment which includes more shell integration than just the current directory stuff, as well as some escaping.

The gist of the escape sequence is:

printf "\033]7;file://HOSTNAME/CURRENT/DIR\033\\"

If you come up with something that works for this for CMD and/or powershell, I've love to include it in the wezterm docs!

vte.sh.txt

@God-damnit-all
Copy link
Author

God-damnit-all commented Feb 8, 2020

Why not just pull the current directory from the cmd.exe process's handles? The right one looks like this:
image
It will always be a File handle with a Granted Access of 0x100020, its File information is Directory in Mode 0x20, and Object address is null.

@wez
Copy link
Member

wez commented Feb 8, 2020

re: why not look at handles?

cmd.exe may be running eg: msys bash. How do you know which is the right process to snoop? How do you know which is the right handle to look at? What if you are actually sshing into a remote windows system where you don't have access to inspect the process?

The spec for this feature: https://gitlab.freedesktop.org/terminal-wg/specifications/merge_requests/7/diffs touches briefly on this.

@God-damnit-all
Copy link
Author

The right one would be a child process of wezterm.exe, yes?

@wez
Copy link
Member

wez commented Feb 8, 2020

it's really not that simple: as I said above, the direct child may be running an arbitrary tree of nested children: a cmd may run a cmd which may run a cmd which may run a bash shell. There is no "right" way to pick one, and even if there was, that isn't portable and doesn't fit how ptys and ttys work; there may not even be a child process: we may be running a serial connection to a linux machine for instance, or a TLS connection to a remote multiplexer.

The feature that is implemented works based on an escape sequence because that is the most reasonable way to implement this specification; that's why it is specified in the way that it is.

@God-damnit-all
Copy link
Author

That's a shame, I don't think there's any way of throwing the escape sequence in a way that wouldn't cause a new line, unless there's some magic way of doing it with doskey.

@wez
Copy link
Member

wez commented Feb 9, 2020

I took a peek at some information for CMD and in theory using this feature should be as simple as:

prompt $E]7;file://localhost/$P$E\$P$G

but, because of the way that the windows PTY stuff works, the conhost layer in the background currently swallows and doesn't re-emit the sequence to the terminal emulator.

This issue is tracking support for this feature in the Windows PTY layer: microsoft/terminal#3158

@wez wez changed the title 'Use current tab directory as default for new tabs' not working with Windows cmd document how to use OSC 7 on Windows Feb 9, 2020
wez added a commit that referenced this issue Feb 9, 2020
This allows this prompt setting to work:

```
prompt $E]7;file://localhost/$P$E\$P$G
```

although this one sets it for future prompts:

```
setx prompt $E]7;file://localhost/$P$E\$P$G
```

refs: #146
@wez
Copy link
Member

wez commented Feb 9, 2020

Turns out that I overlooked something; the pty was actually passing through the sequence, but we weren't passing the cwd through to the newly spawned processes.

I've pushed a commit that enables this feature for windows; you just need to run:

setx prompt $E]7;file://localhost/$P$E\$P$G

to set it up in the default environment for you when you next login, so that it applies to all of your cmd windows by default.

@God-damnit-all
Copy link
Author

In the interest of portable compatibility, is there any way I can have wezterm auto-execute this for cmd windows?

wez added a commit that referenced this issue Feb 9, 2020
This is useful for setting up a reasonable initial environment.
For example, on Windows you might want to set the `prompt` environment
so that some basic shell integration is enabled; this will cause new
tabs to open with the same cwd as the current tab:

```
set_environment_variables = { "prompt"="$E]7;file://localhost/$P$E\\$P$G" }
```

This setting is intended to apply only to the local domain.

refs: #146
@wez
Copy link
Member

wez commented Feb 9, 2020

With the commit that I just pushed, put this in your wezterm.toml:

set_environment_variables = { "prompt"="$E]7;file://localhost/$P$E\\$P$G" }

@wez
Copy link
Member

wez commented Feb 9, 2020

You can get a little more fancy; this shows an alternative way to express setting this environment, and also how to embed some SGR sequences to change the color and have the prompt span multiple lines:

[set_environment_variables]
prompt = "$E]7;file://localhost/$P$E\\$E[32m$T$E[0m $E[35m$P$E[36m$_$G$E[0m "

wezterm-prompt-windows

@God-damnit-all

This comment has been minimized.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2023

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants