Skip to content

Commit

Permalink
nix: complete devshells for given flake
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 22, 2024
1 parent 06b8b13 commit a51229f
Showing 1 changed file with 34 additions and 45 deletions.
79 changes: 34 additions & 45 deletions pkg/actions/tools/nix/develop.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,44 @@ type devShellsSchema struct {
} `json:"devShells"`
}

// ActionDevShells completes a flake and development shells from that flake
func ActionDevShells() carapace.Action {
return carapace.ActionMultiPartsN("#", 2, func(c carapace.Context) carapace.Action {
if len(c.Parts) == 0 {
return ActionFlakes()
// ActionDevShells completes development shells from given flake
func ActionDevShells(flake string) carapace.Action {
return carapace.ActionExecCommand("nix", "flake", "show", "--json", flake)(func(output []byte) carapace.Action {
devShells := &devShellsSchema{}
if err := json.Unmarshal(output, devShells); err != nil {
return carapace.ActionMessage(err.Error())
}

return carapace.ActionExecCommand("nix", "flake", "show", "--json", c.Parts[0])(func(output []byte) carapace.Action {
devShells := &devShellsSchema{}
if err := json.Unmarshal(output, devShells); err != nil {
return carapace.ActionMessage(err.Error())
return carapace.ActionMultiPartsN(".", 2, func(c carapace.Context) carapace.Action {
if len(c.Parts) == 0 {
currentSystem, err := getCurrentSystem(c)
if err != nil {
return carapace.ActionMessage(err.Error())
}

systems := make([]string, 0)
for system := range devShells.DevShells {
systems = append(systems, system)
}

return carapace.ActionValues(systems...).StyleF(func(s string, sc style.Context) string {
if s == currentSystem {
return style.Blue
}
return style.Default
}).Suffix(".")
}

vals := make([]string, 0)
currentSystem, ok := devShells.DevShells[c.Parts[0]]
if !ok {
return carapace.ActionValues()
}

return actionDevShellsAndSystem(c, devShells)
for name, value := range currentSystem {
vals = append(vals, name, value.Name)
}
return carapace.ActionValuesDescribed(vals...)
})
})
}
Expand All @@ -49,38 +73,3 @@ func getCurrentSystem(c carapace.Context) (string, error) {

return config.System.Value, nil
}

// Completes <system>.<devshell>
func actionDevShellsAndSystem(_ carapace.Context, devShells *devShellsSchema) carapace.Action {
return carapace.ActionMultiPartsN(".", 2, func(c carapace.Context) carapace.Action {
if len(c.Parts) == 0 {
currentSystem, err := getCurrentSystem(c)
if err != nil {
return carapace.ActionMessage(err.Error())
}

systems := make([]string, 0)
for system := range devShells.DevShells {
systems = append(systems, system)
}

return carapace.ActionValues(systems...).StyleF(func(s string, sc style.Context) string {
if s == currentSystem {
return style.Blue
}
return style.Default
}).Suffix(".")
}

vals := make([]string, 0)
currentSystem, ok := devShells.DevShells[c.Parts[0]]
if !ok {
return carapace.ActionValues()
}

for name, value := range currentSystem {
vals = append(vals, name, value.Name)
}
return carapace.ActionValuesDescribed(vals...)
})
}

0 comments on commit a51229f

Please sign in to comment.