Browse Source

For Form.GetFormItemByLabel() to work, we must not modify the label text. Fixes #94, fixes #99

merge-requests/1/merge
Oliver 4 years ago
parent
commit
5bd22542e5
  1. 28
      checkbox.go
  2. 28
      dropdown.go
  3. 15
      form.go
  4. 2
      grid.go
  5. 38
      inputfield.go

28
checkbox.go

@ -17,6 +17,10 @@ type Checkbox struct { @@ -17,6 +17,10 @@ type Checkbox struct {
// The text to be displayed before the input area.
label string
// The screen width of the label area. A value of 0 means use the width of
// the label text.
labelWidth int
// The label color.
labelColor tcell.Color
@ -68,6 +72,13 @@ func (c *Checkbox) GetLabel() string { @@ -68,6 +72,13 @@ func (c *Checkbox) GetLabel() string {
return c.label
}
// SetLabelWidth sets the screen width of the label. A value of 0 will cause the
// primitive to use the width of the label string.
func (c *Checkbox) SetLabelWidth(width int) *Checkbox {
c.labelWidth = width
return c
}
// SetLabelColor sets the color of the label.
func (c *Checkbox) SetLabelColor(color tcell.Color) *Checkbox {
c.labelColor = color
@ -87,8 +98,8 @@ func (c *Checkbox) SetFieldTextColor(color tcell.Color) *Checkbox { @@ -87,8 +98,8 @@ func (c *Checkbox) SetFieldTextColor(color tcell.Color) *Checkbox {
}
// SetFormAttributes sets attributes shared by all form items.
func (c *Checkbox) SetFormAttributes(label string, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
c.label = label
func (c *Checkbox) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
c.labelWidth = labelWidth
c.labelColor = labelColor
c.backgroundColor = bgColor
c.fieldTextColor = fieldTextColor
@ -138,8 +149,17 @@ func (c *Checkbox) Draw(screen tcell.Screen) { @@ -138,8 +149,17 @@ func (c *Checkbox) Draw(screen tcell.Screen) {
}
// Draw label.
_, drawnWidth := Print(screen, c.label, x, y, rightLimit-x, AlignLeft, c.labelColor)
x += drawnWidth
if c.labelWidth > 0 {
labelWidth := c.labelWidth
if labelWidth > rightLimit-x {
labelWidth = rightLimit - x
}
Print(screen, c.label, x, y, labelWidth, AlignLeft, c.labelColor)
x += labelWidth
} else {
_, drawnWidth := Print(screen, c.label, x, y, rightLimit-x, AlignLeft, c.labelColor)
x += drawnWidth
}
// Draw checkbox.
fieldStyle := tcell.StyleDefault.Background(c.fieldBackgroundColor).Foreground(c.fieldTextColor)

28
dropdown.go

@ -51,6 +51,10 @@ type DropDown struct { @@ -51,6 +51,10 @@ type DropDown struct {
// The color for prefixes.
prefixTextColor tcell.Color
// The screen width of the label area. A value of 0 means use the width of
// the label text.
labelWidth int
// The screen width of the input area. A value of 0 means extend as much as
// possible.
fieldWidth int
@ -113,6 +117,13 @@ func (d *DropDown) GetLabel() string { @@ -113,6 +117,13 @@ func (d *DropDown) GetLabel() string {
return d.label
}
// SetLabelWidth sets the screen width of the label. A value of 0 will cause the
// primitive to use the width of the label string.
func (d *DropDown) SetLabelWidth(width int) *DropDown {
d.labelWidth = width
return d
}
// SetLabelColor sets the color of the label.
func (d *DropDown) SetLabelColor(color tcell.Color) *DropDown {
d.labelColor = color
@ -140,8 +151,8 @@ func (d *DropDown) SetPrefixTextColor(color tcell.Color) *DropDown { @@ -140,8 +151,8 @@ func (d *DropDown) SetPrefixTextColor(color tcell.Color) *DropDown {
}
// SetFormAttributes sets attributes shared by all form items.
func (d *DropDown) SetFormAttributes(label string, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
d.label = label
func (d *DropDown) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
d.labelWidth = labelWidth
d.labelColor = labelColor
d.backgroundColor = bgColor
d.fieldTextColor = fieldTextColor
@ -227,8 +238,17 @@ func (d *DropDown) Draw(screen tcell.Screen) { @@ -227,8 +238,17 @@ func (d *DropDown) Draw(screen tcell.Screen) {
}
// Draw label.
_, drawnWidth := Print(screen, d.label, x, y, rightLimit-x, AlignLeft, d.labelColor)
x += drawnWidth
if d.labelWidth > 0 {
labelWidth := d.labelWidth
if labelWidth > rightLimit-x {
labelWidth = rightLimit - x
}
Print(screen, d.label, x, y, labelWidth, AlignLeft, d.labelColor)
x += labelWidth
} else {
_, drawnWidth := Print(screen, d.label, x, y, rightLimit-x, AlignLeft, d.labelColor)
x += drawnWidth
}
// What's the longest option text?
maxWidth := 0

15
form.go

@ -1,8 +1,6 @@ @@ -1,8 +1,6 @@
package tview
import (
"strings"
"github.com/gdamore/tcell"
)
@ -20,7 +18,7 @@ type FormItem interface { @@ -20,7 +18,7 @@ type FormItem interface {
GetLabel() string
// SetFormAttributes sets a number of item attributes at once.
SetFormAttributes(label string, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem
SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem
// GetFieldWidth returns the width of the form item's field (the area which
// is manipulated by the user) in number of screen cells. A value of 0
@ -279,8 +277,7 @@ func (f *Form) Draw(screen tcell.Screen) { @@ -279,8 +277,7 @@ func (f *Form) Draw(screen tcell.Screen) {
// Find the longest label.
var maxLabelWidth int
for _, item := range f.items {
label := strings.TrimSpace(item.GetLabel())
labelWidth := StringWidth(label)
labelWidth := StringWidth(item.GetLabel())
if labelWidth > maxLabelWidth {
maxLabelWidth = labelWidth
}
@ -292,20 +289,18 @@ func (f *Form) Draw(screen tcell.Screen) { @@ -292,20 +289,18 @@ func (f *Form) Draw(screen tcell.Screen) {
var focusedPosition struct{ x, y, width, height int }
for index, item := range f.items {
// Calculate the space needed.
label := strings.TrimSpace(item.GetLabel())
labelWidth := StringWidth(label)
labelWidth := StringWidth(item.GetLabel())
var itemWidth int
if f.horizontal {
fieldWidth := item.GetFieldWidth()
if fieldWidth == 0 {
fieldWidth = DefaultFormFieldWidth
}
label += " "
labelWidth++
itemWidth = labelWidth + fieldWidth
} else {
// We want all fields to align vertically.
label += strings.Repeat(" ", maxLabelWidth-labelWidth)
labelWidth = maxLabelWidth
itemWidth = width
}
@ -320,7 +315,7 @@ func (f *Form) Draw(screen tcell.Screen) { @@ -320,7 +315,7 @@ func (f *Form) Draw(screen tcell.Screen) {
itemWidth = rightLimit - x
}
item.SetFormAttributes(
label,
labelWidth,
f.labelColor,
f.backgroundColor,
f.fieldTextColor,

2
grid.go

@ -258,7 +258,7 @@ func (g *Grid) HasFocus() bool { @@ -258,7 +258,7 @@ func (g *Grid) HasFocus() bool {
return true
}
}
return false
return g.hasFocus
}
// InputHandler returns the handler for this primitive.

38
inputfield.go

@ -41,6 +41,10 @@ type InputField struct { @@ -41,6 +41,10 @@ type InputField struct {
// The text color of the placeholder.
placeholderTextColor tcell.Color
// The screen width of the label area. A value of 0 means use the width of
// the label text.
labelWidth int
// The screen width of the input area. A value of 0 means extend as much as
// possible.
fieldWidth int
@ -97,6 +101,13 @@ func (i *InputField) GetLabel() string { @@ -97,6 +101,13 @@ func (i *InputField) GetLabel() string {
return i.label
}
// SetLabelWidth sets the screen width of the label. A value of 0 will cause the
// primitive to use the width of the label string.
func (i *InputField) SetLabelWidth(width int) *InputField {
i.labelWidth = width
return i
}
// SetPlaceholder sets the text to be displayed when the input text is empty.
func (i *InputField) SetPlaceholder(text string) *InputField {
i.placeholder = text
@ -121,15 +132,15 @@ func (i *InputField) SetFieldTextColor(color tcell.Color) *InputField { @@ -121,15 +132,15 @@ func (i *InputField) SetFieldTextColor(color tcell.Color) *InputField {
return i
}
// SetPlaceholderExtColor sets the text color of placeholder text.
func (i *InputField) SetPlaceholderExtColor(color tcell.Color) *InputField {
// SetPlaceholderTextColor sets the text color of placeholder text.
func (i *InputField) SetPlaceholderTextColor(color tcell.Color) *InputField {
i.placeholderTextColor = color
return i
}
// SetFormAttributes sets attributes shared by all form items.
func (i *InputField) SetFormAttributes(label string, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
i.label = label
func (i *InputField) SetFormAttributes(labelWidth int, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
i.labelWidth = labelWidth
i.labelColor = labelColor
i.backgroundColor = bgColor
i.fieldTextColor = fieldTextColor
@ -203,8 +214,17 @@ func (i *InputField) Draw(screen tcell.Screen) { @@ -203,8 +214,17 @@ func (i *InputField) Draw(screen tcell.Screen) {
}
// Draw label.
_, drawnWidth := Print(screen, i.label, x, y, rightLimit-x, AlignLeft, i.labelColor)
x += drawnWidth
if i.labelWidth > 0 {
labelWidth := i.labelWidth
if labelWidth > rightLimit-x {
labelWidth = rightLimit - x
}
Print(screen, i.label, x, y, labelWidth, AlignLeft, i.labelColor)
x += labelWidth
} else {
_, drawnWidth := Print(screen, i.label, x, y, rightLimit-x, AlignLeft, i.labelColor)
x += drawnWidth
}
// Draw input area.
fieldWidth := i.fieldWidth
@ -280,7 +300,11 @@ func (i *InputField) setCursor(screen tcell.Screen) { @@ -280,7 +300,11 @@ func (i *InputField) setCursor(screen tcell.Screen) {
if i.fieldWidth > 0 && fieldWidth > i.fieldWidth-1 {
fieldWidth = i.fieldWidth - 1
}
x += StringWidth(i.label) + fieldWidth
if i.labelWidth > 0 {
x += i.labelWidth + fieldWidth
} else {
x += StringWidth(i.label) + fieldWidth
}
if x >= rightLimit {
x = rightLimit - 1
}

Loading…
Cancel
Save