layout.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. Size: LayoutSizeNormal,
  11. Viewport: Dimensions{Width: 80, Height: 25},
  12. Container: Dimensions{Width: 80, Height: 25},
  13. }
  14. }
  15. type LayoutSize string
  16. const (
  17. LayoutSizeSmall LayoutSize = "small"
  18. LayoutSizeNormal LayoutSize = "normal"
  19. LayoutSizeLarge LayoutSize = "large"
  20. )
  21. type Dimensions struct {
  22. Width int
  23. Height int
  24. }
  25. type LayoutInfo struct {
  26. Size LayoutSize
  27. Viewport Dimensions
  28. Container Dimensions
  29. }
  30. type Modal interface {
  31. tea.Model
  32. Render(background string) string
  33. Close() tea.Cmd
  34. }
  35. type Focusable interface {
  36. Focus() tea.Cmd
  37. Blur() tea.Cmd
  38. IsFocused() bool
  39. }
  40. type Sizeable interface {
  41. SetSize(width, height int) tea.Cmd
  42. GetSize() (int, int)
  43. }
  44. type Bindings interface {
  45. BindingKeys() []key.Binding
  46. }
  47. func KeyMapToSlice(t any) (bindings []key.Binding) {
  48. typ := reflect.TypeOf(t)
  49. if typ.Kind() != reflect.Struct {
  50. return nil
  51. }
  52. for i := range typ.NumField() {
  53. v := reflect.ValueOf(t).Field(i)
  54. bindings = append(bindings, v.Interface().(key.Binding))
  55. }
  56. return
  57. }