layout.go 967 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package layout
  2. import (
  3. "reflect"
  4. "github.com/charmbracelet/bubbles/v2/key"
  5. tea "github.com/charmbracelet/bubbletea/v2"
  6. )
  7. var Current *LayoutInfo
  8. func init() {
  9. Current = &LayoutInfo{
  10. Viewport: Dimensions{Width: 80, Height: 25},
  11. Container: Dimensions{Width: 80, Height: 25},
  12. }
  13. }
  14. type LayoutSize string
  15. type Dimensions struct {
  16. Width int
  17. Height int
  18. }
  19. type LayoutInfo struct {
  20. Viewport Dimensions
  21. Container Dimensions
  22. }
  23. type Modal interface {
  24. tea.Model
  25. Render(background string) string
  26. Close() tea.Cmd
  27. }
  28. type Focusable interface {
  29. Focus() tea.Cmd
  30. Blur() tea.Cmd
  31. IsFocused() bool
  32. }
  33. type Sizeable interface {
  34. SetSize(width, height int) tea.Cmd
  35. GetSize() (int, int)
  36. }
  37. func KeyMapToSlice(t any) (bindings []key.Binding) {
  38. typ := reflect.TypeOf(t)
  39. if typ.Kind() != reflect.Struct {
  40. return nil
  41. }
  42. for i := range typ.NumField() {
  43. v := reflect.ValueOf(t).Field(i)
  44. bindings = append(bindings, v.Interface().(key.Binding))
  45. }
  46. return
  47. }