tui.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. // interface for renderable structs
  44. type widget interface {
  45. render() tea.Msg
  46. }
  47. // The main bubbletea Model
  48. type Model struct {
  49. widget widget
  50. content string
  51. Path string
  52. // viewport viewport.Model
  53. }
  54. // Main model definitions -----------------------------------------------------
  55. // ----------------------------------------------------------------------------
  56. // The bubbletea init function
  57. func (m Model) Init() tea.Cmd { return m.load }
  58. // Handles quit logic and viewport scroll and size updates
  59. func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
  60. // widget specifc keyhandling
  61. var cmds []tea.Cmd
  62. switch m.widget.(type) {
  63. case IssueCollection:
  64. if msg, ok := msg.(tea.KeyMsg); ok {
  65. switch msg.String() {
  66. case "j":
  67. if collection, ok := m.widget.(IssueCollection); ok {
  68. if collection.selection+1 < len(collection.Collection) {
  69. collection.selection = collection.selection + 1
  70. } else {
  71. collection.selection = 0
  72. }
  73. m.widget = collection
  74. return m, collection.render
  75. }
  76. case "k":
  77. // do something only if widget is collection
  78. if collection, ok := m.widget.(IssueCollection); ok {
  79. if collection.selection != 0 {
  80. collection.selection = collection.selection - 1
  81. } else {
  82. collection.selection = len(collection.Collection) - 1
  83. }
  84. m.widget = collection
  85. return m, collection.render
  86. }
  87. case "enter":
  88. if _, ok := m.widget.(IssueCollection); ok {
  89. m.Path = m.widget.(IssueCollection).Collection[m.widget.(IssueCollection).selection].Path
  90. return m, m.load
  91. }
  92. case "q":
  93. cmds = append(cmds, tea.Quit)
  94. }
  95. }
  96. }
  97. // general message handling
  98. switch msg := msg.(type) {
  99. case tea.KeyMsg: // keymsg capture that is always present
  100. switch msg.String() {
  101. case "ctrl+c":
  102. cmds = append(cmds, tea.Quit)
  103. }
  104. case widget: // widget is initialized from m.load()
  105. switch T := msg.(type) {
  106. case createIssue:
  107. m.widget = T
  108. cmds = append(cmds, T.render, T.init())
  109. default:
  110. m.widget = T
  111. cmds = append(cmds, T.render)
  112. }
  113. case string:
  114. m.content = msg
  115. }
  116. // finally, handle input updates if any
  117. if w, ok := m.widget.(createIssue); ok {
  118. var cmd tea.Cmd
  119. m.widget, cmd = w.update(msg)
  120. cmds = append(cmds, cmd, w.render)
  121. }
  122. return m, tea.Batch(cmds...)
  123. }
  124. // Handles top level view functionality
  125. func (m Model) View() string {
  126. var output string
  127. if len(m.content) == 0 {
  128. return "loading..."
  129. } else {
  130. output = output + m.content
  131. }
  132. output = output + "\nj/k: down/up\tenter: select\tq: quit"
  133. return output
  134. }
  135. // WIDGET DEFINITIONS ---------------------------------------------------------
  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. // tea.Cmd definitions --------------------------------------------------------
  272. // ----------------------------------------------------------------------------
  273. // Handles load logic
  274. func (m Model) load() tea.Msg {
  275. if IsIssue(m.Path) {
  276. issue, err := Issue{}.NewFromPath(m.Path)
  277. if err != nil {
  278. return nil
  279. }
  280. return issue
  281. }
  282. if IsIssueCollection(m.Path) {
  283. collection, err := IssueCollection{}.NewFromPath(m.Path)
  284. if err != nil {
  285. return nil
  286. }
  287. return collection
  288. }
  289. return initialInputModel(m.Path, "lorem ipsum")
  290. }
  291. // Handles all render logic for Issue structs
  292. func (i Issue) render() tea.Msg {
  293. var output string
  294. // title
  295. output = output + titleStyle.Render(i.Title)
  296. // status
  297. output = output + fmt.Sprintf("\n%s", statusStyle.Render(i.Status.Data))
  298. // variadics
  299. var tags string
  300. for _, field := range i.Tags.Fields {
  301. tags = tags + field.Path + ", "
  302. }
  303. tags = strings.TrimRight(tags, ", ")
  304. var blockedby string
  305. for _, field := range i.Blockedby.Fields {
  306. blockedby = blockedby + field.Path + ", "
  307. }
  308. blockedby = strings.TrimRight(blockedby, ", ")
  309. if len(i.Tags.Fields) > 0 {
  310. output = output + variadicTitleStyle.Render("\n\nTags:")
  311. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(tags))
  312. }
  313. if len(i.Blockedby.Fields) > 0 {
  314. output = output + variadicTitleStyle.Render("\n\nBlockedby:")
  315. output = output + fmt.Sprintf("\n%s", variadicDataStyle.Render(blockedby))
  316. }
  317. // description
  318. output = output + titleStyle.Render("\n\nDescription:\n")
  319. output = output + fmt.Sprintf("\n%s", i.Description.Data)
  320. return borderStyle.Render(output)
  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. }