Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 62 additions & 39 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func sortedKeys[V any](m map[string]V) []string {
}

// digitSpan renders sorted ladder digits compactly: "1-5" when
// contiguous, else "1,3,7".
// contiguous, else "1,3,7". The byte arithmetic relies on every key
// being a single digit, which effortKeyRe in config.go enforces.
func digitSpan(digits []string) string {
contiguous := len(digits) > 1
for i := 1; i < len(digits); i++ {
Expand Down Expand Up @@ -74,23 +75,6 @@ func tokensOf(cfg Config) []string {
return tokens
}

// tokenSource reports where a token's effective definition comes from:
// "project" for profiles the project file declares and for grammar
// tokens of project-declared model letters, else "global".
func tokenSource(token string, projModels, projProfiles map[string]bool) string {
if projProfiles[token] {
return "project"
}
letters := token
if last := token[len(token)-1]; last >= '0' && last <= '9' {
letters = token[:len(token)-1]
}
if projModels[letters] {
return "project"
}
return "global"
}

// viewContext carries everything the list and show renderers need,
// assembled once per command so the renderers stay pure and testable.
type viewContext struct {
Expand All @@ -103,13 +87,41 @@ type viewContext struct {
projModels map[string]bool // model letters the project file declares
projProfiles map[string]bool // profile names the project file declares
globalTokens map[string]bool // tokens `clau link` would create (global set)
projEfforts bool // the project file declares a non-empty [efforts]
pal palette
}

func (vc viewContext) linked(token string) bool {
return isOwned(filepath.Join(vc.dir, linkFileName("c"+token, vc.goos)), vc.clauPath)
}

// tokenSource reports where a token's effective definition comes from:
// "project" for profiles the project file declares, for grammar tokens
// of project-declared model letters, and for every digit token when the
// project declares [efforts] (it replaces the whole ladder, so even a
// digit the global ladder also had is the project's), else "global".
// A profile the project does not declare is global whatever its name
// looks like: profiles beat the grammar in resolveToken, so the digit
// rules never apply to one.
func (vc viewContext) tokenSource(token string) string {
if vc.projProfiles[token] {
return "project"
}
if _, ok := vc.cfg.Profiles[token]; ok {
return "global"
}
letters := token
digit := false
if last := token[len(token)-1]; last >= '0' && last <= '9' {
letters = token[:len(token)-1]
digit = true
}
if vc.projModels[letters] || (digit && vc.projEfforts) {
return "project"
}
return "global"
}

// joinBadges dim-joins non-empty annotation parts with " · ".
func joinBadges(pal palette, parts ...string) string {
var kept []string
Expand Down Expand Up @@ -151,16 +163,24 @@ func profileCell(p Profile, pal palette) string {
}

// familyStates aggregates link state over a model letter's grammar
// family (the letter plus its effort digits): linkable when any family
// token is in the global set, linked when every family command exists.
// The digits come from the effective ladder, so a project [efforts]
// replacement leaves the badge conservative: it can warn about family
// commands the global-only `clau link` will never create.
func familyStates(letter string, spec ModelSpec, vc viewContext) (linkable, linked bool) {
// family as `clau link` manages it: the letter plus the digit tokens in
// the global set. Linkable when any of those is global, linked when
// every one has its command. The effective ladder is deliberately not
// consulted: link is global-only, so under a project [efforts]
// replacement it would warn forever about commands link never creates
// while missing the global ones it prunes. A project-only digit token
// shows up in the footer count and in `clau show`, not on the row.
// A profile named like a digit token (o5, o7) is skipped: it wins
// resolution, so its command is the profile row's to warn about.
func familyStates(letter string, vc viewContext) (linkable, linked bool) {
toks := []string{letter}
if spec.Efforts {
for _, d := range sortedKeys(vc.cfg.Efforts) {
toks = append(toks, letter+d)
for d := '1'; d <= '9'; d++ {
t := letter + string(d)
if _, isProfile := vc.cfg.Profiles[t]; isProfile {
continue
}
if vc.globalTokens[t] {
toks = append(toks, t)
}
}
linked = true
Expand Down Expand Up @@ -199,17 +219,20 @@ func renderSection(header string, rows []listRowV, pal palette) []string {
return lines
}

// footerLine summarizes link health: linkable commands are the global
// token set (what `clau link` manages); the rest are project-only.
// footerLine summarizes link health: linkable commands are the whole
// global token set (what `clau link` creates and prunes, whether or not
// the project layer still exposes them); effective tokens outside that
// set are project-only.
func footerLine(vc viewContext) string {
linkable, linked, projectOnly := 0, 0, 0
for t := range vc.globalTokens {
linkable++
if vc.linked(t) {
linked++
}
}
for _, t := range tokensOf(vc.cfg) {
if vc.globalTokens[t] {
linkable++
if vc.linked(t) {
linked++
}
} else {
if !vc.globalTokens[t] {
projectOnly++
}
}
Expand Down Expand Up @@ -246,7 +269,7 @@ func listView(vc viewContext) []string {
var mrows []listRowV
for _, letter := range sortedKeys(vc.cfg.Models) {
spec := vc.cfg.Models[letter]
linkable, linked := familyStates(letter, spec, vc)
linkable, linked := familyStates(letter, vc)
note := ""
if !spec.Efforts {
note = pal.dim + "no effort digits" + pal.reset
Expand Down Expand Up @@ -377,7 +400,7 @@ func showView(vc viewContext, arg string) ([]string, error) {
out = append(out, row("kind", kindLine(vc, token)))

src := "global (" + contractHome(vc.globalPath) + ")"
if tokenSource(token, vc.projModels, vc.projProfiles) == "project" {
if vc.tokenSource(token) == "project" {
src = "project (" + vc.st.Path + ")"
}
out = append(out, row("source", src))
Expand Down Expand Up @@ -483,7 +506,7 @@ func newViewContext(cfg Config, st ProjectStatus, dir string) viewContext {
if g, err := loadConfig(configPath()); err == nil {
globalCfg = g
}
vc.projModels, vc.projProfiles, _ = projectDeclarations(st.Path)
vc.projModels, vc.projProfiles, vc.projEfforts, _ = projectDeclarations(st.Path)
}
vc.globalTokens = map[string]bool{}
for _, t := range tokensOf(globalCfg) {
Expand Down Expand Up @@ -663,7 +686,7 @@ func projectFindings(st ProjectStatus, store map[string]string, corrupt bool) []
fs = append(fs, finding{"ok", "no project config in effect"})
case st.Applied:
fs = append(fs, finding{"ok", fmt.Sprintf("project config %s trusted and applied", st.Path)})
if models, profiles, err := projectDeclarations(st.Path); err == nil {
if models, profiles, _, err := projectDeclarations(st.Path); err == nil {
var parts []string
if len(models) > 0 {
keys := make([]string, 0, len(models))
Expand Down
Loading
Loading