Skip to content

Commit

Permalink
support tabs on views
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Jun 9, 2019
1 parent efab7b0 commit 619845f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
37 changes: 34 additions & 3 deletions gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package gocui

import (
standardErrors "errors"
"strings"
"time"

"github.com/go-errors/errors"
Expand Down Expand Up @@ -501,7 +502,7 @@ func (g *Gui) flush() error {
if err := g.drawFrameCorners(v, fgColor, bgColor); err != nil {
return err
}
if v.Title != "" {
if v.Title != "" || len(v.Tabs) > 0 {
if err := g.drawTitle(v, fgColor, bgColor); err != nil {
return err
}
Expand Down Expand Up @@ -615,17 +616,47 @@ func (g *Gui) drawTitle(v *View, fgColor, bgColor Attribute) error {
return nil
}

for i, ch := range v.Title {
tabs := v.Tabs
separator := " - "
charIndex := 0
currentTabStart := -1
currentTabEnd := -1
if len(tabs) == 0 {
tabs = []string{v.Title}
} else {
for i, tab := range tabs {
if i == v.TabIndex {
currentTabStart = charIndex
currentTabEnd = charIndex + len(tab)
break
}
charIndex += len(tab)
if i < len(tabs)-1 {
charIndex += len(separator)
}
}
}

str := strings.Join(tabs, separator)

for i, ch := range str {
x := v.x0 + i + 2
if x < 0 {
continue
} else if x > v.x1-2 || x >= g.maxX {
break
}
if err := g.SetRune(x, v.y0, ch, fgColor, bgColor); err != nil {
currentFgColor := fgColor
currentBgColor := bgColor
if i >= currentTabStart && i <= currentTabEnd {
currentFgColor = v.SelFgColor - AttrBold
currentBgColor = v.SelBgColor
}
if err := g.SetRune(x, v.y0, ch, currentFgColor, currentBgColor); err != nil {
return err
}
}

return nil
}

Expand Down
3 changes: 3 additions & 0 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ type View struct {
// If Frame is true, Title allows to configure a title for the view.
Title string

Tabs []string
TabIndex int

// If Frame is true, Subtitle allows to configure a subtitle for the view.
Subtitle string

Expand Down

0 comments on commit 619845f

Please sign in to comment.