profile.go 2.9 KB

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