tui.go 13 KB

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