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
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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])
}
}

Expand Down
Loading