tui.go 13 KB

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