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

VS Code Shell Integration support #1827

Closed
solid-pixel opened this issue Apr 4, 2022 · 20 comments
Closed

VS Code Shell Integration support #1827

solid-pixel opened this issue Apr 4, 2022 · 20 comments

Comments

@solid-pixel
Copy link

Hello, and thank you for this fantastic theme ❤️

Visual Studio Code recently introduced shell integration terminal.integrated.shellIntegration which perfectly supports bash, pwsh and zsh.

image
Notice the blue circle before the command line

Unfortunately, it seems to work with any zsh theme except for p10k, and I suspect the reason is similar to this although I'm no expert: #1591

What I think VS Code does, is it reads .zshrc and then creates this file on which the shell will now rely on:

/tmp/vscode-zsh/.zshrc

# ---------------------------------------------------------------------------------------------
#   Copyright (c) Microsoft Corporation. All rights reserved.
#   Licensed under the MIT License. See License.txt in the project root for license information.
# ---------------------------------------------------------------------------------------------
autoload -Uz add-zsh-hook

# Now that the init script is running, unset ZDOTDIR to ensure ~/.zlogout runs as expected as well
# as prevent problems that may occur if the user's init scripts depend on ZDOTDIR not being set.
unset ZDOTDIR

# This variable allows the shell to both detect that VS Code's shell integration is enabled as well
# as disable it by unsetting the variable.
VSCODE_SHELL_INTEGRATION=1

if [ -f ~/.zshenv ]; then
	. ~/.zshenv
fi
if [[ -o "login" &&  -f ~/.zprofile ]]; then
	. ~/.zprofile
fi
if [ -f ~/.zshrc ]; then
	. ~/.zshrc
fi

# Shell integration was disabled by the shell, exit without warning assuming either the shell has
# explicitly disabled shell integration as it's incompatible or it implements the protocol.
if [ -z "$VSCODE_SHELL_INTEGRATION" ]; then
	return
fi

__vsc_in_command_execution="1"
__vsc_last_history_id=0

__vsc_prompt_start() {
	printf "\033]633;A\007"
}

__vsc_prompt_end() {
	printf "\033]633;B\007"
}

__vsc_update_cwd() {
	printf "\033]633;P;Cwd=%s\007" "$PWD"
}

__vsc_command_output_start() {
	printf "\033]633;C\007"
}

__vsc_continuation_start() {
	printf "\033]633;F\007"
}

__vsc_continuation_end() {
	printf "\033]633;G\007"
}

__vsc_right_prompt_start() {
	printf "\033]633;H\007"
}

__vsc_right_prompt_end() {
	printf "\033]633;I\007"
}

__vsc_command_complete() {
	local __vsc_history_id=$(history | tail -n1 | awk '{print $1;}')
	if [[ "$__vsc_history_id" == "$__vsc_last_history_id" ]]; then
		printf "\033]633;D\007"
	else
		printf "\033]633;D;%s\007" "$__vsc_status"
		__vsc_last_history_id=$__vsc_history_id
	fi
	__vsc_update_cwd
}

__vsc_update_prompt() {
	__vsc_prior_prompt="$PS1"
	__vsc_in_command_execution=""
	PS1="%{$(__vsc_prompt_start)%}$PREFIX$PS1%{$(__vsc_prompt_end)%}"
	PS2="%{$(__vsc_continuation_start)%}$PS2%{$(__vsc_continuation_end)%}"
	if [ -n "$RPROMPT" ]; then
		__vsc_prior_rprompt="$RPROMPT"
		RPROMPT="%{$(__vsc_right_prompt_start)%}$RPROMPT%{$(__vsc_right_prompt_end)%}"
	fi
}

__vsc_precmd() {
	local __vsc_status="$?"
	if [ -z "${__vsc_in_command_execution-}" ]; then
		# not in command execution
		__vsc_command_output_start
	fi

	__vsc_command_complete "$__vsc_status"

	# in command execution
	if [ -n "$__vsc_in_command_execution" ]; then
		# non null
		__vsc_update_prompt
	fi
}

__vsc_preexec() {
	PS1="$__vsc_prior_prompt"
	if [ -n "$RPROMPT" ]; then
		RPROMPT="$__vsc_prior_rprompt"
	fi
	__vsc_in_command_execution="1"
	__vsc_command_output_start
}
add-zsh-hook precmd __vsc_precmd
add-zsh-hook preexec __vsc_preexec

# Show the welcome message
if [ -z "${VSCODE_SHELL_HIDE_WELCOME-}" ]; then
	echo "\033[1;32mShell integration activated\033[0m"
else
	VSCODE_SHELL_HIDE_WELCOME=""
fi

Because I'm dumb and not sure how to fix it, the only thing I could try was to add typeset -g VSCODE_SHELL_INTEGRATION=1 to .p10k.zsh but that didn't change anything 😁

Any ideas please?
Thank you very much for your time!

@romkatv
Copy link
Owner

romkatv commented Apr 4, 2022

vscode integration doesn't work with powerlevel10k. It does make your shell slower though.

You can find a bit of context here: microsoft/vscode#145610 (comment)

