flex.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package layout
  2. import (
  3. "strings"
  4. "github.com/charmbracelet/lipgloss/v2"
  5. "github.com/sst/opencode/internal/styles"
  6. )
  7. type Direction int
  8. const (
  9. Row Direction = iota
  10. Column
  11. )
  12. type Justify int
  13. const (
  14. JustifyStart Justify = iota
  15. JustifyEnd
  16. JustifyCenter
  17. JustifySpaceBetween
  18. JustifySpaceAround
  19. )
  20. type Align int
  21. const (
  22. AlignStart Align = iota
  23. AlignEnd
  24. AlignCenter
  25. AlignStretch // Only applicable in the cross-axis
  26. )
  27. type FlexOptions struct {
  28. Direction Direction
  29. Justify Justify
  30. Align Align
  31. Width int
  32. Height int
  33. }
  34. type FlexItem struct {
  35. View string
  36. FixedSize int // Fixed size in the main axis (width for Row, height for Column)
  37. Grow bool // If true, the item will grow to fill available space
  38. }
  39. // Render lays out a series of view strings based on flexbox-like rules.
  40. func Render(opts FlexOptions, items ...FlexItem) string {
  41. if len(items) == 0 {
  42. return ""
  43. }
  44. // Calculate dimensions for each item
  45. mainAxisSize := opts.Width
  46. crossAxisSize := opts.Height
  47. if opts.Direction == Column {
  48. mainAxisSize = opts.Height
  49. crossAxisSize = opts.Width
  50. }
  51. // Calculate total fixed size and count grow items
  52. totalFixedSize := 0
  53. growCount := 0
  54. for _, item := range items {
  55. if item.FixedSize > 0 {
  56. totalFixedSize += item.FixedSize
  57. } else if item.Grow {
  58. growCount++
  59. }
  60. }
  61. // Calculate available space for grow items
  62. availableSpace := mainAxisSize - totalFixedSize
  63. if availableSpace < 0 {
  64. availableSpace = 0
  65. }
  66. // Calculate size for each grow item
  67. growItemSize := 0
  68. if growCount > 0 && availableSpace > 0 {
  69. growItemSize = availableSpace / growCount
  70. }
  71. // Prepare sized views
  72. sizedViews := make([]string, len(items))
  73. actualSizes := make([]int, len(items))
  74. for i, item := range items {
  75. view := item.View
  76. // Determine the size for this item
  77. itemSize := 0
  78. if item.FixedSize > 0 {
  79. itemSize = item.FixedSize
  80. } else if item.Grow && growItemSize > 0 {
  81. itemSize = growItemSize
  82. } else {
  83. // No fixed size and not growing - use natural size
  84. if opts.Direction == Row {
  85. itemSize = lipgloss.Width(view)
  86. } else {
  87. itemSize = lipgloss.Height(view)
  88. }
  89. }
  90. // Apply size constraints
  91. if opts.Direction == Row {
  92. // For row direction, constrain width and handle height alignment
  93. if itemSize > 0 {
  94. view = styles.NewStyle().
  95. Width(itemSize).
  96. Height(crossAxisSize).
  97. Render(view)
  98. }
  99. // Apply cross-axis alignment
  100. switch opts.Align {
  101. case AlignCenter:
  102. view = lipgloss.PlaceVertical(crossAxisSize, lipgloss.Center, view)
  103. case AlignEnd:
  104. view = lipgloss.PlaceVertical(crossAxisSize, lipgloss.Bottom, view)
  105. case AlignStart:
  106. view = lipgloss.PlaceVertical(crossAxisSize, lipgloss.Top, view)
  107. case AlignStretch:
  108. // Already stretched by Height setting above
  109. }
  110. } else {
  111. // For column direction, constrain height and handle width alignment
  112. if itemSize > 0 {
  113. view = styles.NewStyle().
  114. Height(itemSize).
  115. Width(crossAxisSize).
  116. Render(view)
  117. }
  118. // Apply cross-axis alignment
  119. switch opts.Align {
  120. case AlignCenter:
  121. view = lipgloss.PlaceHorizontal(crossAxisSize, lipgloss.Center, view)
  122. case AlignEnd:
  123. view = lipgloss.PlaceHorizontal(crossAxisSize, lipgloss.Right, view)
  124. case AlignStart:
  125. view = lipgloss.PlaceHorizontal(crossAxisSize, lipgloss.Left, view)
  126. case AlignStretch:
  127. // Already stretched by Width setting above
  128. }
  129. }
  130. sizedViews[i] = view
  131. if opts.Direction == Row {
  132. actualSizes[i] = lipgloss.Width(view)
  133. } else {
  134. actualSizes[i] = lipgloss.Height(view)
  135. }
  136. }
  137. // Calculate total actual size
  138. totalActualSize := 0
  139. for _, size := range actualSizes {
  140. totalActualSize += size
  141. }
  142. // Apply justification
  143. remainingSpace := mainAxisSize - totalActualSize
  144. if remainingSpace < 0 {
  145. remainingSpace = 0
  146. }
  147. // Calculate spacing based on justification
  148. var spaceBefore, spaceBetween, spaceAfter int
  149. switch opts.Justify {
  150. case JustifyStart:
  151. spaceAfter = remainingSpace
  152. case JustifyEnd:
  153. spaceBefore = remainingSpace
  154. case JustifyCenter:
  155. spaceBefore = remainingSpace / 2
  156. spaceAfter = remainingSpace - spaceBefore
  157. case JustifySpaceBetween:
  158. if len(items) > 1 {
  159. spaceBetween = remainingSpace / (len(items) - 1)
  160. } else {
  161. spaceAfter = remainingSpace
  162. }
  163. case JustifySpaceAround:
  164. if len(items) > 0 {
  165. spaceAround := remainingSpace / (len(items) * 2)
  166. spaceBefore = spaceAround
  167. spaceAfter = spaceAround
  168. spaceBetween = spaceAround * 2
  169. }
  170. }
  171. // Build the final layout
  172. var parts []string
  173. // Add space before if needed
  174. if spaceBefore > 0 {
  175. if opts.Direction == Row {
  176. parts = append(parts, strings.Repeat(" ", spaceBefore))
  177. } else {
  178. parts = append(parts, strings.Repeat("\n", spaceBefore))
  179. }
  180. }
  181. // Add items with spacing
  182. for i, view := range sizedViews {
  183. parts = append(parts, view)
  184. // Add space between items (not after the last one)
  185. if i < len(sizedViews)-1 && spaceBetween > 0 {
  186. if opts.Direction == Row {
  187. parts = append(parts, strings.Repeat(" ", spaceBetween))
  188. } else {
  189. parts = append(parts, strings.Repeat("\n", spaceBetween))
  190. }
  191. }
  192. }
  193. // Add space after if needed
  194. if spaceAfter > 0 {
  195. if opts.Direction == Row {
  196. parts = append(parts, strings.Repeat(" ", spaceAfter))
  197. } else {
  198. parts = append(parts, strings.Repeat("\n", spaceAfter))
  199. }
  200. }
  201. // Join the parts
  202. if opts.Direction == Row {
  203. return lipgloss.JoinHorizontal(lipgloss.Top, parts...)
  204. } else {
  205. return lipgloss.JoinVertical(lipgloss.Left, parts...)
  206. }
  207. }
  208. // Helper function to create a simple vertical layout
  209. func Vertical(width, height int, items ...FlexItem) string {
  210. return Render(FlexOptions{
  211. Direction: Column,
  212. Width: width,
  213. Height: height,
  214. Justify: JustifyStart,
  215. Align: AlignStretch,
  216. }, items...)
  217. }
  218. // Helper function to create a simple horizontal layout
  219. func Horizontal(width, height int, items ...FlexItem) string {
  220. return Render(FlexOptions{
  221. Direction: Row,
  222. Width: width,
  223. Height: height,
  224. Justify: JustifyStart,
  225. Align: AlignStretch,
  226. }, items...)
  227. }