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

cmd: change the prompt in lua #961

Merged
merged 1 commit into from
Jun 21, 2016

Conversation

jankatins
Copy link
Contributor

@jankatins jankatins commented May 24, 2016

This keeps the PROMPT variable as is and changes the prompt to the cmder style
in the clink code.

This has two advantages:

  • opening a cmd in a cmder session will now show the old prompt code instead of
    a ugly raw prompt without the replacements. This led to ugly output when a
    batch file echoed their content (e.g conda build recipe/).
  • when a command rewrites the prompt (e.g. an activate in a virtualenv), these
    commands sometime simple overwrite the PROMPT so that the cmder enhancements
    were not anymore in place. Now we simply don't care and overwrite it with our
    stuff. This might mean that a user has to install a lua script so that e.g.
    conda environments are visible on the prompt.

Closes: #749

@jankatins
Copy link
Contributor Author

Example old:

C:\Users\jschulz
λ cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten.

C:\Users\jschulz {git}{hg}
{lamb} exit

C:\Users\jschulz
λ

new:

c:\data\external\cmder (prompt-in-clink)
λ cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten.

c:\data\external\cmder>exit

c:\data\external\cmder (prompt-in-clink)
λ

@jankatins
Copy link
Contributor Author

jankatins commented May 24, 2016

Just as a reference:

A filter which adds a conda environment string in the front of the lambda:

---
 -- Find out the basename of a file/directory (last element after \ or /
 -- @return {basename}
---
function basename(inputstr)
        sep = "\\/"
        local last = nil
        local t={} ; i=1
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                --t[i] = str
                --i = i + 1
                last = str
        end
        return last
end

---
 -- Find out current conda env
 -- @return {false|conda env name}
---
function get_conda_env()
    env_path = clink.get_env('CONDA_DEFAULT_ENV')
    if env_path then
        basen = basename(env_path)
        return basen
    end
    return false
end

---
 -- add conda env name 
---
function conda_prompt_filter()
    -- add in conda env name
    local conda_env = get_conda_env()
    if conda_env then
        clink.prompt.value = string.gsub(clink.prompt.value, "{lamb}", "["..conda_env.."] {lamb}")
    end
end

clink.prompt.register_filter(conda_prompt_filter, 20)

Looks like this:

c:\data\external\cmder (prompt-in-clink)
λ activate borked35
Deactivating environment "C:\portabel\miniconda"...
Activating environment "C:\portabel\miniconda\envs\borked35"...

c:\data\external\cmder (prompt-in-clink)
[borked35] λ

@jankatins
Copy link
Contributor Author

Just for the record: I'm running my cmder with this PR since a few days and it works great: no more garbled output in conda build :-)

@nullpunkt
Copy link

I would like this a +1 hence my installation actually keeps displaying {git}{lamda} and the other placeholders even though i already installed 0.4.7 of clink.

unbenannt

@jankatins
Copy link
Contributor Author

@nullpunkt I don't think this is the same "problem" :-( This only "fixes" the problem that a subshell gets a proper commandline, not the one which is started in cmder.

-> please discuss in a different issue

@jankatins
Copy link
Contributor Author

rebased on top of current development

This keeps the PROMPT variable as is and changes the prompt to the cmder style
in the clink code.

This has two advantages:

* opening a cmd in a cmder session will now show the old prompt code instead of
  a ugly raw prompt without the replacements. This led to ugly output when a
  batch file echoed their content (e.g `conda build recipe/`).

* when a command rewrites the prompt (e.g. an activate in a virtualenv), these
  command sometimes simply overwrites the PROMPT so that the cmder enhancements
  were not anymore in place. Now we simply don't care and overwrite it with our
  stuff in the clink part. This might mean that a user has to install a lua
  script so that e.g. conda environments are visible on the prompt.
@MartiUK
Copy link
Member

MartiUK commented Jun 21, 2016

👍 LGTM

@Stanzilla Stanzilla merged commit 84d44b1 into cmderdev:development Jun 21, 2016
@Stanzilla
Copy link
Member

Merged. Thank you for that! :)

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

4 participants