tui.go 7.1 KB

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