pure icon indicating copy to clipboard operation
pure copied to clipboard

Add support for modifying the pre-prompt (add / edit / remove pre-prompt parts)

Open mafredri opened this issue 6 years ago • 10 comments

Issuehunt badges

This issue is for tracking discussion and development of an extension to the pre-prompt that will allow users to make modifications to it by some means.

The purpose is to minimize the need for our users to maintain a fork just so that they can add some information to the pre-prompt.

Prologue: It would be nice to provide an API that allows user defined segments to run asynchronously as well. And it should be simple enough that it’s hard to make mistakes. Ordering of parts/segments would also be nice and if we ever make a new segment it would be nice if the user doesn’t have to manually enable it due to hard coding the current segments, or whatnot.

Perhaps functions like prompt_pure_add_segment myfunc and _set_order myfunc 3 (moves it to pos 3 and old 3-n are offset by one).

Another thought I’ve had is more hook-like, e.g. right before re-rendering the prompt. A hook function is called where you can manually modify the preprompt array, but it requires a bit more intimate knowledge of how to work with zsh arrays.

Issue originally raised by @sindresorhus in https://github.com/sindresorhus/pure/pull/458#issuecomment-459633908

There is a $80.00 open bounty on this issue. Add more on Issuehunt.

mafredri avatar Feb 04 '19 18:02 mafredri

do you expect users to list segments they would love to see available (so that you can pre-code them, even if they are not enabled by default)? On my side, I'd love to see

  • numbre of stash
  • upstream name

In case you are not expecting input like this, feel free to delete my comment :-)

CircleCode avatar Feb 08 '19 15:02 CircleCode

for workaround I added preprompt_parts+=($(custom_prompt_parts 2>/dev/null)) to prompt_pure_preprompt_render() with no other customization options (I just needed way to add extra parts to the end)

Then just have custom_prompt_parts() in my settings env

But something similar would be nice.

warbaque avatar Feb 11 '19 07:02 warbaque

Just throwing in my support - I've been using the https://github.com/intelfx/pure branch (which is now really far behind), but allows customization by adding to prompt_pure_pieces (https://github.com/intelfx/pure#example-1)

(I've added things such as a shrunken path via oh-my-zsh shrink_path, kubernetes context via kctx, aws-vault profile, etc.)

onyxraven avatar Mar 02 '19 21:03 onyxraven

I would also like to see this feature added. I enjoy using t to manage quick todo tasks, and I would like to add the number of incomplete tasks to my prompt.

awmottaz avatar Mar 26 '19 15:03 awmottaz

@issuehunt has funded $80.00 to this issue.


IssueHuntBot avatar May 02 '19 06:05 IssueHuntBot

One use-case I had in mind for this:

I've been wanting a way to automatically switch the active Node.js version when I enter a Node.js project. It would switch to match the Node.js range in the "engines" field in package.json. That way I can ensure I don't use JS/Node.js features that should not be used.

I would use n for the actual switching. I don't like nvm. It's too buggy.

sindresorhus avatar Jun 17 '19 07:06 sindresorhus

@sindresorhus take a look at nvm

awmottaz avatar Jun 17 '19 23:06 awmottaz

@awmottaz Yes, I'm aware of nvm and the other version managers.

sindresorhus avatar Jun 19 '19 19:06 sindresorhus

I can see a copule of ways of approaching this customization.

Step 0. split the rendering of part prompts in to functions;

Then:

Option a: something like intelfx branch.

# optionally define custom generators
prompt_custom() {
	preprompt_parts+=( custom  )
}

prompt pure

# add the generator where it's needed
prompt_pure_pieces=(
	${prompt_pure_pieces:0:2}
	prompt_custom
	${prompt_pure_pieces:2}
)

Thought the API suggested by the OP is probably nicer

Perhaps functions like prompt_pure_add_segment myfunc and _set_order myfunc 3 (moves it to pos 3 and old 3-n are offset by one).

This allows "full" control but would be fragile to ordering change of the built in prompt parts.

Option b: add pre and post hooks to the functions from step 0. meaning something like this would add sparkles to the start of every line

custom_start() {
    	preprompt_parts+=(✨)
}

pure_add_hook pre path "custom_start" # ZSH is new to me - can we pass functions by anything other than name?

Option c: combine both of them for total control.

Does anyone have thoughts?

ashb avatar Jun 21 '19 19:06 ashb

We could look at https://github.com/denysdovhan/spaceship-prompt for inspiration about customizability (They already got a lot of inspiration from Pure).

sindresorhus avatar Jun 21 '19 19:06 sindresorhus