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

[FEATURE] Enable zsh integration #107

Merged
merged 11 commits into from Jun 4, 2018
Merged

[FEATURE] Enable zsh integration #107

merged 11 commits into from Jun 4, 2018

Conversation

mlhamel
Copy link
Collaborator

@mlhamel mlhamel commented May 30, 2018

Why

This PR is enable shell integration for zsh

How

Splitting out the logic of the shell integration into separated file: 1 for bash, 1 for zsh and a common 1

The approach taken was inspired by https://superuser.com/questions/735660/whats-the-zsh-equivalent-of-bashs-prompt-command/735969#735969

Example:

➜  dad git:(zsh-integration) ✗ ls
DESIGN.md  Gopkg.lock Gopkg.toml LICENSE    README.md  dad        dad.go     dev.yml    dist       docs       examples   install.sh pkg        script     tests      vendor
🐼  golang activated. (version: 1.10.1)
🐼  python activated. (version: dad-327487016-3.6.5)

@mlhamel mlhamel requested a review from pior as a code owner May 30, 2018 01:02
@pior
Copy link
Member

pior commented May 30, 2018

/cc @paccorsi

Copy link
Member

@pior pior left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

package integration

var zshSource = `
prmptcmd() { $("__dad_prompt_command") }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to call the prompt command in a subshell with $(...) ?
Could we just call it directly? prmptcmd() { __dad_prompt_command }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

promptcmd? (with a o)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes sadly it seems it needs to be called in a subshell cause if not, it would run as soon as you type prmptcmd() { __dad_prompt_command } and not everytime a prompt is being displayed

fmt.Println(bashSource)
var currentShell = os.Getenv("SHELL")

fmt.Println(shellSource)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we print most of the source already if we have a major failure condition right after?

If we are running an unsupported shell, I think we should not print anything to stdout, because it will be eval'ed... by this unsupported shell.

I know I mentioned the general idea of preferring warnings rather than stopping execution...(because it could partially work and that could be enough for some people) but in this case, it looks like it could actually be very dangerous...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you're right it should work without shell integration but we should not have a half one

} else if strings.HasSuffix(currentShell, "zsh") {
fmt.Println(zshSource)
} else {
fmt.Println(color.Brown("Your shell is not supported"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The execution context is an eval:

eval "$(dad --shell-init --with-completion)"

So anything printed on the stdout will be eval'ed.
To display a warning, it must be printed on stderr.


color "github.com/logrusorgru/aurora"
)

func Print() {
fmt.Println(bashSource)
var currentShell = os.Getenv("SHELL")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an idea, we could warn in case the value is empty, since it would be a very special case of "unknown shell".
That may help someone debug their environment someday.

@pior pior changed the title Enable zsh integration [FEATURE] Enable zsh integration May 30, 2018
} else if strings.HasSuffix(currentShell, "zsh") {
fmt.Println(shellSource, zshSource)
} else if {
fmt.Fprintln(os.Stderr, color.Red("Your shell configuration is undefined"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Maybe mention that the SHELL variable itself is empty, and this is what's weird.

} else {
fmt.Fprintln(os.Stderr, color.Brown("Your shell is not supported"))
}
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return

Copy link
Member

@pior pior left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

fmt.Println(shellSource, bashSource)
} else if strings.HasSuffix(currentShell, "zsh") {
fmt.Println(shellSource, zshSource)
} else if {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {


var zshSource = `
prmptcmd() { $("__dad_prompt_command") }
precmd_functions=(prmptcmd)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

precmd_functions+=(prmptcmd)
To preserve the other pre-command hooks.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could also be wrapped in a if statement in case dad is already activated

if [[ ! "${precmd_functions[@]}" == *__dad_prompt_command* ]]; then
  precmd_functions+=(prmptcmd)
fi

dev.yml Outdated
@@ -1,4 +1,5 @@
up:
- python: 3.6.5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙅‍♂️

Copy link
Member

@pior pior left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have an integration test on this. But it's perfectly fine that it's NOT included in this PR.
I like the idea of a code base that every one incrementally (a feature added without tests is better than a never merged PR)

Welcome Zsh 🎉

@mlhamel mlhamel merged commit 4b6ff88 into master Jun 4, 2018
@mlhamel mlhamel deleted the zsh-integration branch June 4, 2018 02:05
@pior pior mentioned this pull request Jun 4, 2018
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

Successfully merging this pull request may close these issues.

None yet

3 participants