-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
618 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/langgenius/dify-plugin-daemon/internal/types/entities/constants" | ||
) | ||
|
||
var languages = []constants.Language{ | ||
constants.Python, | ||
constants.Go + " (not supported yet)", | ||
} | ||
|
||
type language struct { | ||
cursor int | ||
} | ||
|
||
func newLanguage() language { | ||
return language{ | ||
// default language is python | ||
cursor: 0, | ||
} | ||
} | ||
|
||
func (l language) Language() constants.Language { | ||
return languages[l.cursor] | ||
} | ||
|
||
func (l language) View() string { | ||
s := "Select the language you want to use for plugin development\n" | ||
for i, language := range languages { | ||
if i == l.cursor { | ||
s += fmt.Sprintf("-> %s\n", language) | ||
} else { | ||
s += fmt.Sprintf(" %s\n", language) | ||
} | ||
} | ||
return s | ||
} | ||
|
||
func (l language) Update(msg tea.Msg) (subMenu, subMenuEvent, tea.Cmd) { | ||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.String() { | ||
case "ctrl+c", "q": | ||
return l, SUB_MENU_EVENT_NONE, tea.Quit | ||
case "j", "down": | ||
l.cursor++ | ||
if l.cursor >= len(languages) { | ||
l.cursor = len(languages) - 1 | ||
} | ||
case "k", "up": | ||
l.cursor-- | ||
if l.cursor < 0 { | ||
l.cursor = 0 | ||
} | ||
case "enter": | ||
if l.cursor != 0 { | ||
return l, SUB_MENU_EVENT_NONE, nil | ||
} | ||
|
||
return l, SUB_MENU_EVENT_NEXT, nil | ||
} | ||
} | ||
|
||
return l, SUB_MENU_EVENT_NONE, nil | ||
} | ||
|
||
func (l language) Init() tea.Cmd { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
type subMenuKey string | ||
|
||
const ( | ||
SUB_MENU_KEY_PROFILE subMenuKey = "profile" | ||
SUB_MENU_KEY_LANGUAGE subMenuKey = "language" | ||
SUB_MENU_KEY_PERMISSION subMenuKey = "permission" | ||
) | ||
|
||
type model struct { | ||
subMenus map[subMenuKey]subMenu | ||
subMenuSeq []subMenuKey | ||
currentSubMenu subMenuKey | ||
} | ||
|
||
func initialize() model { | ||
m := model{} | ||
m.subMenus = map[subMenuKey]subMenu{ | ||
SUB_MENU_KEY_PROFILE: newProfile(), | ||
SUB_MENU_KEY_LANGUAGE: newLanguage(), | ||
SUB_MENU_KEY_PERMISSION: newPermission(), | ||
} | ||
m.currentSubMenu = SUB_MENU_KEY_PROFILE | ||
|
||
m.subMenuSeq = []subMenuKey{ | ||
SUB_MENU_KEY_PROFILE, | ||
SUB_MENU_KEY_LANGUAGE, | ||
SUB_MENU_KEY_PERMISSION, | ||
} | ||
|
||
return m | ||
} | ||
|
||
func (m model) Init() tea.Cmd { | ||
return nil | ||
} | ||
|
||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
currentSubMenu, event, cmd := m.subMenus[m.currentSubMenu].Update(msg) | ||
m.subMenus[m.currentSubMenu] = currentSubMenu | ||
|
||
switch event { | ||
case SUB_MENU_EVENT_NEXT: | ||
if m.currentSubMenu != m.subMenuSeq[len(m.subMenuSeq)-1] { | ||
// move the current sub menu to the next one | ||
for i, key := range m.subMenuSeq { | ||
if key == m.currentSubMenu { | ||
m.currentSubMenu = m.subMenuSeq[i+1] | ||
break | ||
} | ||
} | ||
} | ||
case SUB_MENU_EVENT_PREV: | ||
if m.currentSubMenu != m.subMenuSeq[0] { | ||
// move the current sub menu to the previous one | ||
for i, key := range m.subMenuSeq { | ||
if key == m.currentSubMenu { | ||
m.currentSubMenu = m.subMenuSeq[i-1] | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
return m, cmd | ||
} | ||
|
||
func (m model) View() string { | ||
return m.subMenus[m.currentSubMenu].View() | ||
} | ||
|
||
func main() { | ||
p := tea.NewProgram(initialize()) | ||
if _, err := p.Run(); err != nil { | ||
fmt.Println("Error running program:", err) | ||
} | ||
} |
Oops, something went wrong.