starship icon indicating copy to clipboard operation
starship copied to clipboard

Add transient prompt configuration option

Open lberrymage opened this issue 4 years ago • 21 comments

Feature Request

Is your feature request related to a problem? Please describe.

Currently, every time a command is entered, the prompt is recreated on the line below. This is normal behavior, but arguably can look cluttery when the prompt extends beyond a single line.

Describe the solution you'd like

The solution I propose is a configuration option for a transient prompt. The prompt would appear to stay on one line while previous commands would appear as normal. Here's what this looks like in powerlevel10k. I don't know how to implement this, nor do any particular drawbacks come to mind. Powerlevel10k's implementation could be a good place to look for inspiration.

lberrymage avatar Jan 29 '20 23:01 lberrymage

Very cool feature but difficult to implement. Powerlevel10k code is virtually impenetrable so I'll give a few pointers that should help you if you decide to implement it in Starship.

There are two distinct approaches to implementing this feature.

  1. Truncate prompt in zle-line-finish, restore in precmd. See this post.
  2. Hook zle-line-init and wrap everything in recursive-edit. See this post.

The first fails when you interrupt modal widgets with Ctrl-C. See this bug report and my description here.

The second suppresses job monitor notifications as if no_notify option was in effect. See the bottom of this post.

Both of these problems make the simple implementations unusable. After banging my head against this for a month I managed to fix the first implementation. The code is quite hairy and I haven't posted it anywhere yet. The idea is to stop trying to prevent overzealous prompt truncation on Ctrl-C and instead quickly recover the original prompt when it becomes apparent that it shouldn't have been truncated in the first place. This requires wrapping all zle widgets and deferring a function with zle -F on a ready file descriptor.

In addition, both solutions trigger bugs in zsh: bug 1, bug 2, bug 3. The first two bugs cause zsh to segfault but thankfully there are workarounds -- follow the trail from the links I posted above. I wasn't able to find a workaround for the third bug but it triggers in a more limited set of circumstances and doesn't crash zsh, so it's not that terrible. If you find a workaround for it, please let me know.

romkatv avatar Jan 30 '20 07:01 romkatv

romkatv gave base level sample code for the two implementations here: https://github.com/romkatv/powerlevel10k/issues/888#issuecomment-657969840

kamalmarhubi avatar Nov 21 '20 15:11 kamalmarhubi

Cmd's Clink also has native support for transient prompts - https://chrisant996.github.io/clink/clink.html#transient-prompt

Would be cool if Starship can leverage this one day ✌

rashil2000 avatar Nov 16 '21 12:11 rashil2000

Cmd support (through Clink) was added in #3277. It included a transient-prompt and a transient-right-prompt, but later it was removed because as of now it was a single shell functionality. For anyone else looking to use this, I have pasted the relevant documentation here.


TransientPrompt and TransientRightPrompt in Cmd

Clink allows you to replace the previous-printed prompt with custom strings. This is useful in cases where all the prompt information is not always needed. To enable this, run clink set prompt.transient <value> (you need to do this only once) where <value> can be one of:

  • always: always replace the previous prompt
  • same_dir: replace the previous prompt only if the working directory is same
  • off: do not replace the prompt (i.e. turn off transience)

Make the following changes to your starship.lua to customize what gets displayed on the left and on the right:

  • First, declare a custom prompt filter my_custom_starship after the Starship initialization command. Define it to return the prompt string as-is.
  • By default, the left side of input gets replaced with >. To customize this, define a new function called my_custom_starship:transientfilter. This function receives the current prompt as a string that you can utilize.
  • By default, the right side of input is empty. To customize this, define a new function called my_custom_starship:transientrightfilter. This function also receives the current prompt as a string that you can utilize.

In the end it will look something like this:

-- Initialize Starship
load(io.popen('starship init cmd'):read("*a"))()

-- Define a custom prompt filter
local my_custom_starship = clink.promptfilter(10)
function my_custom_starship:filter(prompt)
  return prompt
end

-- Display Starship's `character` module on left side
function my_custom_starship:transientfilter(prompt)
  return io.popen("starship module character"
    .." --keymap="..rl.getvariable('keymap')
  ):read("*a")
end

-- Display the time at which the last command was started on right side
function my_custom_starship:transientrightfilter(prompt)
  return io.popen("starship module time"):read("*a")
end

rashil2000 avatar Jan 10 '22 08:01 rashil2000

@rashil2000 does this still work?

max397574 avatar Apr 22 '22 14:04 max397574

Haven't used it in a while, what error did you face?

rashil2000 avatar Apr 22 '22 14:04 rashil2000

I haven't tried it yet

max397574 avatar Apr 22 '22 15:04 max397574

any progress considering the transient prompt?

tummetott avatar Jun 22 '22 13:06 tummetott

@Tummetott it's not dependent on Starship, it's a feature of the respective shell

rashil2000 avatar Jun 22 '22 14:06 rashil2000

no it works with my shell with other prompts

max397574 avatar Jun 22 '22 19:06 max397574

I would love this feature to be supported in Starship. Oh My Posh seems to be able to do it in cmd (as discussed above), fish, zsh and powershell, so I would hope it to be possible to properly integrate it?

FWest98 avatar Jun 27 '22 21:06 FWest98

@chipbuster I've got transience working for Starship in PowerShell (thanks to Oh-My-Posh). Since this functionality is now available for more than one shell (Cmd, and now PowerShell), will it be acceptable in the Advanced Config section of docs?

rashil2000 avatar Jun 29 '22 19:06 rashil2000

@rashil2000 Sure!

Quick disclaimer: I've been considering reorganizing the docs so that all the shell-specific stuff is in one place, but I think we can deal with that when we need to.

chipbuster avatar Jun 30 '22 18:06 chipbuster

This missing feature still holds me back to switch to Starship. Would be amazing to see this getting implemented. 🤗

weilbith avatar Jul 02 '22 08:07 weilbith

PR is up #4143

I'm not a very experienced fish user, but if anyone is willing to take a shot at it they can checkout my comment in the PR.

rashil2000 avatar Jul 02 '22 11:07 rashil2000

Reading the linked merged pr it seems it only partially implements that feature request (not for zsh, etc.).

ibotty avatar Sep 09 '22 15:09 ibotty

pr for zsh https://github.com/starship/starship/pull/4205

max397574 avatar Sep 09 '22 16:09 max397574

Reopening this, we have added this feature for fish but no other shells, let's keep this open at least until the existing PR for zsh has been merged.

andytom avatar Sep 10 '22 11:09 andytom

Reopening this, we have added this feature for fish but no other shells, let's keep this open at least until the existing PR for zsh has been merged.

There hasn't been progress in the Zsh PR, is there a need for contributors? Putting a tag for "contribution-needed" could help?

NewtonChutney avatar Mar 10 '24 05:03 NewtonChutney

Unfortunately nobody is confident enough to implement this with all its edge cases

tummetott avatar Mar 10 '24 11:03 tummetott