tui.go 6.1 KB

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