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

Prevent other apps from overwriting the prompt? #749

Closed
jankatins opened this issue Dec 1, 2015 · 17 comments
Closed

Prevent other apps from overwriting the prompt? #749

jankatins opened this issue Dec 1, 2015 · 17 comments

Comments

@jankatins
Copy link
Contributor

Is there a way to prevent other programms from overwriting the prompt? I've conda, which adds the current environment to the start (and resets the cmder prompt back to the default) but also make the prompt behave very badly when I use 'up' to navigate to previous multiline commands (see screenshot: empty line between first and last and cursor in the empty line; I can edit, it just doesn't show up in the right place)

2015-12-02_004433

@vvartivarian-nbcu
Copy link

The multiline bug is in cmdr.lua get_git_status() caused by using os.execute(). I used io.popen instead here and it is fixed

@danspam
Copy link

danspam commented Dec 3, 2015

Suffering from the same double line bug here and using io.popen() instead of os.execute() does appear to cure it. Should be marked as a bug and fixed. Unfortunately I know nothing about lua so I would be completely unqualified to know if that is the correct fix and to issue a PR

@jankatins
Copy link
Contributor Author

I can also confirm that the extra-line bug is gone when using io.popen(). Will send a PR...

@jankatins
Copy link
Contributor Author

Ok, waiting for news on #736, as that would conflict with this PR...

@jankatins
Copy link
Contributor Author

I've a working solution which sets the prompt in my personal config\cmder.lua (after #736). Part of it sets the prompt in lua if the current prompt does not include a {lamb} anymore. An idea would be to remove the prompt call from init.bat and add such a filter to vendor\cmder.lua.

---
 -- 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

---
 -- after conda activate: reset prompt and add conda env name 
