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
67 changes: 33 additions & 34 deletions internal/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"

"github.com/raksul/go-clickup/clickup"
"github.com/spf13/cobra"
"github.com/timimsms/cu/internal/api"
"github.com/timimsms/cu/internal/cache"
Expand All @@ -16,6 +17,36 @@ var (
isProjectFlag bool
)

// listRow is the table view of a clickup.List.
type listRow struct {
ID string `json:"id" yaml:"id"`
Name string `json:"name" yaml:"name"`
Default string `json:"default" yaml:"default"`
Tasks int `json:"tasks" yaml:"tasks"`
Archived bool `json:"archived" yaml:"archived"`
}

// listTableRows converts lists into table rows, marking the default list with "*".
func listTableRows(lists []clickup.List, defaultListID string) []listRow {
rows := make([]listRow, 0, len(lists))
for _, l := range lists {
defaultMarker := ""
if l.ID == defaultListID {
defaultMarker = "*"
}
// task_count may be absent; leave Tasks at zero in that case
tasks, _ := l.TaskCount.Int64()
rows = append(rows, listRow{
ID: l.ID,
Name: l.Name,
Default: defaultMarker,
Tasks: int(tasks),
Archived: l.Archived,
})
}
return rows
}

var listCmd = &cobra.Command{
Use: "list",
Short: "Manage lists",
Expand Down Expand Up @@ -54,7 +85,7 @@ var listListCmd = &cobra.Command{
os.Exit(1)
}

var allLists []interface{}
var allLists []clickup.List

if folderID != "" {
// Get lists from folder
Expand Down Expand Up @@ -109,39 +140,7 @@ var listListCmd = &cobra.Command{
format := cmd.Flag("output").Value.String()

if format == "table" {
// Prepare table data
type listRow struct {
ID string `json:"id"`
Name string `json:"name"`
Default string `json:"default"`
Tasks int `json:"tasks"`
Archived bool `json:"archived"`
}

var rows []listRow
for _, item := range allLists {
// Type assertion based on the actual type
switch v := item.(type) {
case map[string]interface{}:
id, _ := v["id"].(string)
name, _ := v["name"].(string)
archived, _ := v["archived"].(bool)

defaultMarker := ""
if id == defaultListID {
defaultMarker = "*"
}

row := listRow{
ID: id,
Name: name,
Default: defaultMarker,
Archived: archived,
}
rows = append(rows, row)
}
}

rows := listTableRows(allLists, defaultListID)
if err := output.Format(format, rows); err != nil {
fmt.Fprintf(os.Stderr, "Failed to format output: %v\n", err)
os.Exit(1)
Expand Down
32 changes: 32 additions & 0 deletions internal/cmd/list_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package cmd

import (
"bytes"
"encoding/json"
"strings"
"testing"

"github.com/raksul/go-clickup/clickup"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/timimsms/cu/internal/output"
)

func TestListCommands_Structure(t *testing.T) {
Expand Down Expand Up @@ -37,3 +43,29 @@ func TestListCommands_Structure(t *testing.T) {
assert.NotNil(t, cmd.Run)
})
}

// Regression test for #24: table output must contain a row per list.
func TestListTableRows(t *testing.T) {
lists := []clickup.List{
{ID: "901", Name: "Backlog", TaskCount: json.Number("12")},
{ID: "902", Name: "Sprint", Archived: true},
}

rows := listTableRows(lists, "902")
require.Len(t, rows, 2)
assert.Equal(t, listRow{ID: "901", Name: "Backlog", Tasks: 12}, rows[0])
assert.Equal(t, listRow{ID: "902", Name: "Sprint", Default: "*", Archived: true}, rows[1])

var buf bytes.Buffer
require.NoError(t, (&output.TableFormatter{Writer: &buf}).Format(rows))

lines := strings.Split(strings.TrimSpace(buf.String()), "\n")
require.Len(t, lines, 4, "header, separator and one line per list")
assert.Equal(t, []string{"id", "name", "default", "tasks", "archived"}, strings.Fields(lines[0]))
assert.Equal(t, []string{"901", "Backlog", "12", "false"}, strings.Fields(lines[2]))
assert.Equal(t, []string{"902", "Sprint", "*", "0", "true"}, strings.Fields(lines[3]))
}

func TestListTableRows_Empty(t *testing.T) {
assert.Empty(t, listTableRows(nil, ""))
}
Loading