Skip to content

Commit

Permalink
add header option (#111)
Browse files Browse the repository at this point in the history
* add header oprion

* fix after review: change to use rune, truncate too long header

* Update fuzzyfinder.go

Co-authored-by: ktr <ktr@syfm.me>

* Update fuzzyfinder.go

Co-authored-by: ktr <ktr@syfm.me>

* fix after review

Co-authored-by: ktr <ktr@syfm.me>
  • Loading branch information
momom-i and ktr0731 committed Aug 31, 2021
1 parent 2bff5ce commit 1df34fd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
3 changes: 3 additions & 0 deletions fuzzing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,13 @@ func TestFuzz(t *testing.T) {
var (
iface interface{}
promptStr string
header string
)
fuzz.Fuzz(&promptStr)
fuzz.Fuzz(&header)
opts := []fuzzyfinder.Option{
fuzzyfinder.WithPromptString(promptStr),
fuzzyfinder.WithHeader(header),
}
if *hotReload {
iface = &tracks
Expand Down
40 changes: 29 additions & 11 deletions fuzzyfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func (f *finder) _draw() {
maxWidth = width/2 - 1
}

maxHeight := height

// prompt line
var promptLinePad int

Expand All @@ -132,7 +134,7 @@ func (f *finder) _draw() {
Foreground(tcell.ColorBlue).
Background(tcell.ColorDefault)

f.term.SetContent(promptLinePad, height-1, r, nil, style)
f.term.SetContent(promptLinePad, maxHeight-1, r, nil, style)
promptLinePad++
}
var r rune
Expand All @@ -144,22 +146,38 @@ func (f *finder) _draw() {
Bold(true)

// Add a space between '>' and runes.
f.term.SetContent(promptLinePad+w, height-1, r, nil, style)
f.term.SetContent(promptLinePad+w, maxHeight-1, r, nil, style)
w += runewidth.RuneWidth(r)
}
f.term.ShowCursor(promptLinePad+f.state.cursorX, height-1)
f.term.ShowCursor(promptLinePad+f.state.cursorX, maxHeight-1)

maxHeight--

// Header line
if len(f.opt.header) > 0 {
w = 0
for _, r := range []rune(runewidth.Truncate(f.opt.header, maxWidth-2, "..")) {
style := tcell.StyleDefault.
Foreground(tcell.ColorGreen).
Background(tcell.ColorDefault)
f.term.SetContent(2+w, maxHeight-1, r, nil, style)
w += runewidth.RuneWidth(r)
}
maxHeight--
}

// Number line
for i, r := range fmt.Sprintf("%d/%d", len(f.state.matched), len(f.state.items)) {
style := tcell.StyleDefault.
Foreground(tcell.ColorYellow).
Background(tcell.ColorDefault)

f.term.SetContent(2+i, height-2, r, nil, style)
f.term.SetContent(2+i, maxHeight-1, r, nil, style)
}
maxHeight--

// Item lines
itemAreaHeight := height - 2 - 1
itemAreaHeight := maxHeight - 1
matched := f.state.matched
offset := f.state.cursorY
y := f.state.y
Expand All @@ -175,8 +193,8 @@ func (f *finder) _draw() {
Foreground(tcell.ColorRed).
Background(tcell.ColorBlack)

f.term.SetContent(0, height-3-i, '>', nil, style)
f.term.SetContent(1, height-3-i, ' ', nil, style)
f.term.SetContent(0, maxHeight-1-i, '>', nil, style)
f.term.SetContent(1, maxHeight-1-i, ' ', nil, style)
}

if f.opt.multi {
Expand All @@ -185,7 +203,7 @@ func (f *finder) _draw() {
Foreground(tcell.ColorRed).
Background(tcell.ColorBlack)

f.term.SetContent(1, height-3-i, '>', nil, style)
f.term.SetContent(1, maxHeight-1-i, '>', nil, style)
}
}

Expand Down Expand Up @@ -226,11 +244,11 @@ func (f *finder) _draw() {
rw := runewidth.RuneWidth(r)
// Shorten item cells.
if w+rw+2 > maxWidth {
f.term.SetContent(w, height-3-i, '.', nil, style)
f.term.SetContent(w+1, height-3-i, '.', nil, style)
f.term.SetContent(w, maxHeight-1-i, '.', nil, style)
f.term.SetContent(w+1, maxHeight-1-i, '.', nil, style)
break
} else {
f.term.SetContent(w, height-3-i, r, nil, style)
f.term.SetContent(w, maxHeight-1-i, r, nil, style)
w += rw
}
}
Expand Down
8 changes: 8 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type opt struct {
multi bool
hotReload bool
promptString string
header string
}

type mode int
Expand Down Expand Up @@ -70,3 +71,10 @@ func withMulti() Option {
o.multi = true
}
}

// WithHeader enables to set the header.
func WithHeader(s string) Option {
return func(o *opt) {
o.header = s
}
}

0 comments on commit 1df34fd

Please sign in to comment.