tui.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. // widget specifc keyhandling
  57. switch m.widget.(type) {
  58. case issueCreate:
  59. if msg, ok := msg.(tea.KeyMsg); ok {
  60. switch msg.String() {
  61. case "enter": // TODO create, write, and render an issue on press enter
  62. return m, tea.Quit
  63. }
  64. }
  65. default:
  66. if msg, ok := msg.(tea.KeyMsg); ok {
  67. switch msg.String() {
  68. case "q":
  69. return m, tea.Quit
  70. case "j":
  71. if collection, ok := m.widget.(IssueCollection); ok {
  72. if collection.selection+1 < len(collection.Collection) {
  73. collection.selection = collection.selection + 1
  74. } else {
  75. collection.selection = 0
  76. }
  77. m.widget = collection
  78. return m, collection.view
  79. }
  80. return m, nil
  81. case "k":
  82. // do something only if widget is collection
  83. if collection, ok := m.widget.(IssueCollection); ok {
  84. if collection.selection != 0 {
  85. collection.selection = collection.selection - 1
  86. } else {
  87. collection.selection = len(collection.Collection) - 1
  88. }
  89. m.widget = collection
  90. return m, collection.view
  91. }
  92. return m, nil
  93. case "enter":
  94. if _, ok := m.widget.(IssueCollection); ok {
  95. m.Path = m.widget.(IssueCollection).Collection[m.widget.(IssueCollection).selection].Path
  96. return m, m.load
  97. }
  98. return m, nil
  99. }
  100. }
  101. }
  102. switch msg := msg.(type) {
  103. case tea.KeyMsg:
  104. switch msg.String() {
  105. case "ctrl+c":
  106. return m, tea.Quit
  107. }
  108. case widget:
  109. switch T := msg.(type) {
  110. default:
  111. m.widget = T
  112. return m, T.view
  113. case issueCreate:
  114. T = T.init(Issue{Path: m.Path})
  115. m.widget = T
  116. return m, T.view
  117. }
  118. case string:
  119. m.content = msg
  120. return m, nil
  121. }
  122. return m, nil
  123. }
  124. // Handles top level view functionality
  125. func (m Model) View() string {
  126. var output string
  127. if len(m.content) == 0 {
  128. return "loading..."
  129. } else {
  130. output = output + m.content
  131. }
  132. output = output + "\nj/k: down/up\tenter: select\tq: quit"
  133. return output
  134. }
  135. type issueCreate struct {
  136. focusedField int
  137. inputs []textinput.Model
  138. issue Issue
  139. }
  140. func (c issueCreate) init(i Issue) issueCreate {
  141. c.issue = i
  142. title := textinput.Model{Placeholder: "title", CharLimit: 64, Width: 45}
  143. status := textinput.Model{Placeholder: "status", CharLimit: 64, Width: 45}
  144. tags := textinput.Model{Placeholder: "tags", CharLimit: 64, Width: 45}
  145. blockers := textinput.Model{Placeholder: "blockers", CharLimit: 64, Width: 45}
  146. c.inputs = append(c.inputs, title, status, tags, blockers)
  147. return c
  148. }
  149. func (c issueCreate) update(msg tea.Msg) (issueCreate, tea.Cmd) {
  150. switch T := msg.(type) {
  151. case issueCreate:
  152. c.issue = T.issue
  153. c.focusedField = T.focusedField
  154. c.inputs = T.inputs
  155. return c, nil
  156. case tea.KeyMsg:
  157. switch T.String() {
  158. case "tab":
  159. if c.focusedField < len(c.inputs)-1 {
  160. c.focusedField++
  161. } else {
  162. c.focusedField = 0
  163. }
  164. return c, nil
  165. default:
  166. inputModel, inputCmd := c.inputs[c.focusedField].Update(msg)
  167. c.inputs[c.focusedField] = inputModel
  168. return c, inputCmd
  169. }
  170. }
  171. return c, nil
  172. }
  173. func (c issueCreate) view() tea.Msg { return "testing" }
  174. // Handles load logic
  175. func (m Model) load() tea.Msg {
  176. if IsIssue(m.Path) {
  177. issue, err := Issue{}.NewFromPath(m.Path)
  178. if err != nil {
  179. return nil
  180. }
  181. return issue
  182. }
  183. if IsIssueCollection(m.Path) {
  184. collection, err := IssueCollection{}.NewFromPath(m.Path)
  185. if err != nil {
  186. return nil
  187. }
  188. return collection
  189. }
  190. return issueCreate{issue: Issue{Path: m.Path}}
  191. }
  192. // Handles all view logic for Issue structs
  193. func (i Issue) view() tea.Msg {
  194. var output string
  195. // title
  196. output = output + titleStyle.Render(i.Title)
  197. // status
  198. output = output + fmt.Sprintf("\n%s", statusStyle.Render(i.Status.Data))
  199. // variadics
  200. var tags string
  201. for _, field := range i.Tags.Fields {
  202. tags = tags + field.Path + ", "
  203. }
  204. tags = strings.TrimRight(tags, ", ")
  205. var blockedby string
  206. for _, field := range i.Blockedby.Fields {
  207. blockedby = blockedby + field.Path + ", "
  208. }
  209. blockedby = strings.TrimRight(blockedby, ", ")
  210. if len(i.Tags.Fields) > 0 {
  211. output = output + variadicTitleStyle.Render("\n\nTags:")
  212. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(tags))
  213. }
  214. if len(i.Blockedby.Fields) > 0 {
  215. output = output + variadicTitleStyle.Render("\n\nBlockedby:")
  216. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(blockedby))
  217. }
  218. // description
  219. output = output + titleStyle.Render("\n\nDescription:\n")
  220. output = output + fmt.Sprintf("\n%s", i.Description.Data)
  221. return borderStyle.Render(output)
  222. }
  223. // Handles all view logic for IssueCollection structs.
  224. func (ic IssueCollection) view() tea.Msg {
  225. var output string
  226. var left string
  227. output = output + "Issues in " + ic.Path + "...\n\n"
  228. for i, issue := range ic.Collection {
  229. // pointer render
  230. if i == ic.selection {
  231. left = left + pointerStyle.Render("-> ")
  232. } else {
  233. left = left + pointerStyle.Render(" ")
  234. }
  235. // index render
  236. left = left + "[" + indexStyle.Render(fmt.Sprintf("%d", i+1)) + "]: "
  237. // title render
  238. left = left + fmt.Sprintf("%s\n", titleStyle.Render(issue.Title))
  239. }
  240. output = output + collectionStyleLeft.Render(left)
  241. return output
  242. }