language.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package plugin
  2. import (
  3. "fmt"
  4. tea "github.com/charmbracelet/bubbletea"
  5. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/constants"
  6. )
  7. var languages = []constants.Language{
  8. constants.Python,
  9. constants.Go + " (not supported yet)",
  10. }
  11. type language struct {
  12. cursor int
  13. }
  14. func newLanguage() language {
  15. return language{
  16. // default language is python
  17. cursor: 0,
  18. }
  19. }
  20. func (l language) Language() constants.Language {
  21. return languages[l.cursor]
  22. }
  23. func (l language) View() string {
  24. s := `Select the language you want to use for plugin development, and press ` + GREEN + `Enter` + RESET + ` to continue,
  25. BTW, you need Python 3.12+ to develop the Plugin if you choose Python.
  26. `
  27. for i, language := range languages {
  28. if i == l.cursor {
  29. s += fmt.Sprintf("\033[32m-> %s\033[0m\n", language)
  30. } else {
  31. s += fmt.Sprintf(" %s\n", language)
  32. }
  33. }
  34. return s
  35. }
  36. func (l language) Update(msg tea.Msg) (subMenu, subMenuEvent, tea.Cmd) {
  37. switch msg := msg.(type) {
  38. case tea.KeyMsg:
  39. switch msg.String() {
  40. case "ctrl+c", "q":
  41. return l, SUB_MENU_EVENT_NONE, tea.Quit
  42. case "j", "down":
  43. l.cursor++
  44. if l.cursor >= len(languages) {
  45. l.cursor = len(languages) - 1
  46. }
  47. case "k", "up":
  48. l.cursor--
  49. if l.cursor < 0 {
  50. l.cursor = 0
  51. }
  52. case "enter":
  53. if l.cursor != 0 {
  54. l.cursor = 0
  55. return l, SUB_MENU_EVENT_NONE, nil
  56. }
  57. return l, SUB_MENU_EVENT_NEXT, nil
  58. }
  59. }
  60. return l, SUB_MENU_EVENT_NONE, nil
  61. }
  62. func (l language) Init() tea.Cmd {
  63. return nil
  64. }