tui.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. "path/filepath"
  12. "strings"
  13. "github.com/charmbracelet/bubbles/textinput"
  14. tea "github.com/charmbracelet/bubbletea"
  15. "github.com/charmbracelet/lipgloss"
  16. )
  17. // Type and Style definitions -------------------------------------------------
  18. // ----------------------------------------------------------------------------
  19. // [lipgloss] style definitions, stores the currently displayed "widget"
  20. var (
  21. titleStyle = lipgloss.NewStyle().
  22. Bold(true).
  23. Underline(true)
  24. statusStyle = lipgloss.NewStyle().
  25. Faint(true).
  26. Italic(true)
  27. variadicTitleStyle = lipgloss.NewStyle().
  28. Align(lipgloss.Left).
  29. Italic(true)
  30. variadicDataStyle = lipgloss.NewStyle().
  31. Width(40).
  32. BorderStyle(lipgloss.ASCIIBorder())
  33. borderStyle = lipgloss.NewStyle().
  34. Padding(1, 2).
  35. Margin(1).
  36. BorderStyle(lipgloss.NormalBorder())
  37. indexStyle = lipgloss.NewStyle().
  38. Italic(true)
  39. pointerStyle = lipgloss.NewStyle().
  40. Faint(true)
  41. collectionStyleLeft = lipgloss.NewStyle().
  42. Align(lipgloss.Left)
  43. )
  44. // MAIN MODEL DEFINITIONS -----------------------------------------------------
  45. // ----------------------------------------------------------------------------
  46. // The main bubbletea Model
  47. type Model struct {
  48. widget widget
  49. content string
  50. Path string
  51. // viewport viewport.Model
  52. }
  53. // The bubbletea init function
  54. func (m Model) Init() tea.Cmd { return m.load }
  55. // Handles quit logic and viewport scroll and size updates
  56. func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  57. // widget specifc keyhandling
  58. var cmds []tea.Cmd
  59. switch m.widget.(type) {
  60. case IssueCollection: // TODO handle updates to IssueCollection widgets in its own update func
  61. if msg, ok := msg.(tea.KeyMsg); ok {
  62. switch msg.String() {
  63. case "j":
  64. if collection, ok := m.widget.(IssueCollection); ok {
  65. if collection.selection+1 < len(collection.Collection) {
  66. collection.selection = collection.selection + 1
  67. } else {
  68. collection.selection = 0
  69. }
  70. m.widget = collection
  71. return m, collection.render
  72. }
  73. case "k":
  74. // do something only if widget is collection
  75. if collection, ok := m.widget.(IssueCollection); ok {
  76. if collection.selection != 0 {
  77. collection.selection = collection.selection - 1
  78. } else {
  79. collection.selection = len(collection.Collection) - 1
  80. }
  81. m.widget = collection
  82. return m, collection.render
  83. }
  84. case "enter":
  85. if _, ok := m.widget.(IssueCollection); ok {
  86. m.Path = m.widget.(IssueCollection).Collection[m.widget.(IssueCollection).selection].Path
  87. return m, m.load
  88. }
  89. case "q":
  90. cmds = append(cmds, tea.Quit)
  91. }
  92. }
  93. }
  94. // general message handling
  95. switch msg := msg.(type) {
  96. case tea.KeyMsg: // keymsg capture that is always present
  97. switch msg.String() {
  98. case "ctrl+c":
  99. cmds = append(cmds, tea.Quit)
  100. }
  101. case widget: // widget is initialized from m.load()
  102. switch T := msg.(type) {
  103. case create:
  104. m.widget = T
  105. cmds = append(cmds, T.render, T.init())
  106. default:
  107. m.widget = T
  108. cmds = append(cmds, T.render)
  109. }
  110. case string:
  111. m.content = msg
  112. }
  113. // finally, handle input updates if any
  114. if w, ok := m.widget.(create); ok {
  115. var cmd tea.Cmd
  116. m.widget, cmd = w.update(msg)
  117. cmds = append(cmds, cmd, w.render)
  118. }
  119. return m, tea.Batch(cmds...)
  120. }
  121. // Handles top level view functionality
  122. func (m Model) View() string {
  123. var output string
  124. if len(m.content) == 0 {
  125. return "loading..."
  126. } else {
  127. output = output + m.content
  128. }
  129. output = output + "\nj/k: down/up\tenter: select\tq: quit"
  130. return output
  131. }
  132. // WIDGET DEFINITIONS ---------------------------------------------------------
  133. // ----------------------------------------------------------------------------
  134. // TODO add keyhelp func to widget interface
  135. // interface definition for widgets
  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. // TODO invoke editor for descriptions
  147. // TODO handle reset on esc
  148. // widget for creating an issue
  149. type create struct {
  150. inputFields []inputField
  151. Path string
  152. selected int
  153. Err error // not implemented
  154. }
  155. // constructor for createIssue widget
  156. func initialCreateModel(path string, placeholder string) create {
  157. spawnInput := func(f bool) textinput.Model {
  158. ti := textinput.New()
  159. ti.Placeholder = placeholder
  160. if f {
  161. ti.Focus()
  162. }
  163. ti.CharLimit = 80
  164. ti.Width = 30
  165. return ti
  166. }
  167. var inputs []inputField
  168. for i, t := range [4]string{"title", "status", "tags", "blockers"} {
  169. if i == 0 {
  170. inputs = append(inputs, inputField{title: t, input: spawnInput(true)})
  171. } else {
  172. inputs = append(inputs, inputField{title: t, input: spawnInput(false)})
  173. }
  174. switch t {
  175. case "title":
  176. parsed := parsePathToHuman(path)
  177. if parsed == "." {
  178. parsed = ""
  179. }
  180. inputs[i].input.SetValue(parsed)
  181. case "status":
  182. inputs[i].input.SetValue("open")
  183. }
  184. }
  185. return create{
  186. inputFields: inputs,
  187. Path: path,
  188. selected: 0,
  189. Err: nil,
  190. }
  191. }
  192. func (c create) init() tea.Cmd {
  193. return textinput.Blink
  194. }
  195. func (c create) update(msg tea.Msg) (create, tea.Cmd) {
  196. var cmds []tea.Cmd
  197. var cmd tea.Cmd
  198. // simple anon funcs to increment the selected index
  199. incrementSelected := func() {
  200. if c.selected < len(c.inputFields) {
  201. c.selected++
  202. for i := 0; i < len(c.inputFields); i++ {
  203. if i == c.selected {
  204. c.inputFields[i].input.Focus()
  205. } else {
  206. c.inputFields[i].input.Blur()
  207. }
  208. }
  209. } else {
  210. c.selected = 0
  211. c.inputFields[c.selected].input.Focus()
  212. }
  213. }
  214. decrementSelected := func() {
  215. if c.selected != 0 {
  216. c.selected--
  217. for i := 0; i < len(c.inputFields); i++ {
  218. if i == c.selected {
  219. c.inputFields[i].input.Focus()
  220. } else {
  221. c.inputFields[i].input.Blur()
  222. }
  223. }
  224. } else {
  225. for i := 0; i < len(c.inputFields); i++ {
  226. c.inputFields[i].input.Blur()
  227. }
  228. c.selected = len(c.inputFields)
  229. }
  230. }
  231. switch msg := msg.(type) { // keybinding handler
  232. case tea.KeyMsg:
  233. switch msg.String() {
  234. case "tab":
  235. incrementSelected()
  236. case "shift+tab":
  237. decrementSelected()
  238. case "enter":
  239. if c.selected == len(c.inputFields) { // confirm create
  240. c.selected++
  241. } else if c.selected == len(c.inputFields)+1 { // confirmed
  242. cmds = append(cmds, c.create)
  243. } else {
  244. incrementSelected()
  245. }
  246. case "esc": // cancel
  247. cmds = append(cmds, tea.Quit)
  248. }
  249. case createResult:
  250. cmds = append(cmds, c.write(Issue(msg)))
  251. case writeResult:
  252. switch value := msg.(type) {
  253. case bool:
  254. if !value {
  255. } else {
  256. cmds = append(cmds, tea.Quit)
  257. }
  258. case error:
  259. panic(value)
  260. }
  261. }
  262. for i, ti := range c.inputFields {
  263. c.inputFields[i].input, cmd = ti.input.Update(msg)
  264. cmds = append(cmds, cmd)
  265. }
  266. cmds = append(cmds, cmd)
  267. return c, tea.Batch(cmds...)
  268. }
  269. func (c create) render() tea.Msg {
  270. borderStyle := lipgloss.NewStyle().
  271. BorderStyle(lipgloss.NormalBorder()).
  272. Margin(1).
  273. Padding(0, 1)
  274. ulStyle := lipgloss.NewStyle().Underline(true)
  275. var output string
  276. for _, field := range c.inputFields {
  277. output = output + fmt.Sprintf(
  278. "\n%s:%s",
  279. field.title,
  280. borderStyle.Render(field.input.View()),
  281. )
  282. }
  283. output = strings.TrimLeft(output, "\n")
  284. if c.selected < len(c.inputFields) {
  285. output = output + borderStyle.Render("press enter to submit...")
  286. } else if c.selected == len(c.inputFields) {
  287. output = output + borderStyle.Render(ulStyle.Render("press enter to submit..."))
  288. } else if c.selected == len(c.inputFields)+1 {
  289. confirmPrompt := fmt.Sprintf(
  290. "create issue titled \"%s\"?\n\n%s",
  291. ulStyle.Render(c.inputFields[0].input.Value()),
  292. ulStyle.Render("press enter to confirm..."),
  293. )
  294. output = output + borderStyle.Render(confirmPrompt)
  295. }
  296. return output
  297. }
  298. // -------- Issue widget definitions ------------------------------------------
  299. // ----------------------------------------------------------------------------
  300. // Handles all render logic for Issue structs
  301. func (i Issue) render() tea.Msg {
  302. var output string
  303. // title
  304. output = output + titleStyle.Render(i.Title)
  305. // status
  306. output = output + fmt.Sprintf("\n%s", statusStyle.Render(i.Status.Data))
  307. // variadics
  308. var tags string
  309. for _, field := range i.Tags.Fields {
  310. tags = tags + field.Path + ", "
  311. }
  312. tags = strings.TrimRight(tags, ", ")
  313. var blockedby string
  314. for _, field := range i.Blockedby.Fields {
  315. blockedby = blockedby + field.Path + ", "
  316. }
  317. blockedby = strings.TrimRight(blockedby, ", ")
  318. if len(i.Tags.Fields) > 0 {
  319. output = output + variadicTitleStyle.Render("\n\nTags:")
  320. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(tags))
  321. }
  322. if len(i.Blockedby.Fields) > 0 {
  323. output = output + variadicTitleStyle.Render("\n\nBlockedby:")
  324. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(blockedby))
  325. }
  326. // description
  327. output = output + titleStyle.Render("\n\nDescription:\n")
  328. output = output + fmt.Sprintf("\n%s", i.Description.Data)
  329. return borderStyle.Render(output)
  330. }
  331. // -------- IssueCollection widget definitions --------------------------------
  332. // ----------------------------------------------------------------------------
  333. // Handles all render logic for IssueCollection structs.
  334. func (ic IssueCollection) render() tea.Msg {
  335. var output string
  336. var left string
  337. output = output + "Issues in " + ic.Path + "...\n\n"
  338. for i, issue := range ic.Collection {
  339. // pointer render
  340. if i == ic.selection {
  341. left = left + pointerStyle.Render("-> ")
  342. } else {
  343. left = left + pointerStyle.Render(" ")
  344. }
  345. // index render
  346. left = left + "[" + indexStyle.Render(fmt.Sprintf("%d", i+1)) + "]: "
  347. // title render
  348. left = left + fmt.Sprintf("%s\n", titleStyle.Render(issue.Title))
  349. }
  350. output = output + collectionStyleLeft.Render(left)
  351. return output
  352. }
  353. // tea.Cmd definitions --------------------------------------------------------
  354. // ----------------------------------------------------------------------------
  355. // Handles load logic
  356. func (m Model) load() tea.Msg {
  357. if IsIssue(m.Path) {
  358. issue, err := Issue{}.NewFromPath(m.Path)
  359. if err != nil {
  360. return nil
  361. }
  362. return issue
  363. }
  364. if IsIssueCollection(m.Path) {
  365. collection, err := IssueCollection{}.NewFromPath(m.Path)
  366. if err != nil {
  367. return nil
  368. }
  369. return collection
  370. }
  371. return initialCreateModel(m.Path, "lorem ipsum")
  372. }
  373. type createResult Issue
  374. // TODO implement description field in createIssue.create cmd
  375. // A widget for creating issues
  376. func (c create) create() tea.Msg {
  377. data := make(map[string]string)
  378. commaSplit := func(t string) []string {
  379. s := strings.Split(t, ",")
  380. for i, v := range s {
  381. s[i] = strings.TrimLeft(v, " \t\n")
  382. s[i] = strings.TrimRight(s[i], " \t\n")
  383. s[i] = parseHumanToPath(s[i])
  384. }
  385. return s
  386. }
  387. for _, field := range c.inputFields {
  388. data[field.title] = field.input.Value()
  389. }
  390. var newIssue = Issue{
  391. Path: c.Path,
  392. Tags: VariadicField{Path: "/tags"},
  393. Blockedby: VariadicField{Path: "/blockedby"},
  394. }
  395. for key, value := range data {
  396. switch key {
  397. case "title":
  398. newIssue.Title = value
  399. if parsePathToHuman(newIssue.Path) != value {
  400. dir, _ := filepath.Split(newIssue.Path)
  401. newIssue.Path = filepath.Join(dir, value)
  402. }
  403. case "status":
  404. newIssue.Status = Field{Path: "/status", Data: value}
  405. case "description":
  406. newIssue.Description = Field{Path: "/description", Data: value}
  407. case "tags":
  408. splitTags := commaSplit(value)
  409. for _, tag := range splitTags {
  410. newIssue.Tags.Fields = append(newIssue.Tags.Fields, Field{Path: tag})
  411. }
  412. case "blockers":
  413. splitBlockedby := commaSplit(value)
  414. for _, blocker := range splitBlockedby {
  415. newIssue.Blockedby.Fields = append(
  416. newIssue.Blockedby.Fields, Field{Path: blocker},
  417. )
  418. }
  419. }
  420. }
  421. return createResult(newIssue)
  422. }
  423. type writeResult any
  424. // Wraps a cmd func, passes an initialized Issue to WriteIssue()
  425. func (c create) write(issue Issue) tea.Cmd {
  426. return func() tea.Msg {
  427. result, err := WriteIssue(issue, false)
  428. if err != nil {
  429. return writeResult(err)
  430. }
  431. return writeResult(result)
  432. }
  433. }