add autosizing,maxlen to input widget

This commit is contained in:
Bradley Cicenas 2017-01-21 13:46:48 -05:00
parent edce2b674a
commit 6db32bad56

View File

@ -14,6 +14,7 @@ type Input struct {
ui.Block ui.Block
Label string Label string
Data string Data string
MaxLen int
TextFgColor ui.Attribute TextFgColor ui.Attribute
TextBgColor ui.Attribute TextBgColor ui.Attribute
padding Padding padding Padding
@ -23,14 +24,20 @@ func NewInput() *Input {
i := &Input{ i := &Input{
Block: *ui.NewBlock(), Block: *ui.NewBlock(),
Label: "input", Label: "input",
MaxLen: 20,
TextFgColor: ui.ThemeAttr("par.text.fg"), TextFgColor: ui.ThemeAttr("par.text.fg"),
TextBgColor: ui.ThemeAttr("par.text.bg"), TextBgColor: ui.ThemeAttr("par.text.bg"),
padding: Padding{4, 2}, padding: Padding{4, 2},
} }
i.Width, i.Height = 30, 3 i.calcSize()
return i return i
} }
func (i *Input) calcSize() {
i.Height = 3 // minimum height
i.Width = i.MaxLen + (i.padding[0] * 2)
}
func (i *Input) Buffer() ui.Buffer { func (i *Input) Buffer() ui.Buffer {
var cell ui.Cell var cell ui.Cell
buf := i.Block.Buffer() buf := i.Block.Buffer()
@ -53,6 +60,10 @@ func (i *Input) KeyPress(e ui.Event) {
i.Data = i.Data[0:idx] i.Data = i.Data[0:idx]
} }
ui.Render(i) ui.Render(i)
return
}
if len(i.Data) >= i.MaxLen {
return
} }
if strings.Index(input_chars, ch) > -1 { if strings.Index(input_chars, ch) > -1 {
i.Data += ch i.Data += ch