mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add autosizing,maxlen to input widget
This commit is contained in:
parent
edce2b674a
commit
6db32bad56
@ -14,6 +14,7 @@ type Input struct {
|
||||
ui.Block
|
||||
Label string
|
||||
Data string
|
||||
MaxLen int
|
||||
TextFgColor ui.Attribute
|
||||
TextBgColor ui.Attribute
|
||||
padding Padding
|
||||
@ -23,14 +24,20 @@ func NewInput() *Input {
|
||||
i := &Input{
|
||||
Block: *ui.NewBlock(),
|
||||
Label: "input",
|
||||
MaxLen: 20,
|
||||
TextFgColor: ui.ThemeAttr("par.text.fg"),
|
||||
TextBgColor: ui.ThemeAttr("par.text.bg"),
|
||||
padding: Padding{4, 2},
|
||||
}
|
||||
i.Width, i.Height = 30, 3
|
||||
i.calcSize()
|
||||
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 {
|
||||
var cell ui.Cell
|
||||
buf := i.Block.Buffer()
|
||||
@ -53,6 +60,10 @@ func (i *Input) KeyPress(e ui.Event) {
|
||||
i.Data = i.Data[0:idx]
|
||||
}
|
||||
ui.Render(i)
|
||||
return
|
||||
}
|
||||
if len(i.Data) >= i.MaxLen {
|
||||
return
|
||||
}
|
||||
if strings.Index(input_chars, ch) > -1 {
|
||||
i.Data += ch
|
||||
|
Loading…
Reference in New Issue
Block a user