pew-org/pew

show virtualenv name under windows prompt

Opened this issue · 1 comments

I'm using pipenv on windows 7, and it uses pew to manage virtual environments.

I'm missing the virtual environment name under PROMPT. (as occurs under bash or fish)

image

This could be set as set "PROMPT=%environment_name% %PROMPT%"

where environment_name represents the name of the environment.

It would be based on virtualenv activate.bat script.

running into the same issue yesterday, I played around a bit.

I'm using powershell, and fixed the issue for this shell by creating :

def fork_powershell(env, shellcmd, cwd):
    or_ctrld = '' if windows else "or 'Ctrl+D' "
    err("Launching subshell in virtual environment. Type 'exit' ", or_ctrld,
        "to return.", sep='')
    if 'VIRTUAL_ENV' in os.environ:
        err("Be aware that this environment will be nested on top "
            "of '%s'" % Path(os.environ['VIRTUAL_ENV']).name)
    shellcmd.extend(
        ("-NoExit",
         "-File",
         os.path.join(workon_home / env,
                      "Scripts", "activate.ps1"),
         ))
    return inve(env, *shellcmd, cwd=cwd)

and adding a call to it in the shell function :

...
    elif shell_name == "powershell":
        return fork_powershell(env, [shell], cwd)

At least for powershell, the execution of the activate.ps1 script is missing (don't know if this is an intent).

However I'm not sure this solution is good enough nor clean enough to be part of pew master's codebase.

PS:

Here the doc of the powershell arguments I used:

-NoExit
Does not exit after running startup commands.

-File - | <filePath> <args>

If the value of File is "-", the command text is read from standard input. Running powershell -File - without redirected standard input starts a regular session. This is the same as not specifying the File parameter at all.

If the value of File is a file path, the script runs in the local scope ("dot-sourced"), so that the functions and variables that the script creates are available in the current session. Enter the script file path and any parameters. File must be the last parameter in the command. All values typed after the File parameter are interpreted as the script file path and parameters passed to that script.

Parameters passed to the script are passed as literal strings, after interpretation by the current shell. For example, if you are in cmd.exe and want to pass an environment variable value, you would use the cmd.exe syntax: powershell.exe -File .\test.ps1 -TestParam %windir%

In contrast, running powershell.exe -File .\test.ps1 -TestParam $env:windir in cmd.exe results in the script receiving the literal string $env:windir because it has no special meaning to the current cmd.exe shell. The $env:windir style of environment variable reference can be used inside a Command parameter, since there it will be interpreted as PowerShell code.

When the value of File is a file path, File must be the last parameter in the command because any characters typed after the File parameter name are interpreted as the script file path followed by the script parameters.

You can include the script parameters and values in the value of the File parameter. For example: -File .\Get-Script.ps1 -Domain Central

Typically, the switch parameters of a script are either included or omitted. For example, the following command uses the All parameter of the Get-Script.ps1 script file: -File .\Get-Script.ps1 -All

In rare cases, you might need to provide a Boolean value for a switch parameter. It is not possible to pass an explicit boolean value for a switch parameter when running a script in the way. This limitation was removed in PowerShell 6 (pwsh.exe).