mirror of
https://github.com/rivo/tview.git
synced 2025-04-26 13:49:06 +08:00
Added placeholder text to InputField. Resolves #75
This commit is contained in:
parent
0b69b9b581
commit
370ee01609
@ -10,6 +10,7 @@ func main() {
|
|||||||
app := tview.NewApplication()
|
app := tview.NewApplication()
|
||||||
inputField := tview.NewInputField().
|
inputField := tview.NewInputField().
|
||||||
SetLabel("Enter a number: ").
|
SetLabel("Enter a number: ").
|
||||||
|
SetPlaceholder("E.g. 1234").
|
||||||
SetFieldWidth(10).
|
SetFieldWidth(10).
|
||||||
SetAcceptanceFunc(tview.InputFieldInteger).
|
SetAcceptanceFunc(tview.InputFieldInteger).
|
||||||
SetDoneFunc(func(key tcell.Key) {
|
SetDoneFunc(func(key tcell.Key) {
|
||||||
|
@ -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 text to be displayed in the input area when "text" is empty.
|
||||||
|
placeholder string
|
||||||
|
|
||||||
// The label color.
|
// The label color.
|
||||||
labelColor tcell.Color
|
labelColor tcell.Color
|
||||||
|
|
||||||
@ -35,6 +38,9 @@ type InputField struct {
|
|||||||
// The text color of the input area.
|
// The text color of the input area.
|
||||||
fieldTextColor tcell.Color
|
fieldTextColor tcell.Color
|
||||||
|
|
||||||
|
// The text color of the placeholder.
|
||||||
|
placeholderTextColor tcell.Color
|
||||||
|
|
||||||
// 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
|
||||||
@ -62,6 +68,7 @@ func NewInputField() *InputField {
|
|||||||
labelColor: Styles.SecondaryTextColor,
|
labelColor: Styles.SecondaryTextColor,
|
||||||
fieldBackgroundColor: Styles.ContrastBackgroundColor,
|
fieldBackgroundColor: Styles.ContrastBackgroundColor,
|
||||||
fieldTextColor: Styles.PrimaryTextColor,
|
fieldTextColor: Styles.PrimaryTextColor,
|
||||||
|
placeholderTextColor: Styles.ContrastSecondaryTextColor,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,6 +97,12 @@ func (i *InputField) GetLabel() string {
|
|||||||
return i.label
|
return i.label
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetPlaceholder sets the text to be displayed when the input text is empty.
|
||||||
|
func (i *InputField) SetPlaceholder(text string) *InputField {
|
||||||
|
i.placeholder = text
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
// SetLabelColor sets the color of the label.
|
// SetLabelColor sets the color of the label.
|
||||||
func (i *InputField) SetLabelColor(color tcell.Color) *InputField {
|
func (i *InputField) SetLabelColor(color tcell.Color) *InputField {
|
||||||
i.labelColor = color
|
i.labelColor = color
|
||||||
@ -108,6 +121,12 @@ func (i *InputField) SetFieldTextColor(color tcell.Color) *InputField {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetPlaceholderExtColor sets the text color of placeholder text.
|
||||||
|
func (i *InputField) SetPlaceholderExtColor(color tcell.Color) *InputField {
|
||||||
|
i.placeholderTextColor = color
|
||||||
|
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(label string, labelColor, bgColor, fieldTextColor, fieldBgColor tcell.Color) FormItem {
|
||||||
i.label = label
|
i.label = label
|
||||||
@ -200,8 +219,13 @@ func (i *InputField) Draw(screen tcell.Screen) {
|
|||||||
screen.SetContent(x+index, y, ' ', nil, fieldStyle)
|
screen.SetContent(x+index, y, ' ', nil, fieldStyle)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw entered text.
|
// Draw placeholder text.
|
||||||
text := i.text
|
text := i.text
|
||||||
|
if text == "" && i.placeholder != "" {
|
||||||
|
Print(screen, i.placeholder, x, y, fieldWidth, AlignLeft, i.placeholderTextColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw entered text.
|
||||||
if i.maskCharacter > 0 {
|
if i.maskCharacter > 0 {
|
||||||
text = strings.Repeat(string(i.maskCharacter), utf8.RuneCountInString(i.text))
|
text = strings.Repeat(string(i.maskCharacter), utf8.RuneCountInString(i.text))
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ var Styles = struct {
|
|||||||
SecondaryTextColor tcell.Color // Secondary text (e.g. labels).
|
SecondaryTextColor tcell.Color // Secondary text (e.g. labels).
|
||||||
TertiaryTextColor tcell.Color // Tertiary text (e.g. subtitles, notes).
|
TertiaryTextColor tcell.Color // Tertiary text (e.g. subtitles, notes).
|
||||||
InverseTextColor tcell.Color // Text on primary-colored backgrounds.
|
InverseTextColor tcell.Color // Text on primary-colored backgrounds.
|
||||||
|
ContrastSecondaryTextColor tcell.Color // Secondary text on ContrastBackgroundColor-colored backgrounds.
|
||||||
}{
|
}{
|
||||||
PrimitiveBackgroundColor: tcell.ColorBlack,
|
PrimitiveBackgroundColor: tcell.ColorBlack,
|
||||||
ContrastBackgroundColor: tcell.ColorBlue,
|
ContrastBackgroundColor: tcell.ColorBlue,
|
||||||
@ -29,4 +30,5 @@ var Styles = struct {
|
|||||||
SecondaryTextColor: tcell.ColorYellow,
|
SecondaryTextColor: tcell.ColorYellow,
|
||||||
TertiaryTextColor: tcell.ColorGreen,
|
TertiaryTextColor: tcell.ColorGreen,
|
||||||
InverseTextColor: tcell.ColorBlue,
|
InverseTextColor: tcell.ColorBlue,
|
||||||
|
ContrastSecondaryTextColor: tcell.ColorDarkCyan,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user