1
0
mirror of https://github.com/rivo/tview.git synced 2025-04-28 13:48:53 +08:00

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

This commit is contained in:
Oliver 2018-04-09 21:07:03 +02:00
parent b4fd66d458
commit 5bd22542e5
5 changed files with 85 additions and 26 deletions

View File

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

View File

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

15
form.go
View File

@ -1,8 +1,6 @@
package tview package tview
import ( import (
"strings"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
) )
@ -20,7 +18,7 @@ type FormItem interface {
GetLabel() string GetLabel() string
// SetFormAttributes sets a number of item attributes at once. // 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 // 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 // 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) {
// Find the longest label. // Find the longest label.
var maxLabelWidth int var maxLabelWidth int
for _, item := range f.items { for _, item := range f.items {
label := strings.TrimSpace(item.GetLabel()) labelWidth := StringWidth(item.GetLabel())
labelWidth := StringWidth(label)
if labelWidth > maxLabelWidth { if labelWidth > maxLabelWidth {
maxLabelWidth = labelWidth maxLabelWidth = labelWidth
} }
@ -292,20 +289,18 @@ func (f *Form) Draw(screen tcell.Screen) {
var focusedPosition struct{ x, y, width, height int } var focusedPosition struct{ x, y, width, height 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()) labelWidth := StringWidth(item.GetLabel())
labelWidth := StringWidth(label)
var itemWidth int var itemWidth int
if f.horizontal { if f.horizontal {
fieldWidth := item.GetFieldWidth() fieldWidth := item.GetFieldWidth()
if fieldWidth == 0 { if fieldWidth == 0 {
fieldWidth = DefaultFormFieldWidth fieldWidth = DefaultFormFieldWidth
} }
label += " "
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) labelWidth = maxLabelWidth
itemWidth = width itemWidth = width
} }
@ -320,7 +315,7 @@ func (f *Form) Draw(screen tcell.Screen) {
itemWidth = rightLimit - x itemWidth = rightLimit - x
} }
item.SetFormAttributes( item.SetFormAttributes(
label, labelWidth,
f.labelColor, f.labelColor,
f.backgroundColor, f.backgroundColor,
f.fieldTextColor, f.fieldTextColor,

View File

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

View File

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