add ContainsList view field to show current item index and total count in frame

This commit is contained in:
Jesse Duffield 2020-02-25 07:16:55 +11:00
parent 3e1f0f7c3e
commit 5024a02682
2 changed files with 37 additions and 0 deletions

34
gui.go
View File

@ -6,6 +6,7 @@ package gocui
import (
standardErrors "errors"
"fmt"
"strings"
"sync"
"time"
@ -542,6 +543,11 @@ func (g *Gui) flush() error {
return err
}
}
if v.ContainsList {
if err := g.drawListFooter(v, fgColor, bgColor); err != nil {
return err
}
}
}
if err := g.draw(v); err != nil {
return err
@ -729,6 +735,34 @@ func (g *Gui) drawSubtitle(v *View, fgColor, bgColor Attribute) error {
return nil
}
// drawListFooter draws the footer of a list view, showing something like '1 of 10'
func (g *Gui) drawListFooter(v *View, fgColor, bgColor Attribute) error {
if len(v.lines) == 0 {
return nil
}
message := fmt.Sprintf("%d of %d", v.cy+v.oy+1, len(v.lines))
if v.y1 < 0 || v.y1 >= g.maxY {
return nil
}
start := v.x1 - 1 - len(message)
if start < v.x0 {
return nil
}
for i, ch := range message {
x := start + i
if x >= v.x1 {
break
}
if err := g.SetRune(x, v.y1, ch, fgColor, bgColor); err != nil {
return err
}
}
return nil
}
// draw manages the cursor and calls the draw function of a view.
func (g *Gui) draw(v *View) error {
if g.Cursor {

View File

@ -107,6 +107,9 @@ type View struct {
Context string // this is for assigning keybindings to a view only in certain contexts
searcher *searcher
// when ContainsList is true, we show the current index and total count in the view
ContainsList bool
}
type searcher struct {