language.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package init
  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\n"
  25. for i, language := range languages {
  26. if i == l.cursor {
  27. s += fmt.Sprintf("\033[32m-> %s\033[0m\n", language)
  28. } else {
  29. s += fmt.Sprintf(" %s\n", language)
  30. }
  31. }
  32. return s
  33. }
  34. func (l language) Update(msg tea.Msg) (subMenu, subMenuEvent, tea.Cmd) {
  35. switch msg := msg.(type) {
  36. case tea.KeyMsg:
  37. switch msg.String() {
  38. case "ctrl+c", "q":
  39. return l, SUB_MENU_EVENT_NONE, tea.Quit
  40. case "j", "down":
  41. l.cursor++
  42. if l.cursor >= len(languages) {
  43. l.cursor = len(languages) - 1
  44. }
  45. case "k", "up":
  46. l.cursor--
  47. if l.cursor < 0 {
  48. l.cursor = 0
  49. }
  50. case "enter":
  51. if l.cursor != 0 {
  52. l.cursor = 0
  53. return l, SUB_MENU_EVENT_NONE, nil
  54. }
  55. return l, SUB_MENU_EVENT_NEXT, nil
  56. }
  57. }
  58. return l, SUB_MENU_EVENT_NONE, nil
  59. }
  60. func (l language) Init() tea.Cmd {
  61. return nil
  62. }