tui.go 7.3 KB

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