tui.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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
  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: // TODO handle updates to IssueCollection widgets in its own update func
  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. //
  135. // TODO add keyhelp func to widget interface
  136. type widget interface {
  137. render() tea.Msg
  138. }
  139. // -------- creatIssue definitions --------------------------------------------
  140. // ----------------------------------------------------------------------------
  141. // data struct for createIssue
  142. type inputField struct {
  143. input textinput.Model
  144. title string
  145. }
  146. // widget for creating an issue
  147. //
  148. // TODO invoke editor for descriptions
  149. type createIssue struct {
  150. inputFields []inputField
  151. Path string
  152. selected int
  153. Err error // not implemented
  154. }
  155. func initialCreateIssueModel(path string, placeholder string) createIssue {
  156. spawnInput := func(f bool) textinput.Model {
  157. ti := textinput.New()
  158. ti.Placeholder = placeholder
  159. if f {
  160. ti.Focus()
  161. }
  162. ti.CharLimit = 80
  163. ti.Width = 30
  164. return ti
  165. }
  166. var inputs []inputField
  167. for i, t := range [4]string{"title", "status", "tags", "blockers"} {
  168. if i == 0 {
  169. inputs = append(inputs, inputField{title: t, input: spawnInput(true)})
  170. } else {
  171. inputs = append(inputs, inputField{title: t, input: spawnInput(false)})
  172. }
  173. }
  174. return createIssue{
  175. inputFields: inputs,
  176. Path: path,
  177. selected: 0,
  178. Err: nil,
  179. }
  180. }
  181. func (ci createIssue) init() tea.Cmd {
  182. return textinput.Blink
  183. }
  184. func (ci createIssue) update(msg tea.Msg) (createIssue, tea.Cmd) {
  185. var cmds []tea.Cmd
  186. var cmd tea.Cmd
  187. // simple anon funcs to increment the selected index
  188. incrementSelected := func() {
  189. if ci.selected < len(ci.inputFields) {
  190. ci.selected++
  191. for i := 0; i < len(ci.inputFields); i++ {
  192. if i == ci.selected {
  193. ci.inputFields[i].input.Focus()
  194. } else {
  195. ci.inputFields[i].input.Blur()
  196. }
  197. }
  198. } else {
  199. ci.selected = 0
  200. ci.inputFields[ci.selected].input.Focus()
  201. }
  202. }
  203. decrementSelected := func() {
  204. if ci.selected != 0 {
  205. ci.selected--
  206. for i := 0; i < len(ci.inputFields); i++ {
  207. if i == ci.selected {
  208. ci.inputFields[i].input.Focus()
  209. } else {
  210. ci.inputFields[i].input.Blur()
  211. }
  212. }
  213. } else {
  214. for i := 0; i < len(ci.inputFields); i++ {
  215. ci.inputFields[i].input.Blur()
  216. }
  217. ci.selected = len(ci.inputFields)
  218. }
  219. }
  220. switch msg := msg.(type) { // keybinding handler
  221. case tea.KeyMsg:
  222. switch msg.String() {
  223. case "tab":
  224. incrementSelected()
  225. case "shift+tab":
  226. decrementSelected()
  227. case "enter":
  228. if ci.selected == len(ci.inputFields) { // confirm create
  229. ci.selected++
  230. } else if ci.selected == len(ci.inputFields)+1 { // confirmed
  231. cmds = append(cmds, ci.create)
  232. } else {
  233. incrementSelected()
  234. }
  235. case "esc": // cancel
  236. cmds = append(cmds, tea.Quit)
  237. }
  238. case createResult:
  239. cmds = append(cmds, ci.write(Issue(msg)))
  240. case writeResult:
  241. switch value := msg.(type) {
  242. case bool:
  243. if !value {
  244. } else {
  245. cmds = append(cmds, tea.Quit)
  246. }
  247. case error:
  248. panic(value)
  249. }
  250. }
  251. for i, ti := range ci.inputFields {
  252. ci.inputFields[i].input, cmd = ti.input.Update(msg)
  253. cmds = append(cmds, cmd)
  254. }
  255. cmds = append(cmds, cmd)
  256. return ci, tea.Batch(cmds...)
  257. }
  258. func (ci createIssue) render() tea.Msg {
  259. borderStyle := lipgloss.NewStyle().
  260. BorderStyle(lipgloss.NormalBorder()).
  261. Margin(1).
  262. Padding(0, 1)
  263. ulStyle := lipgloss.NewStyle().Underline(true)
  264. var output string
  265. for _, field := range ci.inputFields {
  266. output = output + fmt.Sprintf(
  267. "\n%s:%s",
  268. field.title,
  269. borderStyle.Render(field.input.View()),
  270. )
  271. }
  272. output = strings.TrimLeft(output, "\n")
  273. if ci.selected < len(ci.inputFields) {
  274. output = output + borderStyle.Render("press enter to submit...")
  275. } else if ci.selected == len(ci.inputFields) {
  276. output = output + borderStyle.Render(ulStyle.Render("press enter to submit..."))
  277. } else if ci.selected == len(ci.inputFields)+1 {
  278. confirmPrompt := fmt.Sprintf(
  279. "creating issue titled \"%s\"...\n\n%s",
  280. ulStyle.Render(ci.inputFields[0].input.Value()),
  281. ulStyle.Render("press enter to confirm..."),
  282. )
  283. output = output + borderStyle.Render(confirmPrompt)
  284. }
  285. return output
  286. }
  287. // -------- Issue widget definitions ------------------------------------------
  288. // ----------------------------------------------------------------------------
  289. // Handles all render logic for Issue structs
  290. func (i Issue) render() tea.Msg {
  291. var output string
  292. // title
  293. output = output + titleStyle.Render(i.Title)
  294. // status
  295. output = output + fmt.Sprintf("\n%s", statusStyle.Render(i.Status.Data))
  296. // variadics
  297. var tags string
  298. for _, field := range i.Tags.Fields {
  299. tags = tags + field.Path + ", "
  300. }
  301. tags = strings.TrimRight(tags, ", ")
  302. var blockedby string
  303. for _, field := range i.Blockedby.Fields {
  304. blockedby = blockedby + field.Path + ", "
  305. }
  306. blockedby = strings.TrimRight(blockedby, ", ")
  307. if len(i.Tags.Fields) > 0 {
  308. output = output + variadicTitleStyle.Render("\n\nTags:")
  309. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(tags))
  310. }
  311. if len(i.Blockedby.Fields) > 0 {
  312. output = output + variadicTitleStyle.Render("\n\nBlockedby:")
  313. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(blockedby))
  314. }
  315. // description
  316. output = output + titleStyle.Render("\n\nDescription:\n")
  317. output = output + fmt.Sprintf("\n%s", i.Description.Data)
  318. return borderStyle.Render(output)
  319. }
  320. // -------- IssueCollection widget definitions --------------------------------
  321. // ----------------------------------------------------------------------------
  322. // Handles all render logic for IssueCollection structs.
  323. func (ic IssueCollection) render() tea.Msg {
  324. var output string
  325. var left string
  326. output = output + "Issues in " + ic.Path + "...\n\n"
  327. for i, issue := range ic.Collection {
  328. // pointer render
  329. if i == ic.selection {
  330. left = left + pointerStyle.Render("-> ")
  331. } else {
  332. left = left + pointerStyle.Render(" ")
  333. }
  334. // index render
  335. left = left + "[" + indexStyle.Render(fmt.Sprintf("%d", i+1)) + "]: "
  336. // title render
  337. left = left + fmt.Sprintf("%s\n", titleStyle.Render(issue.Title))
  338. }
  339. output = output + collectionStyleLeft.Render(left)
  340. return output
  341. }
  342. // tea.Cmd definitions --------------------------------------------------------
  343. // ----------------------------------------------------------------------------
  344. // Handles load logic
  345. func (m Model) load() tea.Msg {
  346. if IsIssue(m.Path) {
  347. issue, err := Issue{}.NewFromPath(m.Path)
  348. if err != nil {
  349. return nil
  350. }
  351. return issue
  352. }
  353. if IsIssueCollection(m.Path) {
  354. collection, err := IssueCollection{}.NewFromPath(m.Path)
  355. if err != nil {
  356. return nil
  357. }
  358. return collection
  359. }
  360. return initialCreateIssueModel(m.Path, "lorem ipsum")
  361. }
  362. type createResult Issue
  363. // A widget for creating issues
  364. //
  365. // TODO implement description field in createIssue.create cmd
  366. func (ci createIssue) create() tea.Msg {
  367. data := make(map[string]string)
  368. commaSplit := func(t string) []string {
  369. s := strings.Split(t, ",")
  370. for i, v := range s {
  371. s[i] = strings.TrimLeft(v, " \t\n")
  372. s[i] = strings.TrimRight(s[i], " \t\n")
  373. s[i] = parseHumanToPath(s[i])
  374. }
  375. return s
  376. }
  377. for _, field := range ci.inputFields {
  378. data[field.title] = field.input.Value()
  379. }
  380. var newIssue = Issue{
  381. Path: ci.Path,
  382. Tags: VariadicField{Path: "/tags"},
  383. Blockedby: VariadicField{Path: "/blockedby"},
  384. }
  385. for key, value := range data {
  386. switch key {
  387. case "title":
  388. newIssue.Title = value
  389. case "status":
  390. newIssue.Status = Field{Path: "/status", Data: value}
  391. case "description":
  392. newIssue.Description = Field{Path: "/description", Data: value}
  393. case "tags":
  394. splitTags := commaSplit(value)
  395. for _, tag := range splitTags {
  396. newIssue.Tags.Fields = append(newIssue.Tags.Fields, Field{Path: tag})
  397. }
  398. case "blockers":
  399. splitBlockedby := commaSplit(value)
  400. for _, blocker := range splitBlockedby {
  401. newIssue.Blockedby.Fields = append(
  402. newIssue.Blockedby.Fields, Field{Path: blocker},
  403. )
  404. }
  405. }
  406. }
  407. return createResult(newIssue)
  408. }
  409. type writeResult any
  410. // Wraps a cmd func, passes an initialized Issue to WriteIssue()
  411. func (ci createIssue) write(issue Issue) tea.Cmd {
  412. return func() tea.Msg {
  413. result, err := WriteIssue(issue, false)
  414. if err != nil {
  415. return writeResult(err)
  416. }
  417. return writeResult(result)
  418. }
  419. }