profile.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package main
  2. import (
  3. "fmt"
  4. ti "github.com/charmbracelet/bubbles/textinput"
  5. tea "github.com/charmbracelet/bubbletea"
  6. )
  7. type profile struct {
  8. cursor int
  9. inputs []ti.Model
  10. warning string
  11. }
  12. func newProfile() profile {
  13. name := ti.New()
  14. name.Placeholder = "Plugin name, a directory will be created with this name"
  15. name.CharLimit = 128
  16. name.Prompt = "Plugin name (press Enter to next step): "
  17. name.Focus()
  18. author := ti.New()
  19. author.Placeholder = "Author name"
  20. author.CharLimit = 128
  21. author.Prompt = "Author (press Enter to next step): "
  22. return profile{
  23. inputs: []ti.Model{name, author},
  24. }
  25. }
  26. func (p profile) Name() string {
  27. return p.inputs[0].Value()
  28. }
  29. func (p profile) Author() string {
  30. return p.inputs[1].Value()
  31. }
  32. func (p profile) View() string {
  33. s := fmt.Sprintf("Edit profile of the plugin\n%s\n%s\n", p.inputs[0].View(), p.inputs[1].View())
  34. if p.warning != "" {
  35. s += fmt.Sprintf("\033[31m%s\033[0m\n", p.warning)
  36. }
  37. return s
  38. }
  39. func (p *profile) checkEmpty() bool {
  40. if p.inputs[p.cursor].Value() == "" {
  41. p.warning = "Name and author cannot be empty"
  42. return false
  43. } else {
  44. p.warning = ""
  45. }
  46. return true
  47. }
  48. func (p profile) Update(msg tea.Msg) (subMenu, subMenuEvent, tea.Cmd) {
  49. var cmds []tea.Cmd
  50. switch msg := msg.(type) {
  51. case tea.KeyMsg:
  52. switch msg.String() {
  53. case "ctrl+c":
  54. return p, SUB_MENU_EVENT_NONE, tea.Quit
  55. case "down":
  56. // check if empty
  57. if !p.checkEmpty() {
  58. return p, SUB_MENU_EVENT_NONE, nil
  59. }
  60. // focus next
  61. p.cursor++
  62. if p.cursor >= len(p.inputs) {
  63. p.cursor = 0
  64. }
  65. case "up":
  66. if !p.checkEmpty() {
  67. return p, SUB_MENU_EVENT_NONE, nil
  68. }
  69. p.cursor--
  70. if p.cursor < 0 {
  71. p.cursor = len(p.inputs) - 1
  72. }
  73. case "enter":
  74. if !p.checkEmpty() {
  75. return p, SUB_MENU_EVENT_NONE, nil
  76. }
  77. // submit
  78. if p.cursor == len(p.inputs)-1 {
  79. return p, SUB_MENU_EVENT_NEXT, nil
  80. }
  81. // move to next
  82. p.cursor++
  83. }
  84. }
  85. // update cursor
  86. for i := 0; i < len(p.inputs); i++ {
  87. if i == p.cursor {
  88. p.inputs[i].Focus()
  89. } else {
  90. p.inputs[i].Blur()
  91. }
  92. }
  93. // update view
  94. for i := range p.inputs {
  95. var cmd tea.Cmd
  96. p.inputs[i], cmd = p.inputs[i].Update(msg)
  97. if cmd != nil {
  98. cmds = append(cmds, cmd)
  99. }
  100. }
  101. return p, SUB_MENU_EVENT_NONE, tea.Batch(cmds...)
  102. }
  103. func (p profile) Init() tea.Cmd {
  104. return nil
  105. }