tui.go 4.9 KB

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