tui.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. m.Path = m.widget.(IssueCollection).Collection[m.widget.(IssueCollection).selection].Path
  85. return m, m.load
  86. }
  87. case widget:
  88. switch T := msg.(type) {
  89. case Issue:
  90. m.widget = T
  91. return m, T.view
  92. case IssueCollection:
  93. m.widget = T
  94. return m, T.view
  95. }
  96. case string:
  97. m.content = msg
  98. return m, nil
  99. }
  100. return m, nil
  101. }
  102. // Handles top level view functionality
  103. func (m Model) View() string {
  104. var output string
  105. if len(m.content) == 0 {
  106. return "loading..."
  107. } else {
  108. output = output + m.content
  109. }
  110. output = output + "\nj/k: down/up\tenter: select\tq: quit"
  111. return output
  112. }
  113. // Handles load logic
  114. func (m Model) load() tea.Msg {
  115. if IsIssue(m.Path) {
  116. issue, err := Issue{}.NewFromPath(m.Path)
  117. if err != nil {
  118. return nil
  119. }
  120. return issue
  121. }
  122. if IsIssueCollection(m.Path) {
  123. collection, err := IssueCollection{}.NewFromPath(m.Path)
  124. if err != nil {
  125. return nil
  126. }
  127. return collection
  128. }
  129. return nil
  130. }
  131. // Handles all view logic for Issue structs
  132. func (i Issue) view() tea.Msg {
  133. var output string
  134. // title
  135. output = output + titleStyle.Render(i.Title)
  136. // status
  137. output = output + fmt.Sprintf("\n%s", statusStyle.Render(i.Status.Data))
  138. // variadics
  139. var tags string
  140. for _, field := range i.Tags.Fields {
  141. tags = tags + field.Path + ", "
  142. }
  143. tags = strings.TrimRight(tags, ", ")
  144. var blockedby string
  145. for _, field := range i.Blockedby.Fields {
  146. blockedby = blockedby + field.Path + ", "
  147. }
  148. blockedby = strings.TrimRight(blockedby, ", ")
  149. if len(i.Tags.Fields) > 0 {
  150. output = output + variadicTitleStyle.Render("\n\nTags:")
  151. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(tags))
  152. }
  153. if len(i.Blockedby.Fields) > 0 {
  154. output = output + variadicTitleStyle.Render("\n\nBlockedby:")
  155. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(blockedby))
  156. }
  157. // description
  158. output = output + titleStyle.Render("\n\nDescription:\n")
  159. output = output + fmt.Sprintf("\n%s", i.Description.Data)
  160. return borderStyle.Render(output)
  161. }
  162. // Handles all view logic for IssueCollection structs.
  163. func (ic IssueCollection) view() tea.Msg {
  164. var output string
  165. var left string
  166. output = output + "Issues in " + ic.Path + "...\n\n"
  167. for i, issue := range ic.Collection {
  168. // pointer render
  169. if i == ic.selection {
  170. left = left + pointerStyle.Render("-> ")
  171. } else {
  172. left = left + pointerStyle.Render(" ")
  173. }
  174. // index render
  175. left = left + "[" + indexStyle.Render(fmt.Sprintf("%d", i+1)) + "]: "
  176. // title render
  177. left = left + fmt.Sprintf("%s\n", titleStyle.Render(issue.Title))
  178. }
  179. output = output + collectionStyleLeft.Render(left)
  180. return output
  181. }