From ed3c693cafe70e241efbf376927edc15416757a4 Mon Sep 17 00:00:00 2001 From: Joe Grasse Date: Mon, 9 Apr 2018 10:39:45 -0500 Subject: [PATCH] Fix for GetFormItemByLabel Issue --- checkbox.go | 16 +++++++++++++++- dropdown.go | 14 +++++++++++++- form.go | 9 ++++++--- inputfield.go | 16 ++++++++++++++-- 4 files changed, 48 insertions(+), 7 deletions(-) diff --git a/checkbox.go b/checkbox.go index 3dbeade..aeab2f9 100644 --- a/checkbox.go +++ b/checkbox.go @@ -1,6 +1,8 @@ package tview import ( + "strings" + "github.com/gdamore/tcell" ) @@ -17,6 +19,9 @@ type Checkbox struct { // The text to be displayed before the input area. label string + // The padding to using on the label. + labelPadding int + // The label color. labelColor tcell.Color @@ -74,6 +79,12 @@ func (c *Checkbox) SetLabelColor(color tcell.Color) *Checkbox { return c } +func (c *Checkbox) setLabelPadding(padding int) FormItem { + c.labelPadding = padding + + return c +} + // SetFieldBackgroundColor sets the background color of the input area. func (c *Checkbox) SetFieldBackgroundColor(color tcell.Color) *Checkbox { c.fieldBackgroundColor = color @@ -137,8 +148,11 @@ func (c *Checkbox) Draw(screen tcell.Screen) { return } + // Get label with padding + label := c.label + strings.Repeat(" ", c.labelPadding) + // 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 // Draw checkbox. diff --git a/dropdown.go b/dropdown.go index 0960e07..b079eda 100644 --- a/dropdown.go +++ b/dropdown.go @@ -39,6 +39,9 @@ type DropDown struct { // The text to be displayed before the input area. label string + // The padding to using on the label. + labelPadding int + // The label color. labelColor tcell.Color @@ -119,6 +122,12 @@ func (d *DropDown) SetLabelColor(color tcell.Color) *DropDown { return d } +func (d *DropDown) setLabelPadding(padding int) FormItem { + d.labelPadding = padding + + return d +} + // SetFieldBackgroundColor sets the background color of the options area. func (d *DropDown) SetFieldBackgroundColor(color tcell.Color) *DropDown { d.fieldBackgroundColor = color @@ -226,8 +235,11 @@ func (d *DropDown) Draw(screen tcell.Screen) { return } + // Get label with padding + label := d.label + strings.Repeat(" ", d.labelPadding) + // 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 // What's the longest option text? diff --git a/form.go b/form.go index 5e424ab..af0f6af 100644 --- a/form.go +++ b/form.go @@ -33,6 +33,8 @@ type FormItem interface { // 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). SetFinishedFunc(handler func(key tcell.Key)) FormItem + + setLabelPadding(padding int) FormItem } // 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. positions := make([]struct{ x, y, width, height int }, len(f.items)+len(f.buttons)) var focusedPosition struct{ x, y, width, height int } + var labelPadding int for index, item := range f.items { // Calculate the space needed. label := strings.TrimSpace(item.GetLabel()) @@ -300,12 +303,12 @@ func (f *Form) Draw(screen tcell.Screen) { if fieldWidth == 0 { fieldWidth = DefaultFormFieldWidth } - label += " " + labelPadding = 1 labelWidth++ itemWidth = labelWidth + fieldWidth } else { // We want all fields to align vertically. - label += strings.Repeat(" ", maxLabelWidth-labelWidth) + labelPadding = maxLabelWidth - labelWidth itemWidth = width } @@ -325,7 +328,7 @@ func (f *Form) Draw(screen tcell.Screen) { f.backgroundColor, f.fieldTextColor, f.fieldBackgroundColor, - ) + ).setLabelPadding(labelPadding) // Save position. positions[index].x = x diff --git a/inputfield.go b/inputfield.go index bb6b024..7a0e625 100644 --- a/inputfield.go +++ b/inputfield.go @@ -26,6 +26,9 @@ type InputField struct { // The text to be displayed before the input area. label string + // The padding to using on the label. + labelPadding int + // The text to be displayed in the input area when "text" is empty. placeholder string @@ -109,6 +112,12 @@ func (i *InputField) SetLabelColor(color tcell.Color) *InputField { return i } +func (i *InputField) setLabelPadding(padding int) FormItem { + i.labelPadding = padding + + return i +} + // SetFieldBackgroundColor sets the background color of the input area. func (i *InputField) SetFieldBackgroundColor(color tcell.Color) *InputField { i.fieldBackgroundColor = color @@ -202,8 +211,11 @@ func (i *InputField) Draw(screen tcell.Screen) { return } + // Get label with padding + label := i.label + strings.Repeat(" ", i.labelPadding) + // 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 // Draw input area. @@ -280,7 +292,7 @@ func (i *InputField) setCursor(screen tcell.Screen) { if i.fieldWidth > 0 && fieldWidth > i.fieldWidth-1 { fieldWidth = i.fieldWidth - 1 } - x += StringWidth(i.label) + fieldWidth + x += StringWidth(i.label) + fieldWidth + i.labelPadding if x >= rightLimit { x = rightLimit - 1 }