---
function conda_prompt_filter()
    -- reset to original, e.g. after conda activate destroyed it...
    if string.match(clink.prompt.value, "{lamb}") == nil then
        -- orig: $E[1;32;40m$P$S{git}{hg}$S$_$E[1;30;40m{lamb}$S$E[0m
        -- color codes: "\x1b[1;37;40m"
        cwd = clink.get_cwd()
        prompt = "\x1b[1;32;40m{cwd} {git}{hg} \n\x1b[1;30;40m{lamb} \x1b[0m"
        new_value = string.gsub(prompt, "{cwd}", cwd)
        clink.prompt.value = new_value
    end
    -- 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, 10)

@vvartivarian-nbcu
Copy link

@vladimir-kotikov Would you give your latest clink scripts to cmder? It fixes the multiline bug and you added lots of new stuff too :)

@Stanzilla
Copy link
Member

cmder already comes with clink-completions 0.2.1 in the dev branch

@vvartivarian-nbcu
Copy link

@Stanzilla Unless theres a branch i cant see, I dont think that's correct, it looks like clink-completions in cmder are from March 2015, dating back to the initial birth of clink-completions. The author has changed to disuse os.execute() shortly after that, but we still have the old code in cmder.lua and this bug exists.

@Stanzilla
Copy link
Member

@vladimir-kotikov
Copy link
Contributor

It fixes the multiline bug

@vvartivarian-nbcu, how is it fixes that? The problem is in the https://github.com/cmderdev/cmder/blob/master/config/cmder.lua#L139, which is a part of Cmder

@jankatins
Copy link
Contributor Author

@Stanzilla Can someon from the core team chime in here: would it be ok to add a failsafe to the cmder.lua file which looks for {lamb} in the prompt and if it is not there would reset the prompt to a known good value?

I could also remove the prompt call from init.bat and handle the stuff completely from cmder.lua...

The code is some variant of

---
 --  Change the prompt to the nice cmder style
---
function reset_prompt_filter()
    -- reset to original, e.g. after conda activate destroyed it...
    if string.match(clink.prompt.value, "{lamb}") == nil then
        -- orig: $E[1;32;40m$P$S{git}{hg}$S$_$E[1;30;40m{lamb}$S$E[0m
        -- color codes: "\x1b[1;37;40m"
        cwd = clink.get_cwd()
        prompt = "\x1b[1;32;40m{cwd} {git}{hg} \n\x1b[1;30;40m{lamb} \x1b[0m"
        new_value = string.gsub(prompt, "{cwd}", cwd)
        clink.prompt.value = new_value
    end
end

clink.prompt.register_filter(reset_prompt_filter, 10)

jankatins added a commit to jankatins/cmder that referenced this issue Dec 4, 2015
If having a long line of input in histroy, which went on into the
second row and using <up> to get back to it, you got the effect
that the input was split over three lines: "text, empty, text",
but using the arrows do move to the front of the first line was
moving the cursor in the second (empty) row. You could change
the text, but you needed to do it "in the dark".  No idea why
s/os.execute/is.popen/ fixes this, but it does.

Partly adresses cmderdev#749
@jankatins
Copy link
Contributor Author

The git problem with long lines and using history is in #756

@Stanzilla
Copy link
Member

Hrm is that really generic workaround or more like a Conda workaround?

@jankatins
Copy link
Contributor Author

I suspect that there are more apps which set the prompt to indicate a current status but in my daily usage, its just conda.

virtualenv also does it: https://github.com/pypa/virtualenv/blob/develop/virtualenv_embedded/activate.bat

@jankatins
Copy link
Contributor Author

after the latest conda update, I now have the following in my config/user.lua (needs the changes in the next release...):

---
 -- Find out if the String starts with Start
 -- @return {boolean}
---
function string.starts(String,Start)
   return string.sub(String,1,string.len(Start))==Start
end

---
 -- reset prompt if someone tampered with it...
---
function reset_prompt_filter()
    -- reset to original, e.g. after conda activate destroyed it...
    if string.match(clink.prompt.value, "{lamb}") == nil or not string.starts(clink.prompt.value,"\x1b[") then
        -- orig: $E[1;32;40m$P$S{git}{hg}$S$_$E[1;30;40m{lamb}$S$E[0m
        -- color codes: "\x1b[1;37;40m"
        cwd = clink.get_cwd()
        prompt = "\x1b[1;32;40m{cwd} {git}{hg} \n\x1b[1;30;40m{lamb} \x1b[0m"
        new_value = string.gsub(prompt, "{cwd}", cwd)
        clink.prompt.value = new_value
    end
end

clink.prompt.register_filter(reset_prompt_filter, 10)

For how to add a conda environment information: see here: http://stackoverflow.com/a/34066161/1380673

@jankatins
Copy link
Contributor Author

If there shouldn't be a "reset" of the prompt or maybe even setting the prompt value directly in the lua code, then this issue can be closed

@jankatins
Copy link
Contributor Author

Another argument for doing the complete prompt work in lua: if you start a new cmd in a comder window, the prompt is ugly:

c:\data\external\conda-packages\conda-build-wheel (master)
λ cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten.

c:\data\external\conda-packages\conda-build-wheel {git}{hg}
{lamb} exit

c:\data\external\conda-packages\conda-build-wheel (master)
λ

-> no replacements of {...} and the colors (not shown :-)) are also not right.

This is one of my private setups where I implemented the above workaorunds and temporary disabled the prompt call in vendor/init.bat:

Found ssh-agent at 7612
Found ssh-agent socket at /tmp/ssh-Cj5OAwywjb7p/agent.7904
C:\Users\jschulz
λ cmd
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. Alle Rechte vorbehalten.

C:\Users\jschulz>exit

C:\Users\jschulz
λ

The colors are still broken (at least the first prompt from cmder doe snot retain it's green), but the second cmdline (which is from cmd, does not has a line break) does not include cmder stuff...

sonictk pushed a commit to sonictk/cmder that referenced this issue May 21, 2016
If having a long line of input in histroy, which went on into the
second row and using <up> to get back to it, you got the effect
that the input was split over three lines: "text, empty, text",
but using the arrows do move to the front of the first line was
moving the cursor in the second (empty) row. You could change
the text, but you needed to do it "in the dark".  No idea why
s/os.execute/is.popen/ fixes this, but it does.

Partly adresses cmderdev#749
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

5 participants