Skip to content

Commit

Permalink
Support ANSI colors in --prompt string
Browse files Browse the repository at this point in the history
Close #2086
  • Loading branch information
junegunn committed Jul 5, 2020
1 parent dda3e3c commit 8e027c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
19 changes: 16 additions & 3 deletions src/terminal.go
Expand Up @@ -64,7 +64,7 @@ type Terminal struct {
initDelay time.Duration
infoStyle infoStyle
spinner []string
prompt string
prompt func()
promptLen int
pointer string
pointerLen int
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 8e027c4

Please sign in to comment.