If you search for "romkatv" in issues, you'll find a bit more.

@solid-pixel
Copy link
Author

thanks @romkatv that makes sense, no big deal then, I'll just turn it off. Like you said it does make the shell a bit slower anyway :)
Take care!

@romkatv romkatv closed this as completed Apr 14, 2022
@felipecrs
Copy link

@romkatv VS Code 1.68 closed one issue about ZSH:

Is there any chance VS Code Shell Integration can be enabled back in Powerlevel10k now? I haven't dived deep, though.

@romkatv
Copy link
Owner

romkatv commented Jun 10, 2022

Microsoft would need to document the extensions they implement in VS Code terminal. Once they do this, I and other developers of software running in the terminal will be able to write code that integrates with VS Code terminal. Currently the extensions are undocumented, so only Microsoft can use them and thus only Microsoft can implement integration between VS Code terminal and powerlevel10k.

@felipecrs
Copy link

Understood. I know that you have sent several advices for them, but do you know if there is any GitHub issue that specifically mentions and tracks this required work at their side? If there aren't any, I believe it would be the first step then.

@romkatv
Copy link
Owner

romkatv commented Jun 10, 2022

I don't know. I stopped following what they are doing when I learned which path they are taking and failing to dissuade them.

@felipecrs
Copy link

Got it. Thank you.

@felipecrs
Copy link

Out of curiosity, I agree with everything you said. I agree their approach isn't good, or even "legal"/ethical.

Still, as an end-user, I would rather bite the bullet and "allow" their bad approach (given the benefits).

@romkatv
Copy link
Owner

romkatv commented Jun 10, 2022

Are you implying that I'm intentionally doing something that prevents Microsoft's integration code from working? This is not the case. If they or anyone else sends a PR to powerlevel10k that enables integration with VS Code terminal, I'll accept it provided that it doesn't break anything else and doesn't increase maintenance costs too much. I am also offering to make code changes myself if Microsoft publishes documentation describing said changes. Please note that other terminals that implement comparable features do publish documentation for them, and powerlevel10k integrates with them out of the box.

@felipecrs
Copy link

No no, of course not. Your move of explicitly disabling the integration given the fact that it didn't work anyway and yet caused the prompt to be slower was great.

There is an issue in VS Code side talking about it, by the way. I hope it gets some traction in future:

I am also offering to make code changes myself if Microsoft publishes documentation describing said changes. Please note that other terminals that implement comparable features do publish documentation for them, and powerlevel10k integrates with them out of the box.

That's great, but I believe they would not do such a thing unless we make them understand the need for it. And I suppose there isn't a better person than you to write about it.

Yet, I'm not asking you to do anything. I understand the negative experience you had with them on this matter so far.

@Tyriar
Copy link

Tyriar commented Jun 10, 2022

FWIW the experience was very positive on our end, @romkatv taught us a lot and brought up some important problems. It was just too early to resolve some of the problems and it felt like we were moving towards having a completely separate VS Code implementation in powerlevel10k which I don't think either of us want.

@romkatv
Copy link
Owner

romkatv commented Jun 10, 2022

it felt like we were moving towards having a completely separate VS Code implementation in powerlevel10k which I don't think either of us want.

This is the path I was alluding to. Terminals usually publish specifications of protocols while VS Code team in this instance prefers to publish shell code instead and to avoid documenting or making backward compatibility promises for VS Code terminal extensions. This means that only VS Code team is capable and responsible for integrating with all applications running under the terminal. I know zsh well enough to see that this cannot be done in practice. One cannot write zsh shell integration that will work with all zsh prompts.

@Tyriar
Copy link

Tyriar commented Jun 10, 2022

@romkatv have been experimenting with supporting the more standard protocol in microsoft/vscode#146587 (comment). Seems to work pretty well by just plugging it in to what exists, looks like we might have a downgraded experience when extracting the command though which I think we were talking about in another issue.

Terminals usually publish specifications of protocols while VS Code team

Again this is an experimental feature, documenting it would be shooting ourselves in the foot and take away our ability to change it.

@romkatv
Copy link
Owner

romkatv commented Jun 10, 2022

Terminals usually publish specifications of protocols while VS Code team

Again this is an experimental feature, documenting it would be shooting ourselves in the foot and take away our ability to change it.

I think we are on the same page.

@huyz
Copy link

huyz commented Sep 8, 2022

I'm not clear about the current status. Did VSCode make changes so that it works with powerlevel10k?

@Tyriar
Copy link

Tyriar commented Sep 8, 2022

@huyz here's the update on our end microsoft/vscode#155639

@felipecrs
Copy link

I have been using the limited support since it was released, following these instructions:

@huyz
Copy link

huyz commented Sep 9, 2022

I have been using the limited support since it was released, following these instructions:

@felipecrs Great, that's exactly the counter-intuitive instructions I needed to know

@jajbuchanan
Copy link

Has there been any movement on this?

@romkatv
Copy link
Owner

romkatv commented Mar 6, 2023

You'll need to be more specific. Open a new issue and provide enough details in your question to allow me to answer it.

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

6 participants