Fix for GetFormItemByLabel Issue

pull/99/head
Joe Grasse 7 years ago
parent b4fd66d458
commit ed3c693caf

@ -1,6 +1,8 @@
package tview package tview
import ( import (
"strings"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
) )
@ -17,6 +19,9 @@ type Checkbox struct {
// The text to be displayed before the input area. // The text to be displayed before the input area.
label string label string
// The padding to using on the label.
labelPadding int
// The label color. // The label color.
labelColor tcell.Color labelColor tcell.Color
@ -74,6 +79,12 @@ func (c *Checkbox) SetLabelColor(color tcell.Color) *Checkbox {
return c return c
} }
func (c *Checkbox) setLabelPadding(padding int) FormItem {
c.labelPadding = padding
return c
}
// SetFieldBackgroundColor sets the background color of the input area. // SetFieldBackgroundColor sets the background color of the input area.
func (c *Checkbox) SetFieldBackgroundColor(color tcell.Color) *Checkbox { func (c *Checkbox) SetFieldBackgroundColor(color tcell.Color) *Checkbox {
c.fieldBackgroundColor = color c.fieldBackgroundColor = color
@ -137,8 +148,11 @@ func (c *Checkbox) Draw(screen tcell.Screen) {
return return
} }
// Get label with padding
label := c.label + strings.Repeat(" ", c.labelPadding)
// Draw label. // Draw label.
_, drawnWidth := Print(screen, c.label, x, y, rightLimit-x, AlignLeft, c.labelColor) _, drawnWidth := Print(screen, label, x, y, rightLimit-x, AlignLeft, c.labelColor)
x += drawnWidth x += drawnWidth
// Draw checkbox. // Draw checkbox.

@ -39,6 +39,9 @@ type DropDown struct {
// The text to be displayed before the input area. // The text to be displayed before the input area.
label string label string
// The padding to using on the label.
labelPadding int
// The label color. // The label color.
labelColor tcell.Color labelColor tcell.Color
@ -119,6 +122,12 @@ func (d *DropDown) SetLabelColor(color tcell.Color) *DropDown {
return d return d
} }
func (d *DropDown) setLabelPadding(padding int) FormItem {
d.labelPadding = padding
return d
}
// SetFieldBackgroundColor sets the background color of the options area. // SetFieldBackgroundColor sets the background color of the options area.
func (d *DropDown) SetFieldBackgroundColor(color tcell.Color) *DropDown { func (d *DropDown) SetFieldBackgroundColor(color tcell.Color) *DropDown {
d.fieldBackgroundColor = color d.fieldBackgroundColor = color
@ -226,8 +235,11 @@ func (d *DropDown) Draw(screen tcell.Screen) {
return return
} }
// Get label with padding
label := d.label + strings.Repeat(" ", d.labelPadding)
// Draw label. // Draw label.
_, drawnWidth := Print(screen, d.label, x, y, rightLimit-x, AlignLeft, d.labelColor) _, drawnWidth := Print(screen, label, x, y, rightLimit-x, AlignLeft, d.labelColor)
x += drawnWidth x += drawnWidth
// What's the longest option text? // What's the longest option text?

@ -33,6 +33,8 @@ type FormItem interface {
// Enter key (we're done), the Escape key (cancel input), the Tab key (move to // Enter key (we're done), the Escape key (cancel input), the Tab key (move to
// next field), and the Backtab key (move to previous field). // next field), and the Backtab key (move to previous field).
SetFinishedFunc(handler func(key tcell.Key)) FormItem SetFinishedFunc(handler func(key tcell.Key)) FormItem
setLabelPadding(padding int) FormItem
} }
// Form allows you to combine multiple one-line form elements into a vertical // Form allows you to combine multiple one-line form elements into a vertical
@ -290,6 +292,7 @@ func (f *Form) Draw(screen tcell.Screen) {
// Calculate positions of form items. // Calculate positions of form items.
positions := make([]struct{ x, y, width, height int }, len(f.items)+len(f.buttons)) positions := make([]struct{ x, y, width, height int }, len(f.items)+len(f.buttons))
var focusedPosition struct{ x, y, width, height int } var focusedPosition struct{ x, y, width, height int }
var labelPadding int
for index, item := range f.items { for index, item := range f.items {
// Calculate the space needed. // Calculate the space needed.
label := strings.TrimSpace(item.GetLabel()) label := strings.TrimSpace(item.GetLabel())
@ -300,12 +303,12 @@ func (f *Form) Draw(screen tcell.Screen) {
if fieldWidth == 0 { if fieldWidth == 0 {
fieldWidth = DefaultFormFieldWidth fieldWidth = DefaultFormFieldWidth
} }
label += " " labelPadding = 1
labelWidth++ labelWidth++
itemWidth = labelWidth + fieldWidth itemWidth = labelWidth + fieldWidth
} else { } else {
// We want all fields to align vertically. // We want all fields to align vertically.
label += strings.Repeat(" ", maxLabelWidth-labelWidth) labelPadding = maxLabelWidth - labelWidth
itemWidth = width itemWidth = width
} }
@ -325,7 +328,7 @@ func (f *Form) Draw(screen tcell.Screen) {
f.backgroundColor, f.backgroundColor,
f.fieldTextColor, f.fieldTextColor,
f.fieldBackgroundColor, f.fieldBackgroundColor,
) ).setLabelPadding(labelPadding)
// Save position. // Save position.
positions[index].x = x positions[index].x = x

@ -26,6 +26,9 @@ type InputField struct {
// The text to be displayed before the input area. // The text to be displayed before the input area.
label string label string
// The padding to using on the label.
labelPadding int
// The text to be displayed in the input area when "text" is empty. // The text to be displayed in the input area when "text" is empty.
placeholder string placeholder string
@ -109,6 +112,12 @@ func (i *InputField) SetLabelColor(color tcell.Color) *InputField {
return i return i
} }
func (i *InputField) setLabelPadding(padding int) FormItem {
i.labelPadding = padding
return i
}
// SetFieldBackgroundColor sets the background color of the input area. // SetFieldBackgroundColor sets the background color of the input area.
func (i *InputField) SetFieldBackgroundColor(color tcell.Color) *InputField { func (i *InputField) SetFieldBackgroundColor(color tcell.Color) *InputField {
i.fieldBackgroundColor = color i.fieldBackgroundColor = color
@ -202,8 +211,11 @@ func (i *InputField) Draw(screen tcell.Screen) {
return return
} }
// Get label with padding
label := i.label + strings.Repeat(" ", i.labelPadding)
// Draw label. // Draw label.
_, drawnWidth := Print(screen, i.label, x, y, rightLimit-x, AlignLeft, i.labelColor) _, drawnWidth := Print(screen, label, x, y, rightLimit-x, AlignLeft, i.labelColor)
x += drawnWidth x += drawnWidth
// Draw input area. // Draw input area.
@ -280,7 +292,7 @@ func (i *InputField) setCursor(screen tcell.Screen) {
if i.fieldWidth > 0 && fieldWidth > i.fieldWidth-1 { if i.fieldWidth > 0 && fieldWidth > i.fieldWidth-1 {
fieldWidth = i.fieldWidth - 1 fieldWidth = i.fieldWidth - 1
} }
x += StringWidth(i.label) + fieldWidth x += StringWidth(i.label) + fieldWidth + i.labelPadding
if x >= rightLimit { if x >= rightLimit {
x = rightLimit - 1 x = rightLimit - 1
} }

Loading…
Cancel
Save