tui.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // The package defines an extensible TUI via the bubbletea framework.
  2. //
  3. // TODO enable collection recursing (i.e, embeded collections)
  4. //
  5. // TODO enable scroll/viewport logic on issues!!!
  6. //
  7. // While the package remains in v0.0.X releases, this TUI may be undocumented.
  8. package issues
  9. import (
  10. "fmt"
  11. "strings"
  12. tea "github.com/charmbracelet/bubbletea"
  13. "github.com/charmbracelet/lipgloss"
  14. )
  15. type widget interface {
  16. view() tea.Msg
  17. }
  18. // The main bubbletea Model
  19. type Model struct {
  20. widget widget
  21. content string
  22. Path string
  23. // viewport viewport.Model
  24. }
  25. func (m Model) Init() tea.Cmd { return m.load }
  26. // Handles quit logic and viewport scroll and size updates
  27. func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  28. switch msg := msg.(type) {
  29. case tea.KeyMsg:
  30. switch msg.String() {
  31. case "q":
  32. return m, tea.Quit
  33. case "k":
  34. if collection, ok := m.widget.(IssueCollection); ok {
  35. if collection.selection+1 < len(collection.Collection) {
  36. collection.selection = collection.selection + 1
  37. } else {
  38. collection.selection = 0
  39. }
  40. m.widget = collection
  41. return m, collection.view
  42. }
  43. return m, nil
  44. case "j":
  45. // do something only if widget is collection
  46. if collection, ok := m.widget.(IssueCollection); ok {
  47. if collection.selection != 0 {
  48. collection.selection = collection.selection - 1
  49. } else {
  50. collection.selection = len(collection.Collection) - 1
  51. }
  52. m.widget = collection
  53. return m, collection.view
  54. }
  55. return m, nil
  56. case "enter":
  57. m.Path = m.widget.(IssueCollection).Collection[m.widget.(IssueCollection).selection].Path
  58. return m, m.load
  59. }
  60. case widget:
  61. switch T := msg.(type) {
  62. case Issue:
  63. m.widget = T
  64. return m, T.view
  65. case IssueCollection:
  66. m.widget = T
  67. return m, T.view
  68. }
  69. case string:
  70. m.content = msg
  71. return m, nil
  72. }
  73. return m, nil
  74. }
  75. // Handles load logic
  76. func (m Model) load() tea.Msg {
  77. if IsIssue(m.Path) {
  78. issue, err := Issue{}.NewFromPath(m.Path)
  79. if err != nil {
  80. return nil
  81. }
  82. return issue
  83. }
  84. if IsIssueCollection(m.Path) {
  85. collection, err := IssueCollection{}.NewFromPath(m.Path)
  86. if err != nil {
  87. return nil
  88. }
  89. return collection
  90. }
  91. return nil
  92. }
  93. // [lipgloss] style definitions
  94. var (
  95. titleStyle = lipgloss.NewStyle().
  96. Bold(true).
  97. Underline(true)
  98. statusStyle = lipgloss.NewStyle().
  99. Faint(true).
  100. Italic(true)
  101. variadicTitleStyle = lipgloss.NewStyle().
  102. Align(lipgloss.Left).
  103. Italic(true)
  104. variadicDataStyle = lipgloss.NewStyle().
  105. Width(40).
  106. BorderStyle(lipgloss.ASCIIBorder())
  107. borderStyle = lipgloss.NewStyle().
  108. Padding(1, 2).
  109. Margin(1).
  110. BorderStyle(lipgloss.NormalBorder())
  111. indexStyle = lipgloss.NewStyle().
  112. Italic(true)
  113. pointerStyle = lipgloss.NewStyle().
  114. Faint(true)
  115. collectionStyleLeft = lipgloss.NewStyle().
  116. Align(lipgloss.Left)
  117. )
  118. // Handles all view logic for Issue structs
  119. func (i Issue) view() tea.Msg {
  120. var output string
  121. // title
  122. output = output + titleStyle.Render(i.Title)
  123. // status
  124. output = output + fmt.Sprintf("\n%s", statusStyle.Render(i.Status.Data))
  125. // variadics
  126. var tags string
  127. for _, field := range i.Tags.Fields {
  128. tags = tags + field.Path + ", "
  129. }
  130. tags = strings.TrimRight(tags, ", ")
  131. var blockedby string
  132. for _, field := range i.Blockedby.Fields {
  133. blockedby = blockedby + field.Path + ", "
  134. }
  135. blockedby = strings.TrimRight(blockedby, ", ")
  136. if len(i.Tags.Fields) > 0 {
  137. output = output + variadicTitleStyle.Render("\n\nTags:")
  138. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(tags))
  139. }
  140. if len(i.Blockedby.Fields) > 0 {
  141. output = output + variadicTitleStyle.Render("\n\nBlockedby:")
  142. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(blockedby))
  143. }
  144. // description
  145. output = output + titleStyle.Render("\n\nDescription:\n")
  146. output = output + fmt.Sprintf("\n%s", i.Description.Data)
  147. return borderStyle.Render(output)
  148. }
  149. // Handles all view logic for IssueCollection structs.
  150. func (ic IssueCollection) view() tea.Msg {
  151. var output string
  152. var left string
  153. output = output + "Issues in " + ic.Path + "...\n\n"
  154. for i, issue := range ic.Collection {
  155. // pointer render
  156. if i == ic.selection {
  157. left = left + pointerStyle.Render("-> ")
  158. } else {
  159. left = left + pointerStyle.Render(" ")
  160. }
  161. // index render
  162. left = left + "[" + indexStyle.Render(fmt.Sprintf("%d", i+1)) + "]: "
  163. // title render
  164. left = left + fmt.Sprintf("%s\n", titleStyle.Render(issue.Title))
  165. }
  166. output = output + collectionStyleLeft.Render(left)
  167. output = output + "\nj/k: down/up\tenter: select\tq: quit"
  168. return output
  169. }
  170. // Wraps [issue.Model.renderIssue] in a viewport
  171. func (m Model) View() string {
  172. if len(m.content) == 0 {
  173. return "loading..."
  174. }
  175. return m.content
  176. }