Support ANSI colors in --prompt string

Close #2086
This commit is contained in:
Junegunn Choi 2020-07-05 16:16:46 +09:00
parent dda3e3c39a
commit 8e027c445f
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 17 additions and 3 deletions

View File

@ -26,6 +26,7 @@ CHANGELOG
# Preview window hidden by default, it appears when you first hit '?'
fzf --bind '?:preview:cat {}' --preview-window hidden
```
- Added support for ANSI colors in `--prompt` string
- Vim plugin
- `tmux` layout option for using fzf-tmux
```vim

View File

@ -64,7 +64,7 @@ type Terminal struct {
initDelay time.Duration
infoStyle infoStyle
spinner []string
prompt string
prompt func()
promptLen int
pointer string
pointerLen int
@ -469,7 +469,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
killChan: make(chan int),
tui: renderer,
initFunc: func() { renderer.Init() }}
t.prompt, t.promptLen = t.processTabs([]rune(opts.Prompt), 0)
t.prompt, t.promptLen = t.parsePrompt(opts.Prompt)
t.pointer, t.pointerLen = t.processTabs([]rune(opts.Pointer), 0)
t.marker, t.markerLen = t.processTabs([]rune(opts.Marker), 0)
// Pre-calculated empty pointer and marker signs
@ -479,6 +479,19 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
return &t
}
func (t *Terminal) parsePrompt(prompt string) (func(), int) {
var state *ansiState
trimmed, colors, _ := extractColor(prompt, state, nil)
item := &Item{text: util.ToChars([]byte(trimmed)), colors: colors}
output := func() {
t.printHighlighted(
Result{item: item}, t.strong, tui.ColPrompt, tui.ColPrompt, false, false)
}
_, promptLen := t.processTabs([]rune(trimmed), 0)
return output, promptLen
}
func (t *Terminal) noInfoLine() bool {
return t.infoStyle != infoDefault
}
@ -780,7 +793,7 @@ func (t *Terminal) placeCursor() {
func (t *Terminal) printPrompt() {
t.move(0, 0, true)
t.window.CPrint(tui.ColPrompt, t.strong, t.prompt)
t.prompt()
before, after := t.updatePromptOffset()
t.window.CPrint(tui.ColNormal, t.strong, string(before))