diff --git a/demos/inputfield/main.go b/demos/inputfield/main.go index a9878cf..b5ccd77 100644 --- a/demos/inputfield/main.go +++ b/demos/inputfield/main.go @@ -10,6 +10,7 @@ func main() { app := tview.NewApplication() inputField := tview.NewInputField(). SetLabel("Enter a number: "). + SetPlaceholder("E.g. 1234"). SetFieldWidth(10). SetAcceptanceFunc(tview.InputFieldInteger). SetDoneFunc(func(key tcell.Key) { diff --git a/inputfield.go b/inputfield.go index e8d06d0..4633207 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 text to be displayed in the input area when "text" is empty. + placeholder string + // The label color. labelColor tcell.Color @@ -35,6 +38,9 @@ type InputField struct { // The text color of the input area. 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 // possible. fieldWidth int @@ -62,6 +68,7 @@ func NewInputField() *InputField { labelColor: Styles.SecondaryTextColor, fieldBackgroundColor: Styles.ContrastBackgroundColor, fieldTextColor: Styles.PrimaryTextColor, + placeholderTextColor: Styles.ContrastSecondaryTextColor, } } @@ -90,6 +97,12 @@ func (i *InputField) GetLabel() string { 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. func (i *InputField) SetLabelColor(color tcell.Color) *InputField { i.labelColor = color @@ -108,6 +121,12 @@ 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 { + 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 @@ -200,8 +219,13 @@ func (i *InputField) Draw(screen tcell.Screen) { screen.SetContent(x+index, y, ' ', nil, fieldStyle) } - // Draw entered text. + // Draw placeholder 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 { text = strings.Repeat(string(i.maskCharacter), utf8.RuneCountInString(i.text)) } diff --git a/styles.go b/styles.go index c256778..dbd60ad 100644 --- a/styles.go +++ b/styles.go @@ -18,6 +18,7 @@ var Styles = struct { SecondaryTextColor tcell.Color // Secondary text (e.g. labels). TertiaryTextColor tcell.Color // Tertiary text (e.g. subtitles, notes). InverseTextColor tcell.Color // Text on primary-colored backgrounds. + ContrastSecondaryTextColor tcell.Color // Secondary text on ContrastBackgroundColor-colored backgrounds. }{ PrimitiveBackgroundColor: tcell.ColorBlack, ContrastBackgroundColor: tcell.ColorBlue, @@ -29,4 +30,5 @@ var Styles = struct { SecondaryTextColor: tcell.ColorYellow, TertiaryTextColor: tcell.ColorGreen, InverseTextColor: tcell.ColorBlue, + ContrastSecondaryTextColor: tcell.ColorDarkCyan, }