cmd/ez-agent-configurator/skills.go
package main
import (
"strings"
"github.com/rivo/tview"
"gopkg.in/yaml.v3"
)
type skillPathEntry struct {
Value string
Layer Layer
LocalIndex int
}
func skillPathsFromNode(node *yaml.Node) []string {
root := unwrapDoc(node)
if root == nil {
return nil
}
skills := getMapField(root, "skills")
if skills == nil || skills.Kind != yaml.MappingNode {
return nil
}
paths := getMapField(skills, "paths")
if paths == nil || paths.Kind != yaml.SequenceNode {
return nil
}
var out []string
for _, n := range paths.Content {
if n.Kind == yaml.ScalarNode {
out = append(out, n.Value)
}
}
return out
}
func (a *ConfiguratorApp) skillPathEntries() []skillPathEntry {
user := skillPathsFromNode(a.layers.UserNode)
project := skillPathsFromNode(a.layers.ProjectNode)
seen := map[string]bool{}
var out []skillPathEntry
for i, p := range user {
if !seen[p] {
seen[p] = true
out = append(out, skillPathEntry{p, LayerUser, i})
}
}
if a.layers.EditTarget == LayerProject {
for i, p := range project {
if !seen[p] {
seen[p] = true
out = append(out, skillPathEntry{p, LayerProject, i})
}
}
}
return out
}
func (a *ConfiguratorApp) localSkillPaths() []string {
if a.layers.EditTarget == LayerProject {
return skillPathsFromNode(a.layers.ProjectNode)
}
return skillPathsFromNode(a.layers.UserNode)
}
func (a *ConfiguratorApp) setLocalSkillPaths(paths []string) {
var doc **yaml.Node
if a.layers.EditTarget == LayerProject {
doc = &a.layers.ProjectNode
} else {
doc = &a.layers.UserNode
}
if *doc == nil {
*doc = &yaml.Node{Kind: yaml.DocumentNode, Content: []*yaml.Node{newMapNode()}}
}
root := unwrapDoc(*doc)
if len(paths) == 0 {
if skills := getMapField(root, "skills"); skills != nil && skills.Kind == yaml.MappingNode {
removeMapField(skills, "paths")
if len(skills.Content) == 0 {
removeMapField(root, "skills")
}
}
} else {
skills := ensureMapEntry(root, "skills")
seq := ensureSequenceField(skills, "paths")
seq.Content = nil
for _, p := range paths {
seq.Content = append(seq.Content, newScalarNode(p))
}
}
a.dirtyConfig = true
_ = a.layers.Recompute()
}
func (a *ConfiguratorApp) addSkillPath(value string) bool {
value = strings.TrimSpace(value)
if value == "" {
return false
}
paths := a.localSkillPaths()
a.setLocalSkillPaths(append(paths, value))
return true
}
func (a *ConfiguratorApp) editSkillPath(entry skillPathEntry, value string) bool {
value = strings.TrimSpace(value)
paths := a.localSkillPaths()
if entry.Layer != a.layers.EditTarget || value == "" || entry.LocalIndex < 0 || entry.LocalIndex >= len(paths) {
return false
}
paths[entry.LocalIndex] = value
a.setLocalSkillPaths(paths)
return true
}
func (a *ConfiguratorApp) moveSkillPath(entry skillPathEntry, delta int) bool {
paths := a.localSkillPaths()
to := entry.LocalIndex + delta
if entry.Layer != a.layers.EditTarget || entry.LocalIndex < 0 || entry.LocalIndex >= len(paths) || to < 0 || to >= len(paths) {
return false
}
paths[entry.LocalIndex], paths[to] = paths[to], paths[entry.LocalIndex]
a.setLocalSkillPaths(paths)
return true
}
func (a *ConfiguratorApp) deleteSkillPath(entry skillPathEntry) bool {
paths := a.localSkillPaths()
if entry.Layer != a.layers.EditTarget || entry.LocalIndex < 0 || entry.LocalIndex >= len(paths) {
return false
}
paths = append(paths[:entry.LocalIndex], paths[entry.LocalIndex+1:]...)
a.setLocalSkillPaths(paths)
return true
}
func (a *ConfiguratorApp) showSkillPaths() {
var view tview.Primitive
refresh := func() {}
build := func() tview.Primitive {
items := []listItem{}
for _, e := range a.skillPathEntries() {
e := e
label := e.Value + " [" + string(e.Layer) + "]"
local := e.Layer == a.layers.EditTarget
items = append(items, listItem{Label: label, Selectable: local, Color: layerColor(e.Layer), Action: func() {
if local {
a.showSkillPathEditor(e.LocalIndex, e.Value)
}
}})
}
items = append(items, listItem{Label: "── ── ── ── ── ── ──", Selectable: false}, listItem{Label: "+ Add Skill Path", Selectable: true, Color: "green", Action: func() { a.showSkillPathEditor(-1, "") }})
v := buildListView(" Skill Paths ", items, 72)
if l := extractList(v); l != nil {
setupListNav(l, a)
}
return v
}
view = build()
refresh = func() { a.popView(); a.showSkillPaths() }
_ = refresh
a.pushView("skill-paths", "Skill Paths", view, nil)
}
func (a *ConfiguratorApp) showSkillPathEditor(index int, initial string) {
value := initial
form := tview.NewForm().AddInputField("Path", initial, 60, nil, func(s string) { value = s })
entry := skillPathEntry{Value: initial, Layer: a.layers.EditTarget, LocalIndex: index}
form.AddButton("Save", func() {
ok := false
if index < 0 {
ok = a.addSkillPath(value)
} else {
ok = a.editSkillPath(entry, value)
}
if !ok {
a.setStatusErr("skill path cannot be empty")
return
}
a.popView()
a.popView()
a.showSkillPaths()
})
if index >= 0 {
form.AddButton("Move Up", func() {
a.moveSkillPath(entry, -1)
a.popView()
a.popView()
a.showSkillPaths()
})
form.AddButton("Move Down", func() {
a.moveSkillPath(entry, 1)
a.popView()
a.popView()
a.showSkillPaths()
})
form.AddButton("Delete", func() {
a.deleteSkillPath(entry)
a.popView()
a.popView()
a.showSkillPaths()
})
}
form.AddButton("Cancel", func() { a.popView() })
form.SetBorder(true).SetTitle(" Skill Path ")
a.pushView("skill-path-edit", "Skill Path", centerPrimitive(form, 76, 12), nil)
}