layout.go 618 B

1234567891011121314151617181920212223242526272829303132333435
  1. package layout
  2. import (
  3. "reflect"
  4. "github.com/charmbracelet/bubbles/key"
  5. tea "github.com/charmbracelet/bubbletea"
  6. )
  7. type Focusable interface {
  8. Focus() tea.Cmd
  9. Blur() tea.Cmd
  10. IsFocused() bool
  11. }
  12. type Sizeable interface {
  13. SetSize(width, height int) tea.Cmd
  14. GetSize() (int, int)
  15. }
  16. type Bindings interface {
  17. BindingKeys() []key.Binding
  18. }
  19. func KeyMapToSlice(t any) (bindings []key.Binding) {
  20. typ := reflect.TypeOf(t)
  21. if typ.Kind() != reflect.Struct {
  22. return nil
  23. }
  24. for i := range typ.NumField() {
  25. v := reflect.ValueOf(t).Field(i)
  26. bindings = append(bindings, v.Interface().(key.Binding))
  27. }
  28. return
  29. }