c42a19f7d3
Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
191 lines
5.2 KiB
Go
191 lines
5.2 KiB
Go
package tui
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"reflect"
|
||
"sort"
|
||
"strings"
|
||
|
||
"github.com/charmbracelet/glamour"
|
||
glamansi "github.com/charmbracelet/glamour/ansi"
|
||
"github.com/charmbracelet/glamour/styles"
|
||
"github.com/charmbracelet/lipgloss"
|
||
"github.com/charmbracelet/x/ansi"
|
||
)
|
||
|
||
func documentKeys(values map[string]any) []string {
|
||
keys := make([]string, 0, len(values))
|
||
for key := range values {
|
||
keys = append(keys, key)
|
||
}
|
||
priority := map[string]int{"summary": 0, "result": 1, "report": 2, "diagnosis": 3, "accomplished": 4, "changes": 5, "artifacts": 6, "findings": 7, "evidence": 8, "acceptance_checks": 9, "reason": 10, "error": 11}
|
||
sort.Slice(keys, func(i, j int) bool {
|
||
a, ok := priority[keys[i]]
|
||
if !ok {
|
||
a = 20
|
||
}
|
||
b, ok := priority[keys[j]]
|
||
if !ok {
|
||
b = 20
|
||
}
|
||
if a != b {
|
||
return a < b
|
||
}
|
||
return keys[i] < keys[j]
|
||
})
|
||
return keys
|
||
}
|
||
|
||
func fieldLabel(key string) string {
|
||
label := strings.ReplaceAll(strings.ReplaceAll(key, "_", " "), "-", " ")
|
||
if label == "" {
|
||
return "Value"
|
||
}
|
||
runes := []rune(label)
|
||
runes[0] = []rune(strings.ToUpper(string(runes[0])))[0]
|
||
return string(runes)
|
||
}
|
||
|
||
// Use the terminal's inherited palette rather than a fixed dark Markdown theme.
|
||
// Rendering changes presentation only: the original output remains in the audit.
|
||
func resultMarkdown(text string, width int) []string {
|
||
text = ansi.Strip(text)
|
||
if len(text) > 64<<10 || !strings.ContainsAny(text, "#*_`[|>") {
|
||
return strings.Split(text, "\n")
|
||
}
|
||
style := styles.ASCIIStyleConfig
|
||
zero := uint(0)
|
||
bold, italic := true, true
|
||
style.Document.Margin = &zero
|
||
style.Document.BlockPrefix = ""
|
||
style.Document.BlockSuffix = ""
|
||
style.Heading.StylePrimitive = glamansi.StylePrimitive{Bold: &bold, BlockSuffix: "\n"}
|
||
for _, heading := range []*glamansi.StyleBlock{&style.H1, &style.H2, &style.H3, &style.H4, &style.H5, &style.H6} {
|
||
heading.Prefix = ""
|
||
heading.Suffix = ""
|
||
}
|
||
style.Strong = glamansi.StylePrimitive{Bold: &bold}
|
||
style.Emph = glamansi.StylePrimitive{Italic: &italic}
|
||
style.CodeBlock.Margin = &zero
|
||
renderer, err := glamour.NewTermRenderer(glamour.WithStyles(style), glamour.WithWordWrap(max(1, width)), glamour.WithPreservedNewLines())
|
||
if err != nil {
|
||
return strings.Split(text, "\n")
|
||
}
|
||
rendered, err := renderer.Render(text)
|
||
if err != nil {
|
||
return strings.Split(text, "\n")
|
||
}
|
||
return strings.Split(strings.Trim(rendered, "\n"), "\n")
|
||
}
|
||
|
||
func resultDocument(values map[string]any, width int) []string {
|
||
if len(values) == 0 {
|
||
return []string{"No output recorded yet."}
|
||
}
|
||
var lines []string
|
||
var render func(any, int)
|
||
render = func(value any, depth int) {
|
||
indent := strings.Repeat(" ", min(depth, 8))
|
||
if depth > 8 {
|
||
data, _ := json.Marshal(value)
|
||
lines = append(lines, indent+string(data))
|
||
return
|
||
}
|
||
switch value := value.(type) {
|
||
case string:
|
||
for _, line := range resultMarkdown(value, max(1, width-len(indent))) {
|
||
lines = append(lines, indent+line)
|
||
}
|
||
case map[string]any:
|
||
if len(value) == 0 {
|
||
lines = append(lines, indent+"(empty)")
|
||
return
|
||
}
|
||
for _, key := range documentKeys(value) {
|
||
label := lipgloss.NewStyle().Foreground(themeMuted).Bold(true).Render(fieldLabel(key))
|
||
lines = append(lines, indent+label)
|
||
render(value[key], depth+1)
|
||
lines = append(lines, "")
|
||
}
|
||
case nil:
|
||
lines = append(lines, indent+"(none)")
|
||
default:
|
||
rv := reflect.ValueOf(value)
|
||
if rv.Kind() == reflect.Slice || rv.Kind() == reflect.Array {
|
||
if rv.Len() == 0 {
|
||
lines = append(lines, indent+"(empty list)")
|
||
return
|
||
}
|
||
for i := 0; i < rv.Len(); i++ {
|
||
start := len(lines)
|
||
render(rv.Index(i).Interface(), depth+1)
|
||
if len(lines) > start {
|
||
lines[start] = indent + "• " + strings.TrimLeft(lines[start], " ")
|
||
}
|
||
}
|
||
} else {
|
||
lines = append(lines, indent+fmt.Sprint(value))
|
||
}
|
||
}
|
||
}
|
||
for i, key := range documentKeys(values) {
|
||
if i > 0 {
|
||
lines = append(lines, "")
|
||
}
|
||
if !(i == 0 && (key == "summary" || key == "result" || key == "report")) {
|
||
lines = append(lines, lipgloss.NewStyle().Foreground(themeMuted).Bold(true).Render(fieldLabel(key)))
|
||
}
|
||
render(values[key], 0)
|
||
}
|
||
return lines
|
||
}
|
||
|
||
func (m Model) runVisitTabs(width int) string {
|
||
if m.runDetail == nil {
|
||
return ""
|
||
}
|
||
labels := []string{"Overview"}
|
||
for _, step := range m.runDetail.Steps {
|
||
marker := strings.Fields(ansi.Strip(m.progress(m.stepProgressStatus(step))))
|
||
status := ""
|
||
if len(marker) > 0 {
|
||
status = " " + marker[0]
|
||
}
|
||
labels = append(labels, fmt.Sprintf("%d %s%s", step.Sequence, step.Stage, status))
|
||
}
|
||
index := max(0, min(m.runStepIndex, len(labels)-1))
|
||
selected := action(ansi.Truncate(labels[index], max(1, width-6), "…"), true)
|
||
left, right := index, index
|
||
text := selected
|
||
for {
|
||
changed := false
|
||
if left > 0 {
|
||
prior := action(labels[left-1], false) + " "
|
||
if ansi.StringWidth(prior+text)+4 <= width {
|
||
text = prior + text
|
||
left--
|
||
changed = true
|
||
}
|
||
}
|
||
if right+1 < len(labels) {
|
||
next := " " + action(labels[right+1], false)
|
||
if ansi.StringWidth(text+next)+4 <= width {
|
||
text += next
|
||
right++
|
||
changed = true
|
||
}
|
||
}
|
||
if !changed {
|
||
break
|
||
}
|
||
}
|
||
if left > 0 {
|
||
text = "‹ " + text
|
||
}
|
||
if right+1 < len(labels) {
|
||
text += " ›"
|
||
}
|
||
return ansi.Truncate(text, max(1, width), "…")
|
||
}
|