layout.go 679 B

123456789101112131415161718192021222324252627282930313233343536373839
  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 Bordered interface {
  13. BorderText() map[BorderPosition]string
  14. }
  15. type Sizeable interface {
  16. SetSize(width, height int)
  17. GetSize() (int, int)
  18. }
  19. type Bindings interface {
  20. BindingKeys() []key.Binding
  21. }
  22. func KeyMapToSlice(t any) (bindings []key.Binding) {
  23. typ := reflect.TypeOf(t)
  24. if typ.Kind() != reflect.Struct {
  25. return nil
  26. }
  27. for i := range typ.NumField() {
  28. v := reflect.ValueOf(t).Field(i)
  29. bindings = append(bindings, v.Interface().(key.Binding))
  30. }
  31. return
  32. }