6hja1w
Last Updated: February 25, 2016
·
4.146K
· hauleth
Me

`rbenv shell` and fish

Last time I switched to fish shell and have some problems with rbenv shell command. So I needed to write own wrapper that simulate function provided by rbenv init - and here it go:

set -x PATH $HOME/.rbenv/shims $PATH
rbenv rehash >/dev/null ^&1

function rbenv_shell
  set -l vers $argv[1]

  switch "$vers"
    case '--complete'
      echo '--unset'
      echo 'system'
      exec rbenv-versions --bare
      return
    case '--unset'
      set -e RBENV_VERSION
    case ''
      if [ -z "$RBENV_VERSION" ]
        echo "rbenv: no shell-specific version configured" >&2
        return 1
      else
        echo "$RBENV_VERSION"
      end
    case '*'
      rbenv prefix "$vers" > /dev/null
      set -g -x RBENV_VERSION "$vers"
  end
end

function rbenv
  set -l command $argv[1]
  [ (count $argv) -gt 1 ]; and set -l args $argv[2..-1]

  switch "$command"
    case shell
      rbenv_shell $args
    case '*'
      command rbenv $command $args
  end
end
Say Thanks
Respond

11 Responses
Add your response

5042
6d53507494cadf3a930696364e634992

Thanks, I spent an hour on this.

over 1 year ago ·
6506
260d6bc4f6b1548e3c4fed0f538b753a

Minor bug: you can add [ (count $argv) -eq 0 ]; and set -l command '' at 32th line to prevent bug on invoking rbenv without an argument =P

over 1 year ago ·
6533
Me

@jtomaszewski There is no need to do that. In fish (like in any other shell) when you try to access to non-existent variable then you get empty value.

over 1 year ago ·
6608
260d6bc4f6b1548e3c4fed0f538b753a

Well, yes, but even that, it triggers an error on switch command. f.e. switch $idontexist; end

over 1 year ago ·
6688
Me

@jtomaszewski give example when this snippet doesn't work. I cannot found that one. But if it really bother you then you can write set -l command "$argv[1]".

over 1 year ago ·
6689
260d6bc4f6b1548e3c4fed0f538b753a

Yeah, your way is better. The example is this and only this: rbenv. Ofcourse, unless you're not lazy as me, you can always write rbenv help.
Anyway, nevermind, the problem is unimportant.

over 1 year ago ·
6784
Me

@jtomaszewski Fish allow setting empty variables so there is no need to create fallback to empty string, so if there is no arguments then rbenv will be called without any arguments. Everything works as expected.

over 1 year ago ·
8193
De1373715118caea7c80e173bd7ab04e

@hauleth It is not in the creation that the error happens, but in the switch. It doesn't matter if the variable exists or is empty because the variable is expanded and the result is a switch call without arguments. If you enclose it in quotes, like you did in the rbenv_shell function then you have one argument that happens to be empty. ;D

Thanks for this. It is great. Have you considered submitting it to oh-my-fish?

over 1 year ago ·
8194
De1373715118caea7c80e173bd7ab04e

@hauleth Ok, one more problem: You are returning 1 after --unset. Also, unless I am misunderstanding the way return and switch work on fish, empty returns don't seem necessary since they are each the last command of the function.

over 1 year ago ·
9846
8f93a872e399bc1353cc8d4e791d5401

rbenv master now supports fish:

$ rbenv init fish
# Load rbenv automatically by adding
# the following to ~/.config/fish/config.fish:

. (rbenv init -|psub)
over 1 year ago ·
11371
F9c4bb3ef6db620d6775ee9d926a488e

Thank you, this worked.

over 1 year ago ·