tui.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. // MAIN MODEL DEFINITIONS -----------------------------------------------------
  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. var cmds []tea.Cmd
  58. switch m.widget.(type) {
  59. case IssueCollection:
  60. if msg, ok := msg.(tea.KeyMsg); ok {
  61. switch msg.String() {
  62. case "j":
  63. if collection, ok := m.widget.(IssueCollection); ok {
  64. if collection.selection+1 < len(collection.Collection) {
  65. collection.selection = collection.selection + 1
  66. } else {
  67. collection.selection = 0
  68. }
  69. m.widget = collection
  70. return m, collection.render
  71. }
  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.render
  82. }
  83. case "enter":
  84. if _, ok := m.widget.(IssueCollection); ok {
  85. m.Path = m.widget.(IssueCollection).Collection[m.widget.(IssueCollection).selection].Path
  86. return m, m.load
  87. }
  88. case "q":
  89. cmds = append(cmds, tea.Quit)
  90. }
  91. }
  92. }
  93. // general message handling
  94. switch msg := msg.(type) {
  95. case tea.KeyMsg: // keymsg capture that is always present
  96. switch msg.String() {
  97. case "ctrl+c":
  98. cmds = append(cmds, tea.Quit)
  99. }
  100. case widget: // widget is initialized from m.load()
  101. switch T := msg.(type) {
  102. case createIssue:
  103. m.widget = T
  104. cmds = append(cmds, T.render, T.init())
  105. default:
  106. m.widget = T
  107. cmds = append(cmds, T.render)
  108. }
  109. case string:
  110. m.content = msg
  111. }
  112. // finally, handle input updates if any
  113. if w, ok := m.widget.(createIssue); ok {
  114. var cmd tea.Cmd
  115. m.widget, cmd = w.update(msg)
  116. cmds = append(cmds, cmd, w.render)
  117. }
  118. return m, tea.Batch(cmds...)
  119. }
  120. // Handles top level view functionality
  121. func (m Model) View() string {
  122. var output string
  123. if len(m.content) == 0 {
  124. return "loading..."
  125. } else {
  126. output = output + m.content
  127. }
  128. output = output + "\nj/k: down/up\tenter: select\tq: quit"
  129. return output
  130. }
  131. // WIDGET DEFINITIONS ---------------------------------------------------------
  132. // ----------------------------------------------------------------------------
  133. // interface definition for widgets
  134. type widget interface {
  135. render() tea.Msg
  136. }
  137. // -------- creatIssue definitions --------------------------------------------
  138. // ----------------------------------------------------------------------------
  139. // data struct for createIssue
  140. type inputField struct {
  141. input textinput.Model
  142. title string
  143. }
  144. // widget for creating an issue
  145. type createIssue struct {
  146. inputFields []inputField
  147. Path string
  148. selected int
  149. Err error // behaviour undefined
  150. }
  151. func initialInputModel(path string, placeholder string) createIssue {
  152. spawnInput := func(f bool) textinput.Model {
  153. ti := textinput.New()
  154. ti.Placeholder = placeholder
  155. if f {
  156. ti.Focus()
  157. }
  158. ti.CharLimit = 80
  159. ti.Width = 30
  160. return ti
  161. }
  162. var inputs []inputField
  163. for i, t := range [4]string{"title", "status", "tags", "blockers"} {
  164. if i == 0 {
  165. inputs = append(inputs, inputField{title: t, input: spawnInput(true)})
  166. } else {
  167. inputs = append(inputs, inputField{title: t, input: spawnInput(false)})
  168. }
  169. }
  170. return createIssue{
  171. inputFields: inputs,
  172. Path: path,
  173. selected: 0,
  174. Err: nil,
  175. }
  176. }
  177. func (ci createIssue) init() tea.Cmd {
  178. return textinput.Blink
  179. }
  180. func (ci createIssue) update(msg tea.Msg) (createIssue, tea.Cmd) {
  181. var cmds []tea.Cmd
  182. var cmd tea.Cmd
  183. // simple anon funcs to increment the selected index
  184. incrementSelected := func() {
  185. if ci.selected < len(ci.inputFields) {
  186. ci.selected++
  187. for i := 0; i < len(ci.inputFields); i++ {
  188. if i == ci.selected {
  189. ci.inputFields[i].input.Focus()
  190. } else {
  191. ci.inputFields[i].input.Blur()
  192. }
  193. }
  194. } else {
  195. ci.selected = 0
  196. ci.inputFields[ci.selected].input.Focus()
  197. }
  198. }
  199. decrementSelected := func() {
  200. if ci.selected != 0 {
  201. ci.selected--
  202. for i := 0; i < len(ci.inputFields); i++ {
  203. if i == ci.selected {
  204. ci.inputFields[i].input.Focus()
  205. } else {
  206. ci.inputFields[i].input.Blur()
  207. }
  208. }
  209. } else {
  210. for i := 0; i < len(ci.inputFields); i++ {
  211. ci.inputFields[i].input.Blur()
  212. }
  213. ci.selected = len(ci.inputFields)
  214. }
  215. }
  216. switch msg := msg.(type) { // keybinding handler
  217. case tea.KeyMsg:
  218. switch msg.String() {
  219. case "tab":
  220. incrementSelected()
  221. case "shift+tab":
  222. decrementSelected()
  223. case "enter":
  224. if ci.selected == len(ci.inputFields) { // confirm create
  225. ci.selected++
  226. } else if ci.selected == len(ci.inputFields)+1 { // confirmed
  227. cmds = append(cmds, tea.Quit)
  228. } else {
  229. incrementSelected()
  230. }
  231. case "esc": // cancel
  232. cmds = append(cmds, tea.Quit)
  233. }
  234. }
  235. for i, ti := range ci.inputFields {
  236. ci.inputFields[i].input, cmd = ti.input.Update(msg)
  237. cmds = append(cmds, cmd)
  238. }
  239. cmds = append(cmds, cmd)
  240. return ci, tea.Batch(cmds...)
  241. }
  242. func (ci createIssue) render() tea.Msg {
  243. borderStyle := lipgloss.NewStyle().
  244. BorderStyle(lipgloss.NormalBorder()).
  245. Margin(1).
  246. Padding(0, 1)
  247. ulStyle := lipgloss.NewStyle().Underline(true)
  248. var output string
  249. for _, field := range ci.inputFields {
  250. output = output + fmt.Sprintf(
  251. "\n%s:%s",
  252. field.title,
  253. borderStyle.Render(field.input.View()),
  254. )
  255. }
  256. output = strings.TrimLeft(output, "\n")
  257. if ci.selected < len(ci.inputFields) {
  258. output = output + borderStyle.Render("press enter to submit...")
  259. } else if ci.selected == len(ci.inputFields) {
  260. output = output + borderStyle.Render(ulStyle.Render("press enter to submit..."))
  261. } else if ci.selected == len(ci.inputFields)+1 {
  262. confirmPrompt := fmt.Sprintf(
  263. "creating issue titled \"%s\"...\n\n%s",
  264. ulStyle.Render(ci.inputFields[0].input.Value()),
  265. ulStyle.Render("press enter to confirm..."),
  266. )
  267. output = output + borderStyle.Render(confirmPrompt)
  268. }
  269. return output
  270. }
  271. // -------- Issue widget definitions ------------------------------------------
  272. // ----------------------------------------------------------------------------
  273. // Handles all render logic for Issue structs
  274. func (i Issue) render() tea.Msg {
  275. var output string
  276. // title
  277. output = output + titleStyle.Render(i.Title)
  278. // status
  279. output = output + fmt.Sprintf("\n%s", statusStyle.Render(i.Status.Data))
  280. // variadics
  281. var tags string
  282. for _, field := range i.Tags.Fields {
  283. tags = tags + field.Path + ", "
  284. }
  285. tags = strings.TrimRight(tags, ", ")
  286. var blockedby string
  287. for _, field := range i.Blockedby.Fields {
  288. blockedby = blockedby + field.Path + ", "
  289. }
  290. blockedby = strings.TrimRight(blockedby, ", ")
  291. if len(i.Tags.Fields) > 0 {
  292. output = output + variadicTitleStyle.Render("\n\nTags:")
  293. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(tags))
  294. }
  295. if len(i.Blockedby.Fields) > 0 {
  296. output = output + variadicTitleStyle.Render("\n\nBlockedby:")
  297. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(blockedby))
  298. }
  299. // description
  300. output = output + titleStyle.Render("\n\nDescription:\n")
  301. output = output + fmt.Sprintf("\n%s", i.Description.Data)
  302. return borderStyle.Render(output)
  303. }
  304. // -------- IssueCollection widget definitions --------------------------------
  305. // ----------------------------------------------------------------------------
  306. // Handles all render logic for IssueCollection structs.
  307. func (ic IssueCollection) render() tea.Msg {
  308. var output string
  309. var left string
  310. output = output + "Issues in " + ic.Path + "...\n\n"
  311. for i, issue := range ic.Collection {
  312. // pointer render
  313. if i == ic.selection {
  314. left = left + pointerStyle.Render("-> ")
  315. } else {
  316. left = left + pointerStyle.Render(" ")
  317. }
  318. // index render
  319. left = left + "[" + indexStyle.Render(fmt.Sprintf("%d", i+1)) + "]: "
  320. // title render
  321. left = left + fmt.Sprintf("%s\n", titleStyle.Render(issue.Title))
  322. }
  323. output = output + collectionStyleLeft.Render(left)
  324. return output
  325. }
  326. // tea.Cmd definitions --------------------------------------------------------
  327. // ----------------------------------------------------------------------------
  328. // Handles load logic
  329. func (m Model) load() tea.Msg {
  330. if IsIssue(m.Path) {
  331. issue, err := Issue{}.NewFromPath(m.Path)
  332. if err != nil {
  333. return nil
  334. }
  335. return issue
  336. }
  337. if IsIssueCollection(m.Path) {
  338. collection, err := IssueCollection{}.NewFromPath(m.Path)
  339. if err != nil {
  340. return nil
  341. }
  342. return collection
  343. }
  344. return initialInputModel(m.Path, "lorem ipsum")
  345. }