Refactor tabpane

- Move from termui/extra/tabpane.go to termui/tabpane.go
- Renamed from Tabpane to TabPane
This commit is contained in:
Caleb Bassi 2018-11-29 09:41:29 -08:00
parent efe414873d
commit 4782da6ed6
2 changed files with 25 additions and 30 deletions

View File

@ -8,7 +8,6 @@ package main
import ( import (
ui "github.com/gizak/termui" ui "github.com/gizak/termui"
"github.com/gizak/termui/extra"
) )
func main() { func main() {
@ -24,7 +23,7 @@ func main() {
header.Border = false header.Border = false
header.TextBgColor = ui.ColorBlue header.TextBgColor = ui.ColorBlue
tab1 := extra.NewTab("pierwszy") tab1 := ui.NewTab("pierwszy")
p2 := ui.NewParagraph("Press q to quit\nPress h or l to switch tabs\n") p2 := ui.NewParagraph("Press q to quit\nPress h or l to switch tabs\n")
p2.Height = 5 p2.Height = 5
p2.Width = 37 p2.Width = 37
@ -33,7 +32,7 @@ func main() {
p2.BorderFg = ui.ColorYellow p2.BorderFg = ui.ColorYellow
tab1.AddBlocks(p2) tab1.AddBlocks(p2)
tab2 := extra.NewTab("drugi") tab2 := ui.NewTab("drugi")
bc := ui.NewBarChart() bc := ui.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} 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"} bclabels := []string{"S0", "S1", "S2", "S3", "S4", "S5"}
@ -47,12 +46,12 @@ func main() {
bc.NumColor = ui.ColorYellow bc.NumColor = ui.ColorYellow
tab2.AddBlocks(bc) tab2.AddBlocks(bc)
tab3 := extra.NewTab("trzeci") tab3 := ui.NewTab("trzeci")
tab4 := extra.NewTab("żółw") tab4 := ui.NewTab("żółw")
tab5 := extra.NewTab("four") tab5 := ui.NewTab("four")
tab6 := extra.NewTab("five") tab6 := ui.NewTab("five")
tabpane := extra.NewTabpane() tabpane := ui.NewTabPane()
tabpane.Y = 1 tabpane.Y = 1
tabpane.Width = 30 tabpane.Width = 30
tabpane.Border = true tabpane.Border = true

View File

@ -2,13 +2,9 @@
// Use of this source code is governed by a MIT license that can // Use of this source code is governed by a MIT license that can
// be found in the LICENSE file. // be found in the LICENSE file.
package extra package termui
import ( import "unicode/utf8"
"unicode/utf8"
. "github.com/gizak/termui"
)
type Tab struct { type Tab struct {
Label string Label string
@ -37,7 +33,7 @@ func (tab *Tab) Buffer() Buffer {
return buf return buf
} }
type Tabpane struct { type TabPane struct {
Block Block
Tabs []Tab Tabs []Tab
activeTabIndex int activeTabIndex int
@ -46,8 +42,8 @@ type Tabpane struct {
offTabText int offTabText int
} }
func NewTabpane() *Tabpane { func NewTabPane() *TabPane {
tp := Tabpane{ tp := TabPane{
Block: *NewBlock(), Block: *NewBlock(),
activeTabIndex: 0, activeTabIndex: 0,
offTabText: 0, offTabText: 0,
@ -55,7 +51,7 @@ func NewTabpane() *Tabpane {
return &tp return &tp
} }
func (tp *Tabpane) SetTabs(tabs ...Tab) { func (tp *TabPane) SetTabs(tabs ...Tab) {
tp.Tabs = make([]Tab, len(tabs)) tp.Tabs = make([]Tab, len(tabs))
tp.posTabText = make([]int, len(tabs)+1) tp.posTabText = make([]int, len(tabs)+1)
off := 0 off := 0
@ -67,7 +63,7 @@ func (tp *Tabpane) SetTabs(tabs ...Tab) {
tp.posTabText[len(tabs)] = off - 1 //total length of Tab's text tp.posTabText[len(tabs)] = off - 1 //total length of Tab's text
} }
func (tp *Tabpane) SetActiveLeft() { func (tp *TabPane) SetActiveLeft() {
if tp.activeTabIndex == 0 { if tp.activeTabIndex == 0 {
return return
} }
@ -77,7 +73,7 @@ func (tp *Tabpane) SetActiveLeft() {
} }
} }
func (tp *Tabpane) SetActiveRight() { func (tp *TabPane) SetActiveRight() {
if tp.activeTabIndex == len(tp.Tabs)-1 { if tp.activeTabIndex == len(tp.Tabs)-1 {
return return
} }
@ -93,7 +89,7 @@ func (tp *Tabpane) SetActiveRight() {
// if only right tabs are not visible return 1 // if only right tabs are not visible return 1
// if both return 0 // if both return 0
// use only if fitsWidth() returns false // use only if fitsWidth() returns false
func (tp *Tabpane) checkAlignment() int { func (tp *TabPane) checkAlignment() int {
ret := 0 ret := 0
if tp.offTabText > 0 { if tp.offTabText > 0 {
ret = -1 ret = -1
@ -104,12 +100,12 @@ func (tp *Tabpane) checkAlignment() int {
return ret return ret
} }
// Checks if all tabs fits innerWidth of Tabpane // Checks if all tabs fits innerWidth of TabPane
func (tp *Tabpane) fitsWidth() bool { 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() { func (tp *TabPane) align() {
if !tp.fitsWidth() && !tp.Border { if !tp.fitsWidth() && !tp.Border {
tp.PaddingLeft += 1 tp.PaddingLeft += 1
tp.PaddingRight += 1 tp.PaddingRight += 1
@ -135,10 +131,10 @@ func buf2pt(b Buffer) []point {
return ps return ps
} }
// Adds the point only if it is visible in Tabpane. // Adds the point only if it is visible in TabPane.
// Point can be invisible if concatenation of Tab's texts is widther then // Point can be invisible if concatenation of Tab's texts is widther then
// innerWidth of Tabpane // innerWidth of TabPane
func (tp *Tabpane) addPoint(ptab []point, charOffset *int, oftX *int, points ...point) []point { 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++ *charOffset++
return ptab return ptab
@ -153,7 +149,7 @@ func (tp *Tabpane) addPoint(ptab []point, charOffset *int, oftX *int, points ...
} }
// Draws the point and redraws upper and lower border points (if it has one) // Draws the point and redraws upper and lower border points (if it has one)
func (tp *Tabpane) drawPointWithBorder(p point, ch rune, chbord rune, chdown rune, chup rune) []point { func (tp *TabPane) drawPointWithBorder(p point, ch rune, chbord rune, chdown rune, chup rune) []point {
var addp []point var addp []point
p.Ch = ch p.Ch = ch
if tp.Border { if tp.Border {
@ -169,7 +165,7 @@ func (tp *Tabpane) drawPointWithBorder(p point, ch rune, chbord rune, chdown run
return append(addp, p) return append(addp, p)
} }
func (tp *Tabpane) Buffer() Buffer { func (tp *TabPane) Buffer() Buffer {
if tp.Border { if tp.Border {
tp.Height = 3 tp.Height = 3
} else { } else {
@ -247,7 +243,7 @@ func (tp *Tabpane) Buffer() Buffer {
} }
} }
//draw tab content below the Tabpane //draw tab content below the TabPane
if i == tp.activeTabIndex { if i == tp.activeTabIndex {
blockPoints := buf2pt(tab.Buffer()) blockPoints := buf2pt(tab.Buffer())
for i := 0; i < len(blockPoints); i++ { for i := 0; i < len(blockPoints); i++ {