Skip to content

Commit

Permalink
feat: cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Sep 2, 2024
1 parent b8d1d63 commit 9253da2
Show file tree
Hide file tree
Showing 10 changed files with 618 additions and 34 deletions.
72 changes: 72 additions & 0 deletions cmd/commandline/language.go
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
}
84 changes: 84 additions & 0 deletions cmd/commandline/main.go
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)
}
}
Loading

0 comments on commit 9253da2

Please sign in to comment.