From fd2c72292d878e4052bb0de8830c7d18d7c1fec1 Mon Sep 17 00:00:00 2001 From: Joe Schmitt Date: Fri, 13 Feb 2026 15:09:44 -0500 Subject: [PATCH] fix: make sort direction defaults context-sensitive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The --sort-direction flag now defaults to: - desc for priority (higher priority first) - asc for alpha and label (A→Z order) Previously all sort modes defaulted to desc, which felt backwards for alphabetical and label sorting. Co-Authored-By: Claude Sonnet 4.5 --- main.go | 9 ++++++++- main_test.go | 14 +++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 8396088..5164b81 100644 --- a/main.go +++ b/main.go @@ -58,7 +58,7 @@ type CLI struct { NoCache bool `help:"Skip cache, force fresh search"` ClearCache bool `help:"Clear cache and exit"` Sort string `help:"Sort order: alpha, priority, label (default: priority)" default:"priority" enum:"alpha,priority,label"` - SortDirection string `help:"Sort direction: asc, desc (default: desc)" default:"desc" enum:"asc,desc" name:"sort-direction"` + SortDirection string `help:"Sort direction: asc, desc (default: desc for priority, asc for alpha/label)" default:"" enum:",asc,desc" name:"sort-direction"` JSON bool `short:"j" help:"Output results in JSON format"` Verbose bool `short:"v" help:"Enable debug output"` Version bool `short:"V" help:"Show version"` @@ -123,6 +123,13 @@ func formatOutput(format string, values map[string]string) string { } func sortProjects(projects []discover.Project, sortBy, direction string, mapper *icons.Mapper) { + if direction == "" { + if sortBy == "priority" { + direction = "desc" + } else { + direction = "asc" + } + } desc := direction == "desc" sort.SliceStable(projects, func(i, j int) bool { switch sortBy { diff --git a/main_test.go b/main_test.go index f215b80..fea2c75 100644 --- a/main_test.go +++ b/main_test.go @@ -1725,7 +1725,7 @@ func TestCLI_SortAlpha(t *testing.T) { createTestProject(t, tmpDir, "z-project", "go.mod") createTestProject(t, tmpDir, "a-project", ".git/") - stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache", "--sort", "alpha", "--sort-direction", "asc") + stdout, stderr, err := runPJ(t, "-p", tmpDir, "--no-cache", "--sort", "alpha") if err != nil { t.Fatalf("pj failed: %v\nStderr: %s", err, stderr) } @@ -1786,15 +1786,15 @@ func TestCLI_SortLabel(t *testing.T) { t.Fatalf("Expected 3 projects, got %d", len(lines)) } - // Default direction is desc, so label desc: rust > go > git - if !strings.Contains(lines[0], "rust-project") { - t.Errorf("Label desc: first line should be rust-project, got: %s", lines[0]) + // Default direction is asc, so label asc: git < go < rust + if !strings.Contains(lines[0], "git-project") { + t.Errorf("Label asc: first line should be git-project, got: %s", lines[0]) } if !strings.Contains(lines[1], "go-project") { - t.Errorf("Label desc: second line should be go-project, got: %s", lines[1]) + t.Errorf("Label asc: second line should be go-project, got: %s", lines[1]) } - if !strings.Contains(lines[2], "git-project") { - t.Errorf("Label desc: third line should be git-project, got: %s", lines[2]) + if !strings.Contains(lines[2], "rust-project") { + t.Errorf("Label asc: third line should be rust-project, got: %s", lines[2]) } }