profile.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package plugin
  2. import (
  3. "fmt"
  4. ti "github.com/charmbracelet/bubbles/textinput"
  5. tea "github.com/charmbracelet/bubbletea"
  6. "github.com/langgenius/dify-plugin-daemon/pkg/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 lowercase letters, numbers, dashes and underscores"
  53. return false
  54. } else if p.cursor == 1 && !plugin_entities.AuthorRegex.MatchString(p.inputs[p.cursor].Value()) {
  55. p.warning = "Author name must be 1-64 characters long, and can only contain lowercase letters, numbers, dashes and underscores"
  56. return false
  57. } else {
  58. p.warning = ""
  59. }
  60. return true
  61. }
  62. func (p profile) Update(msg tea.Msg) (subMenu, subMenuEvent, tea.Cmd) {
  63. var cmds []tea.Cmd
  64. switch msg := msg.(type) {
  65. case tea.KeyMsg:
  66. switch msg.String() {
  67. case "ctrl+c":
  68. return p, SUB_MENU_EVENT_NONE, tea.Quit
  69. case "down":
  70. // check if empty
  71. if !p.checkRule() {
  72. return p, SUB_MENU_EVENT_NONE, nil
  73. }
  74. // focus next
  75. p.cursor++
  76. if p.cursor >= len(p.inputs) {
  77. p.cursor = 0
  78. }
  79. case "up":
  80. if !p.checkRule() {
  81. return p, SUB_MENU_EVENT_NONE, nil
  82. }
  83. p.cursor--
  84. if p.cursor < 0 {
  85. p.cursor = len(p.inputs) - 1
  86. }
  87. case "enter":
  88. if !p.checkRule() {
  89. return p, SUB_MENU_EVENT_NONE, nil
  90. }
  91. // submit
  92. if p.cursor == len(p.inputs)-1 {
  93. return p, SUB_MENU_EVENT_NEXT, nil
  94. }
  95. // move to next
  96. p.cursor++
  97. }
  98. }
  99. // update cursor
  100. for i := 0; i < len(p.inputs); i++ {
  101. if i == p.cursor {
  102. p.inputs[i].Focus()
  103. } else {
  104. p.inputs[i].Blur()
  105. }
  106. }
  107. // update view
  108. for i := range p.inputs {
  109. var cmd tea.Cmd
  110. p.inputs[i], cmd = p.inputs[i].Update(msg)
  111. if cmd != nil {
  112. cmds = append(cmds, cmd)
  113. }
  114. }
  115. return p, SUB_MENU_EVENT_NONE, tea.Batch(cmds...)
  116. }
  117. func (p profile) Init() tea.Cmd {
  118. return nil
  119. }