Move Tab to extra
This commit is contained in:
parent
6246586bbd
commit
c30bf1612a
20
block.go
20
block.go
@ -140,3 +140,23 @@ func (d *Block) chopOverflow(ps []Point) []Point {
|
||||
}
|
||||
return nps
|
||||
}
|
||||
|
||||
func (b Block) InnerWidth() int {
|
||||
return b.innerWidth
|
||||
}
|
||||
|
||||
func (b Block) InnerHeight() int {
|
||||
return b.innerHeight
|
||||
}
|
||||
|
||||
func (b Block) InnerX() int {
|
||||
return b.innerX
|
||||
}
|
||||
|
||||
func (b Block) InnerY() int {
|
||||
return b.innerY
|
||||
}
|
||||
|
||||
func (b *Block) Align() {
|
||||
b.align()
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/marigs/termui"
|
||||
"github.com/gizak/termui"
|
||||
"github.com/gizak/termui/extra"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -19,7 +20,7 @@ func main() {
|
||||
header.HasBorder = false
|
||||
header.TextBgColor = termui.ColorBlue
|
||||
|
||||
tab1 := termui.NewTab("pierwszy")
|
||||
tab1 := extra.NewTab("pierwszy")
|
||||
par2 := termui.NewPar("Press q to quit\nPress j or k to switch tabs\n")
|
||||
par2.Height = 5
|
||||
par2.Width = 37
|
||||
@ -28,7 +29,7 @@ func main() {
|
||||
par2.Border.FgColor = termui.ColorYellow
|
||||
tab1.AddBlocks(par2)
|
||||
|
||||
tab2 := termui.NewTab("drugi")
|
||||
tab2 := extra.NewTab("drugi")
|
||||
bc := termui.NewBarChart()
|
||||
data := []int{3, 2, 5, 3, 9, 5, 3, 2, 5, 8, 3, 2, 4, 5, 3, 2, 5, 7, 5, 3, 2, 6, 7, 4, 6, 3, 6, 7, 8, 3, 6, 4, 5, 3, 2, 4, 6, 4, 8, 5, 9, 4, 3, 6, 5, 3, 6}
|
||||
bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"}
|
||||
@ -42,21 +43,22 @@ func main() {
|
||||
bc.NumColor = termui.ColorYellow
|
||||
tab2.AddBlocks(bc)
|
||||
|
||||
tab3 := termui.NewTab("trzeci")
|
||||
tab4 := termui.NewTab("żółw")
|
||||
tab5 := termui.NewTab("four")
|
||||
tab6 := termui.NewTab("five")
|
||||
tab3 := extra.NewTab("trzeci")
|
||||
tab4 := extra.NewTab("żółw")
|
||||
tab5 := extra.NewTab("four")
|
||||
tab6 := extra.NewTab("five")
|
||||
|
||||
tabpane := termui.NewTabpane()
|
||||
tabpane := extra.NewTabpane()
|
||||
tabpane.Y = 1
|
||||
tabpane.Width = 30
|
||||
tabpane.HasBorder = true
|
||||
|
||||
tabpane.SetTabs(*tab1, *tab2, *tab3, *tab4, *tab5, *tab6)
|
||||
|
||||
evt := termui.EventCh()
|
||||
|
||||
termui.Render(header, tabpane)
|
||||
|
||||
evt := termui.EventCh()
|
||||
for {
|
||||
select {
|
||||
case e := <-evt:
|
||||
|
@ -1,8 +1,9 @@
|
||||
|
||||
package termui
|
||||
package extra
|
||||
|
||||
import (
|
||||
"unicode/utf8"
|
||||
|
||||
. "github.com/gizak/termui"
|
||||
)
|
||||
|
||||
type Tab struct {
|
||||
@ -37,6 +38,7 @@ type Tabpane struct {
|
||||
Block
|
||||
Tabs []Tab
|
||||
activeTabIndex int
|
||||
ActiveTabBg Attribute
|
||||
posTabText []int
|
||||
offTabText int
|
||||
}
|
||||
@ -45,7 +47,8 @@ func NewTabpane() *Tabpane {
|
||||
tp := Tabpane{
|
||||
Block: *NewBlock(),
|
||||
activeTabIndex: 0,
|
||||
offTabText: 0}
|
||||
offTabText: 0,
|
||||
ActiveTabBg: Theme().TabActiveBg}
|
||||
return &tp
|
||||
}
|
||||
|
||||
@ -77,8 +80,8 @@ func (tp *Tabpane) SetActiveRight() {
|
||||
}
|
||||
tp.activeTabIndex += 1
|
||||
endOffset := tp.posTabText[tp.activeTabIndex] + tp.Tabs[tp.activeTabIndex].RuneLen
|
||||
if endOffset + tp.offTabText > tp.innerWidth {
|
||||
tp.offTabText = endOffset - tp.innerWidth
|
||||
if endOffset+tp.offTabText > tp.InnerWidth() {
|
||||
tp.offTabText = endOffset - tp.InnerWidth()
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,7 +95,7 @@ func (tp *Tabpane) checkAlignment() int {
|
||||
if tp.offTabText > 0 {
|
||||
ret = -1
|
||||
}
|
||||
if tp.offTabText + tp.innerWidth < tp.posTabText[len(tp.Tabs)] {
|
||||
if tp.offTabText+tp.InnerWidth() < tp.posTabText[len(tp.Tabs)] {
|
||||
ret += 1
|
||||
}
|
||||
return ret
|
||||
@ -100,13 +103,14 @@ func (tp *Tabpane) checkAlignment() int {
|
||||
|
||||
// Checks if all tabs fits innerWidth of Tabpane
|
||||
func (tp *Tabpane) fitsWidth() bool {
|
||||
return tp.innerWidth >= tp.posTabText[len(tp.Tabs)]
|
||||
return tp.InnerWidth() >= tp.posTabText[len(tp.Tabs)]
|
||||
}
|
||||
|
||||
func (tp *Tabpane) align() {
|
||||
if !tp.fitsWidth() && !tp.HasBorder {
|
||||
tp.innerWidth -= 2
|
||||
tp.innerX += 1
|
||||
tp.PaddingLeft += 1
|
||||
tp.PaddingRight += 1
|
||||
tp.Block.Align()
|
||||
}
|
||||
}
|
||||
|
||||
@ -114,11 +118,11 @@ func (tp *Tabpane) align() {
|
||||
// Point can be invisible if concatenation of Tab's texts is widther then
|
||||
// innerWidth of Tabpane
|
||||
func (tp *Tabpane) addPoint(ptab []Point, charOffset *int, oftX *int, points ...Point) []Point {
|
||||
if *charOffset < tp.offTabText || tp.offTabText + tp.innerWidth < *charOffset {
|
||||
if *charOffset < tp.offTabText || tp.offTabText+tp.InnerWidth() < *charOffset {
|
||||
*charOffset++
|
||||
return ptab
|
||||
}
|
||||
for _, p := range(points) {
|
||||
for _, p := range points {
|
||||
p.X = *oftX
|
||||
ptab = append(ptab, p)
|
||||
}
|
||||
@ -133,14 +137,14 @@ func (tp *Tabpane) drawPointWithBorder(p Point, ch rune, chbord rune, chdown run
|
||||
p.Ch = ch
|
||||
if tp.HasBorder {
|
||||
p.Ch = chdown
|
||||
p.Y = tp.innerY-1
|
||||
p.Y = tp.InnerY() - 1
|
||||
addp = append(addp, p)
|
||||
p.Ch = chup
|
||||
p.Y = tp.innerY+1
|
||||
p.Y = tp.InnerY() + 1
|
||||
addp = append(addp, p)
|
||||
p.Ch = chbord
|
||||
}
|
||||
p.Y = tp.innerY
|
||||
p.Y = tp.InnerY()
|
||||
return append(addp, p)
|
||||
}
|
||||
|
||||
@ -155,37 +159,37 @@ func (tp *Tabpane) Buffer() []Point {
|
||||
}
|
||||
ps := tp.Block.Buffer()
|
||||
tp.align()
|
||||
if tp.innerHeight <= 0 || tp.innerWidth <= 0 {
|
||||
if tp.InnerHeight() <= 0 || tp.InnerWidth() <= 0 {
|
||||
return nil
|
||||
}
|
||||
oftX := tp.innerX
|
||||
oftX := tp.InnerX()
|
||||
charOffset := 0
|
||||
pt := Point{Bg: tp.Border.BgColor, Fg: tp.Border.FgColor}
|
||||
for i, tab := range tp.Tabs {
|
||||
|
||||
if i != 0 {
|
||||
pt.X = oftX
|
||||
pt.Y = tp.innerY
|
||||
pt.Y = tp.InnerY()
|
||||
addp := tp.drawPointWithBorder(pt, ' ', VERTICAL_LINE, HORIZONTAL_DOWN, HORIZONTAL_UP)
|
||||
ps = tp.addPoint(ps, &charOffset, &oftX, addp...)
|
||||
}
|
||||
|
||||
if i == tp.activeTabIndex {
|
||||
pt.Bg = theme.TabActiveBg
|
||||
pt.Bg = tp.ActiveTabBg
|
||||
}
|
||||
rs := str2runes(tab.Label)
|
||||
rs := []rune(tab.Label)
|
||||
for k := 0; k < len(rs); k++ {
|
||||
|
||||
addp := make([]Point, 0, 2)
|
||||
if i == tp.activeTabIndex && tp.HasBorder {
|
||||
pt.Ch = ' '
|
||||
pt.Y = tp.innerY + 1
|
||||
pt.Y = tp.InnerY() + 1
|
||||
pt.Bg = tp.Border.BgColor
|
||||
addp = append(addp, pt)
|
||||
pt.Bg = theme.TabActiveBg
|
||||
pt.Bg = tp.ActiveTabBg
|
||||
}
|
||||
|
||||
pt.Y = tp.innerY
|
||||
pt.Y = tp.InnerY()
|
||||
pt.Ch = rs[k]
|
||||
|
||||
addp = append(addp, pt)
|
||||
@ -195,7 +199,7 @@ func (tp *Tabpane) Buffer() []Point {
|
||||
|
||||
if !tp.fitsWidth() {
|
||||
all := tp.checkAlignment()
|
||||
pt.X = tp.innerX-1
|
||||
pt.X = tp.InnerX() - 1
|
||||
|
||||
pt.Ch = '*'
|
||||
if tp.HasBorder {
|
||||
@ -208,7 +212,7 @@ func (tp *Tabpane) Buffer() []Point {
|
||||
ps = append(ps, addp...)
|
||||
}
|
||||
|
||||
pt.X = tp.innerX + tp.innerWidth
|
||||
pt.X = tp.InnerX() + tp.InnerWidth()
|
||||
pt.Ch = '*'
|
||||
if tp.HasBorder {
|
||||
pt.Ch = VERTICAL_LINE
|
||||
@ -232,4 +236,3 @@ func (tp *Tabpane) Buffer() []Point {
|
||||
|
||||
return ps
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user