anki-code / xontrib-prompt-starship

Starship cross-shell prompt in xonsh shell.
MIT License
51 stars 8 forks source link

Is it possible to fix the 'empty line on terminal open' issue in this xontrib? #7

Open StanSvec opened 1 year ago

StanSvec commented 1 year ago

Starship prompt by default prints empty line when the terminal starts. This issue is reported here and here. Despite both issues are closed the problem hasn't been fixed yet. I'm wondering if that's something which can be additionally fixed in this xontrib? Thanks!

anki-code commented 1 year ago

Try to cut empty line here:

https://github.com/anki-code/xontrib-prompt-starship/blob/00e7c8bd1131b28aae94822c2b44d5f0a1e72a7c/xontrib/prompt_starship.py#L11-L16

i.e. "Using starship cross-shell prompt to rendering right sections" in xontrib-prompt-bar

StanSvec commented 1 year ago

Oh so simple, thanks! I came up with a naive solution:

_FIRST_PROMPT = True

def _starship_prompt(cfg=None):
    global _FIRST_PROMPT
    with __xonsh__.env.swap({'STARSHIP_CONFIG': cfg} if cfg else {}):
        prompt = __xonsh__.subproc_captured_stdout([
            'starship', 'prompt',
            '--status', str(int( __xonsh__.history[-1].rtn)) if len(__xonsh__.history) > 0 else '0',
            '--cmd-duration' , str(int((__xonsh__.history[-1].ts[1] - __xonsh__.history[-1].ts[0])*1000)) if len(__xonsh__.history) > 0 else '0',
            '--jobs', str(len(__xonsh__.all_jobs))
        ])
    if _FIRST_PROMPT:
        _FIRST_PROMPT = False
        return prompt.lstrip()
    return prompt

Works fine unless the screen is cleared (Ctrl + L). Is there a way how to detect execution of the clear command, so I could add one more condition for the left strip? Thanks!

anki-code commented 1 year ago

I think you're using prompt toolkit for $SHELL and the first way is to search there.

StanSvec commented 1 year ago

Hmm, I didn't even know that prompt toolkit provides shell functionality, because I don't have any experience with this library. Is it possible to access ptk's API somehow from the prompt_starship.py file please?