Skip to content

Commit

Permalink
feat(option): respect option.min_score setting, add command line swit…
Browse files Browse the repository at this point in the history
…ch for it
  • Loading branch information
water-sucks committed Feb 7, 2025
1 parent c044fa4 commit 601a870
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
23 changes: 22 additions & 1 deletion cmd/option/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func OptionCommand() *cobra.Command {
if len(args) > 0 {
opts.OptionInput = args[0]
}

return nil
},
ValidArgsFunction: OptionsCompletionFunc(&opts),
Expand All @@ -53,6 +54,7 @@ func OptionCommand() *cobra.Command {
cmd.Flags().BoolVarP(&opts.DisplayJson, "json", "j", false, "Output information in JSON format")
cmd.Flags().BoolVarP(&opts.Interactive, "interactive", "i", false, "Show interactive search TUI for options")
cmd.Flags().BoolVarP(&opts.NoUseCache, "no-cache", "n", false, "Do not attempt to use prebuilt option cache")
cmd.Flags().Int64VarP(&opts.MinScore, "min-score", "s", 0, "")
cmd.Flags().BoolVarP(&opts.DisplayValueOnly, "value-only", "v", false, "Show only the selected option's value")

nixopts.AddIncludesNixOption(&cmd, &opts.NixPathIncludes)
Expand All @@ -73,6 +75,11 @@ func optionMain(cmd *cobra.Command, opts *cmdTypes.OptionOpts) error {
cfg := config.FromContext(cmd.Context())
s := system.NewLocalSystem()

minScore := cfg.Option.MinScore
if cmd.Flags().Changed("min-score") {
minScore = opts.MinScore
}

if !s.IsNixOS() {
msg := "this command is only supported on NixOS systems"
log.Error(msg)
Expand Down Expand Up @@ -148,6 +155,8 @@ func optionMain(cmd *cobra.Command, opts *cmdTypes.OptionOpts) error {
fuzzySearchResults = fuzzySearchResults[:10]
}

fuzzySearchResults = filterMinimumScoreMatches(fuzzySearchResults, int(minScore))

if opts.DisplayJson {
displayErrorJson(msg, fuzzySearchResults)
return err
Expand All @@ -157,7 +166,7 @@ func optionMain(cmd *cobra.Command, opts *cmdTypes.OptionOpts) error {
if len(fuzzySearchResults) > 0 {
log.Print("\nSome similar options were found:\n")
for _, v := range fuzzySearchResults {
log.Printf(" - %s %v\n", v.Str, v.Score)
log.Printf(" - %s\n", v.Str)
}
} else {
log.Print("\nTry refining your search query.\n")
Expand Down Expand Up @@ -345,3 +354,15 @@ func evaluateOptionValue(s system.CommandRunner, cfg configuration.Configuration
ErrorMessage: strings.TrimSpace(stderr.String()),
}
}

// Filter a sorted (descending) match list until a minimum score is reached.
// Return a slice of the original matches.
func filterMinimumScoreMatches(matches []fuzzy.Match, minScore int) []fuzzy.Match {
for i, v := range matches {
if v.Score < minScore {
return matches[:i]
}
}

return matches
}
1 change: 1 addition & 0 deletions internal/cmd/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ type OptionOpts struct {
DisplayJson bool
NoUseCache bool
DisplayValueOnly bool
MinScore int64
OptionInput string
}

Expand Down

0 comments on commit 601a870

Please sign in to comment.