From f5b300aa4cc9a3e135037307578bd40a5b9ed499 Mon Sep 17 00:00:00 2001 From: obvionaoe Date: Thu, 2 May 2024 16:48:20 +0100 Subject: [PATCH] implement sorting --- cmd/root.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index a8c86f2..050615b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -22,15 +22,18 @@ THE SOFTWARE. package cmd import ( + "cmp" "github.com/spf13/cobra" "log" "os" + "slices" + "strings" ) var ( - browser string - sort bool - rofiCmd string + browser string + sortProfiles bool + rofiCmd string ) // rootCmd represents the base command when called without any subcommands @@ -60,10 +63,10 @@ func init() { "The browser to use", ) rootCmd.Flags().BoolVarP( - &sort, + &sortProfiles, "sort", "s", - true, + false, "Sort the profiles alphabetically", ) rootCmd.Flags().StringVarP( @@ -93,6 +96,12 @@ func run(cmd *cobra.Command, args []string) { log.Fatal(err) } + if sortProfiles { + slices.SortFunc(profiles, func(a, b string) int { + return cmp.Compare(strings.ToLower(a), strings.ToLower(b)) + }) + } + selectedProfile, err := runRofi(profiles) if err != nil { log.Fatal(err)