diff --git a/.goreleaser.yaml b/.goreleaser.yaml index c34eb23..3791670 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -16,9 +16,9 @@ before: - go mod download builds: - - id: algorithmvisualizer - binary: algorithmvisualizer - main: ./cmd/algorithmvisualizer + - id: algvis + binary: algvis + main: ./cmd/algvis env: - CGO_ENABLED=0 flags: diff --git a/Makefile b/Makefile index e57f696..0eaf01a 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # Build into bin/ (gitignored) so the binary never collides with the algorithmvisualizer/ # source package at the repo root. -BINARY := bin/algorithmvisualizer -PKG := ./cmd/algorithmvisualizer +BINARY := bin/algvis +PKG := ./cmd/algvis VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo dev) COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo none) DATE := $(shell date -u +%Y-%m-%dT%H:%M:%SZ) diff --git a/algorithmvisualizer/algorithmvisualizer.go b/algorithmvisualizer/algorithmvisualizer.go index 553b85e..bd43651 100644 --- a/algorithmvisualizer/algorithmvisualizer.go +++ b/algorithmvisualizer/algorithmvisualizer.go @@ -1,52 +1,86 @@ -// Package algorithmvisualizer is the library behind the algorithmvisualizer command line: -// the HTTP client, request shaping, and the typed data models for algorithmvisualizer. +// Package algorithmvisualizer is the library behind the algvis command line: +// the HTTP client, request shaping, and the typed data models for Algorithm Visualizer. // // The Client here is the spine every command shares. It sets a real // User-Agent, paces requests so a busy session stays polite, and retries the -// transient failures (429 and 5xx) that any public site throws under load. -// Build your endpoint calls and JSON decoding on top of it. +// transient failures (429 and 5xx) that any public API throws under load. package algorithmvisualizer import ( "context" + "encoding/json" "fmt" "io" "net/http" + "strings" + "sync" "time" ) -// DefaultUserAgent identifies the client to algorithmvisualizer. A real, honest -// User-Agent is both polite and the thing most likely to keep you unblocked. -const DefaultUserAgent = "algorithmvisualizer/dev (+https://github.com/tamnd/algorithmvisualizer-cli)" +const DefaultUserAgent = "algvis/dev (+https://github.com/tamnd/algorithmvisualizer-cli)" -// Client talks to algorithmvisualizer over HTTP. -type Client struct { - HTTP *http.Client +// Config holds constructor parameters. +type Config struct { + BaseURL string UserAgent string - // Rate is the minimum gap between requests. Zero means no pacing. - Rate time.Duration - Retries int + Rate time.Duration + Retries int + Timeout time.Duration +} + +// DefaultConfig returns sensible defaults. +func DefaultConfig() Config { + return Config{ + BaseURL: "https://api.github.com", + UserAgent: DefaultUserAgent, + Rate: 500 * time.Millisecond, + Retries: 3, + Timeout: 30 * time.Second, + } +} + +// Algorithm is a single algorithm entry. +type Algorithm struct { + Rank int `json:"rank"` + Category string `json:"category"` + Name string `json:"name"` + URL string `json:"url"` +} + +// Category is a group of algorithms. +type Category struct { + Rank int `json:"rank"` + Name string `json:"name"` + Count int `json:"count"` +} + +// treeResponse is the GitHub Git Trees API response shape. +type treeResponse struct { + Tree []struct { + Path string `json:"path"` + Type string `json:"type"` + } `json:"tree"` +} - last time.Time +// Client talks to the GitHub API for Algorithm Visualizer data. +type Client struct { + cfg Config + httpClient *http.Client + mu sync.Mutex + last time.Time } -// NewClient returns a Client with sensible defaults: a 30s timeout, a 200ms -// minimum gap between requests, and five retries on transient errors. -func NewClient() *Client { +// NewClient returns a Client with the given config. +func NewClient(cfg Config) *Client { return &Client{ - HTTP: &http.Client{Timeout: 30 * time.Second}, - UserAgent: DefaultUserAgent, - Rate: 200 * time.Millisecond, - Retries: 5, + cfg: cfg, + httpClient: &http.Client{Timeout: cfg.Timeout}, } } -// Get fetches url and returns the response body. It paces and retries according -// to the client's settings. The caller owns nothing extra; the body is read -// fully and closed here. -func (c *Client) Get(ctx context.Context, url string) ([]byte, error) { +func (c *Client) get(ctx context.Context, rawURL string) ([]byte, error) { var lastErr error - for attempt := 0; attempt <= c.Retries; attempt++ { + for attempt := 0; attempt <= c.cfg.Retries; attempt++ { if attempt > 0 { select { case <-ctx.Done(): @@ -54,7 +88,7 @@ func (c *Client) Get(ctx context.Context, url string) ([]byte, error) { case <-time.After(backoff(attempt)): } } - body, retry, err := c.do(ctx, url) + body, retry, err := c.do(ctx, rawURL) if err == nil { return body, nil } @@ -63,18 +97,19 @@ func (c *Client) Get(ctx context.Context, url string) ([]byte, error) { return nil, err } } - return nil, fmt.Errorf("get %s: %w", url, lastErr) + return nil, fmt.Errorf("get %s: %w", rawURL, lastErr) } -func (c *Client) do(ctx context.Context, url string) (body []byte, retry bool, err error) { +func (c *Client) do(ctx context.Context, rawURL string) (body []byte, retry bool, err error) { c.pace() - req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil) if err != nil { return nil, false, err } - req.Header.Set("User-Agent", c.UserAgent) + req.Header.Set("User-Agent", c.cfg.UserAgent) + req.Header.Set("Accept", "application/vnd.github+json") - resp, err := c.HTTP.Do(req) + resp, err := c.httpClient.Do(req) if err != nil { return nil, true, err } @@ -94,12 +129,13 @@ func (c *Client) do(ctx context.Context, url string) (body []byte, retry bool, e return b, false, nil } -// pace blocks until at least Rate has passed since the previous request. func (c *Client) pace() { - if c.Rate <= 0 { + c.mu.Lock() + defer c.mu.Unlock() + if c.cfg.Rate <= 0 { return } - if wait := c.Rate - time.Since(c.last); wait > 0 { + if wait := c.cfg.Rate - time.Since(c.last); wait > 0 { time.Sleep(wait) } c.last = time.Now() @@ -112,3 +148,108 @@ func backoff(attempt int) time.Duration { } return d } + +// fetchTree fetches the algorithm tree from GitHub and returns all algorithms. +func (c *Client) fetchTree(ctx context.Context) ([]Algorithm, error) { + raw, err := c.get(ctx, c.cfg.BaseURL+"/repos/algorithm-visualizer/algorithms/git/trees/master?recursive=1") + if err != nil { + return nil, err + } + + var resp treeResponse + if err := json.Unmarshal(raw, &resp); err != nil { + return nil, err + } + + var out []Algorithm + rank := 0 + for _, item := range resp.Tree { + if item.Type != "tree" { + continue + } + parts := strings.SplitN(item.Path, "/", 2) + if len(parts) != 2 { + continue + } + cat, name := parts[0], parts[1] + if strings.HasPrefix(cat, ".") { + continue + } + if strings.Contains(name, "/") { + continue + } + rank++ + out = append(out, Algorithm{ + Rank: rank, + Category: cat, + Name: name, + URL: "https://github.com/algorithm-visualizer/algorithms/tree/master/" + item.Path, + }) + } + return out, nil +} + +// List returns algorithms, optionally filtered by category, up to limit. +func (c *Client) List(ctx context.Context, category string, limit int) ([]Algorithm, error) { + all, err := c.fetchTree(ctx) + if err != nil { + return nil, err + } + var out []Algorithm + rank := 0 + for _, a := range all { + if category != "" && !strings.EqualFold(a.Category, category) { + continue + } + rank++ + a.Rank = rank + out = append(out, a) + if limit > 0 && len(out) >= limit { + break + } + } + return out, nil +} + +// Search finds algorithms matching query in name or category. +func (c *Client) Search(ctx context.Context, query string, limit int) ([]Algorithm, error) { + all, err := c.fetchTree(ctx) + if err != nil { + return nil, err + } + q := strings.ToLower(query) + var out []Algorithm + rank := 0 + for _, a := range all { + if strings.Contains(strings.ToLower(a.Name), q) || strings.Contains(strings.ToLower(a.Category), q) { + rank++ + a.Rank = rank + out = append(out, a) + if limit > 0 && len(out) >= limit { + break + } + } + } + return out, nil +} + +// Categories returns all categories with algorithm counts. +func (c *Client) Categories(ctx context.Context) ([]Category, error) { + all, err := c.fetchTree(ctx) + if err != nil { + return nil, err + } + counts := make(map[string]int) + order := []string{} + for _, a := range all { + if _, ok := counts[a.Category]; !ok { + order = append(order, a.Category) + } + counts[a.Category]++ + } + var out []Category + for i, cat := range order { + out = append(out, Category{Rank: i + 1, Name: cat, Count: counts[cat]}) + } + return out, nil +} diff --git a/algorithmvisualizer/algorithmvisualizer_test.go b/algorithmvisualizer/algorithmvisualizer_test.go index 2b718bc..1263143 100644 --- a/algorithmvisualizer/algorithmvisualizer_test.go +++ b/algorithmvisualizer/algorithmvisualizer_test.go @@ -1,62 +1,136 @@ -package algorithmvisualizer +package algorithmvisualizer_test import ( "context" + "encoding/json" "net/http" "net/http/httptest" "testing" - "time" + + "github.com/tamnd/algorithmvisualizer-cli/algorithmvisualizer" ) -func TestGet(t *testing.T) { +const testTreeJSON = `{ + "tree": [ + {"path": ".images", "type": "tree"}, + {"path": "Backtracking", "type": "tree"}, + {"path": "Backtracking/Knight's Tour Problem", "type": "tree"}, + {"path": "Backtracking/N-Queens Problem", "type": "tree"}, + {"path": "Dynamic Programming", "type": "tree"}, + {"path": "Dynamic Programming/Fibonacci Sequence", "type": "tree"}, + {"path": "Dynamic Programming/Knapsack Problem", "type": "tree"}, + {"path": "Backtracking/Knight's Tour Problem/code.js", "type": "blob"}, + {"path": "Backtracking/N-Queens Problem/code.js", "type": "blob"} + ] +}` + +func newTestServer(t *testing.T) (*httptest.Server, algorithmvisualizer.Config) { + t.Helper() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Header.Get("User-Agent") == "" { - t.Error("request carried no User-Agent") - } - _, _ = w.Write([]byte("ok")) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(testTreeJSON)) })) - defer srv.Close() + t.Cleanup(srv.Close) + + cfg := algorithmvisualizer.DefaultConfig() + cfg.BaseURL = srv.URL + cfg.Rate = 0 + return srv, cfg +} - c := NewClient() - c.Rate = 0 // no pacing in the test +func TestList(t *testing.T) { + _, cfg := newTestServer(t) + client := algorithmvisualizer.NewClient(cfg) - body, err := c.Get(context.Background(), srv.URL) + algos, err := client.List(context.Background(), "", 0) if err != nil { t.Fatal(err) } - if string(body) != "ok" { - t.Errorf("body = %q, want %q", body, "ok") + // .images (dot-prefix) skipped, depth-1 dirs skipped, blobs skipped + // Expect: Knight's Tour, N-Queens, Fibonacci, Knapsack = 4 + if len(algos) != 4 { + t.Errorf("List() = %d algorithms, want 4", len(algos)) + for _, a := range algos { + t.Logf(" %+v", a) + } } } -func TestGetRetriesOn503(t *testing.T) { - var hits int - srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - hits++ - if hits < 3 { - w.WriteHeader(http.StatusServiceUnavailable) - return +func TestListCategory(t *testing.T) { + _, cfg := newTestServer(t) + client := algorithmvisualizer.NewClient(cfg) + + algos, err := client.List(context.Background(), "Backtracking", 0) + if err != nil { + t.Fatal(err) + } + if len(algos) != 2 { + t.Errorf("List(Backtracking) = %d, want 2", len(algos)) + } + for _, a := range algos { + if a.Category != "Backtracking" { + t.Errorf("unexpected category %q", a.Category) } - _, _ = w.Write([]byte("recovered")) - })) - defer srv.Close() + } +} + +func TestSearch(t *testing.T) { + _, cfg := newTestServer(t) + client := algorithmvisualizer.NewClient(cfg) + + algos, err := client.Search(context.Background(), "queen", 0) + if err != nil { + t.Fatal(err) + } + if len(algos) != 1 { + t.Errorf("Search(queen) = %d, want 1", len(algos)) + } + if len(algos) > 0 && algos[0].Name != "N-Queens Problem" { + t.Errorf("got name %q, want %q", algos[0].Name, "N-Queens Problem") + } +} - c := NewClient() - c.Rate = 0 - c.Retries = 5 +func TestCategories(t *testing.T) { + _, cfg := newTestServer(t) + client := algorithmvisualizer.NewClient(cfg) - start := time.Now() - body, err := c.Get(context.Background(), srv.URL) + cats, err := client.Categories(context.Background()) if err != nil { t.Fatal(err) } - if string(body) != "recovered" { - t.Errorf("body = %q after retries", body) + if len(cats) != 2 { + t.Errorf("Categories() = %d, want 2", len(cats)) + } + + want := map[string]int{ + "Backtracking": 2, + "Dynamic Programming": 2, } - if hits != 3 { - t.Errorf("server saw %d hits, want 3", hits) + for _, c := range cats { + if got, ok := want[c.Name]; !ok { + t.Errorf("unexpected category %q", c.Name) + } else if got != c.Count { + t.Errorf("category %q count = %d, want %d", c.Name, c.Count, got) + } + } +} + +func TestAlgorithmJSON(t *testing.T) { + a := algorithmvisualizer.Algorithm{ + Rank: 1, + Category: "Backtracking", + Name: "N-Queens Problem", + URL: "https://github.com/algorithm-visualizer/algorithms/tree/master/Backtracking/N-Queens Problem", + } + b, err := json.Marshal(a) + if err != nil { + t.Fatal(err) + } + var got algorithmvisualizer.Algorithm + if err := json.Unmarshal(b, &got); err != nil { + t.Fatal(err) } - if time.Since(start) < 500*time.Millisecond { - t.Error("retries did not back off") + if got.Rank != a.Rank || got.Name != a.Name || got.Category != a.Category { + t.Errorf("round-trip mismatch: %+v", got) } } diff --git a/cli/cmd_categories.go b/cli/cmd_categories.go new file mode 100644 index 0000000..8b109c4 --- /dev/null +++ b/cli/cmd_categories.go @@ -0,0 +1,19 @@ +package cli + +import ( + "github.com/spf13/cobra" +) + +func (a *App) categoriesCmd() *cobra.Command { + return &cobra.Command{ + Use: "categories", + Short: "List all algorithm categories", + RunE: func(cmd *cobra.Command, _ []string) error { + cats, err := a.client.Categories(cmd.Context()) + if err != nil { + return mapFetchErr(err) + } + return a.renderOrEmpty(cats, len(cats)) + }, + } +} diff --git a/cli/cmd_list.go b/cli/cmd_list.go new file mode 100644 index 0000000..776ae52 --- /dev/null +++ b/cli/cmd_list.go @@ -0,0 +1,25 @@ +package cli + +import ( + "github.com/spf13/cobra" +) + +func (a *App) listCmd() *cobra.Command { + var category string + + cmd := &cobra.Command{ + Use: "list", + Short: "List algorithms", + RunE: func(cmd *cobra.Command, _ []string) error { + limit := a.effectiveLimit(50) + algos, err := a.client.List(cmd.Context(), category, limit) + if err != nil { + return mapFetchErr(err) + } + return a.renderOrEmpty(algos, len(algos)) + }, + } + + cmd.Flags().StringVar(&category, "category", "", "filter by category name") + return cmd +} diff --git a/cli/cmd_search.go b/cli/cmd_search.go new file mode 100644 index 0000000..da7ef2c --- /dev/null +++ b/cli/cmd_search.go @@ -0,0 +1,27 @@ +package cli + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +func (a *App) searchCmd() *cobra.Command { + return &cobra.Command{ + Use: "search ", + Short: "Search algorithms by name", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + query := args[0] + if query == "" { + return codeError(exitUsage, fmt.Errorf("query cannot be empty")) + } + limit := a.effectiveLimit(20) + algos, err := a.client.Search(cmd.Context(), query, limit) + if err != nil { + return mapFetchErr(err) + } + return a.renderOrEmpty(algos, len(algos)) + }, + } +} diff --git a/cli/errors.go b/cli/errors.go new file mode 100644 index 0000000..d1ed14a --- /dev/null +++ b/cli/errors.go @@ -0,0 +1,3 @@ +package cli + +func isNotFound(_ error) bool { return false } diff --git a/cli/output.go b/cli/output.go new file mode 100644 index 0000000..c40a2bf --- /dev/null +++ b/cli/output.go @@ -0,0 +1,23 @@ +package cli + +import ( + "io" + + "github.com/tamnd/algorithmvisualizer-cli/pkg/render" +) + +type Format = render.Format + +const ( + FormatTable = render.FormatTable + FormatJSON = render.FormatJSON + FormatJSONL = render.FormatJSONL + FormatCSV = render.FormatCSV + FormatTSV = render.FormatTSV + FormatURL = render.FormatURL + FormatRaw = render.FormatRaw +) + +func NewRenderer(w io.Writer, format Format, fields []string, noHeader bool, tmpl string) *render.Renderer { + return render.New(w, format, fields, noHeader, tmpl) +} diff --git a/cli/root.go b/cli/root.go index 857ece2..0912678 100644 --- a/cli/root.go +++ b/cli/root.go @@ -1,31 +1,142 @@ -// Package cli builds the algorithmvisualizer command tree on top of the algorithmvisualizer library. +// Package cli builds the algvis command tree on top of the algorithmvisualizer library. package cli import ( + "fmt" + "os" + + "github.com/mattn/go-isatty" "github.com/spf13/cobra" + "github.com/tamnd/algorithmvisualizer-cli/algorithmvisualizer" ) -// Build metadata, set via -ldflags at release time. var ( Version = "dev" Commit = "none" Date = "unknown" ) +const ( + exitError = 1 + exitUsage = 2 + exitNoData = 3 +) + +// ExitError carries an exit code alongside an error. +type ExitError struct { + Code int + Err error +} + +func (e *ExitError) Error() string { + if e.Err != nil { + return e.Err.Error() + } + return fmt.Sprintf("exit %d", e.Code) +} + +func (e *ExitError) Unwrap() error { return e.Err } + +func codeError(code int, err error) error { return &ExitError{Code: code, Err: err} } + +// App holds shared state for all commands. +type App struct { + client *algorithmvisualizer.Client + cfg algorithmvisualizer.Config + + output string + fields []string + noHeader bool + template string + limit int + quiet bool +} + // Root builds the root command and its subtree. func Root() *cobra.Command { - root := &cobra.Command{ - Use: "algorithmvisualizer", - Short: "A command line for algorithmvisualizer.", - Long: `A command line for algorithmvisualizer. + app := &App{cfg: algorithmvisualizer.DefaultConfig()} -This is a fresh scaffold. Add your commands here on top of the algorithmvisualizer -library package, then wire them into Root with root.AddCommand.`, + root := &cobra.Command{ + Use: "algvis", + Short: "Browse Algorithm Visualizer algorithms", SilenceUsage: true, SilenceErrors: true, + PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { + return app.setup() + }, } - root.AddCommand(newVersionCmd()) - // TODO: root.AddCommand(newGetCmd()), etc. + pf := root.PersistentFlags() + pf.StringVarP(&app.output, "output", "o", "auto", "output: table|json|jsonl|csv|tsv|url|raw") + pf.StringSliceVar(&app.fields, "fields", nil, "comma-separated columns to include") + pf.BoolVar(&app.noHeader, "no-header", false, "omit header row in table/csv/tsv") + pf.StringVar(&app.template, "template", "", "Go text/template per record") + pf.IntVarP(&app.limit, "limit", "n", 0, "limit number of records (0 = command default)") + pf.BoolVarP(&app.quiet, "quiet", "q", false, "suppress progress on stderr") + pf.DurationVar(&app.cfg.Rate, "delay", app.cfg.Rate, "minimum spacing between requests") + pf.DurationVar(&app.cfg.Timeout, "timeout", app.cfg.Timeout, "per-request timeout") + pf.IntVar(&app.cfg.Retries, "retries", app.cfg.Retries, "retry attempts on 429/5xx") + pf.StringVar(&app.cfg.UserAgent, "user-agent", app.cfg.UserAgent, "User-Agent header") + + root.AddCommand( + app.listCmd(), + app.searchCmd(), + app.categoriesCmd(), + newVersionCmd(), + ) return root } + +func (a *App) setup() error { + if a.output == "" || a.output == "auto" { + if isatty.IsTerminal(os.Stdout.Fd()) { + a.output = string(FormatTable) + } else { + a.output = string(FormatJSONL) + } + } + if !Format(a.output).Valid() { + return codeError(exitUsage, fmt.Errorf("unknown output format %q", a.output)) + } + a.client = algorithmvisualizer.NewClient(a.cfg) + return nil +} + +func (a *App) render(records any) error { + r := NewRenderer(os.Stdout, Format(a.output), a.fields, a.noHeader, a.template) + return r.Render(records) +} + +func (a *App) renderOrEmpty(records any, n int) error { + if err := a.render(records); err != nil { + return err + } + if n == 0 { + return codeError(exitNoData, nil) + } + return nil +} + +func (a *App) progressf(format string, args ...any) { + if a.quiet { + return + } + _, _ = fmt.Fprintf(os.Stderr, format+"\n", args...) +} + +func mapFetchErr(err error) error { + if err == nil { + return nil + } + if isNotFound(err) { + return codeError(exitNoData, err) + } + return codeError(exitError, err) +} + +func (a *App) effectiveLimit(def int) int { + if a.limit > 0 { + return a.limit + } + return def +} diff --git a/cmd/algorithmvisualizer/main.go b/cmd/algvis/main.go similarity index 100% rename from cmd/algorithmvisualizer/main.go rename to cmd/algvis/main.go diff --git a/go.mod b/go.mod index c415f83..617e43e 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,34 @@ go 1.26 require ( github.com/charmbracelet/fang v1.0.0 + github.com/mattn/go-isatty v0.0.22 github.com/spf13/cobra v1.10.2 ) + +require ( + charm.land/lipgloss/v2 v2.0.0-beta.3.0.20251106193318-19329a3e8410 // indirect + github.com/charmbracelet/colorprofile v0.3.3 // indirect + github.com/charmbracelet/ultraviolet v0.0.0-20251106190538-99ea45596692 // indirect + github.com/charmbracelet/x/ansi v0.11.0 // indirect + github.com/charmbracelet/x/exp/charmtone v0.0.0-20250603201427-c31516f43444 // indirect + github.com/charmbracelet/x/term v0.2.2 // indirect + github.com/charmbracelet/x/termios v0.1.1 // indirect + github.com/charmbracelet/x/windows v0.2.2 // indirect + github.com/clipperhouse/displaywidth v0.4.1 // indirect + github.com/clipperhouse/stringish v0.1.1 // indirect + github.com/clipperhouse/uax29/v2 v2.3.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/lucasb-eyer/go-colorful v1.3.0 // indirect + github.com/mattn/go-runewidth v0.0.19 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/mango v0.1.0 // indirect + github.com/muesli/mango-cobra v1.2.0 // indirect + github.com/muesli/mango-pflag v0.1.0 // indirect + github.com/muesli/roff v0.1.0 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + github.com/spf13/pflag v1.0.9 // indirect + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/sync v0.17.0 // indirect + golang.org/x/sys v0.37.0 // indirect + golang.org/x/text v0.24.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e7d4564 --- /dev/null +++ b/go.sum @@ -0,0 +1,74 @@ +charm.land/lipgloss/v2 v2.0.0-beta.3.0.20251106193318-19329a3e8410 h1:D9PbaszZYpB4nj+d6HTWr1onlmlyuGVNfL9gAi8iB3k= +charm.land/lipgloss/v2 v2.0.0-beta.3.0.20251106193318-19329a3e8410/go.mod h1:1qZyvvVCenJO2M1ac2mX0yyiIZJoZmDM4DG4s0udJkU= +github.com/aymanbagabas/go-udiff v0.3.1 h1:LV+qyBQ2pqe0u42ZsUEtPiCaUoqgA9gYRDs3vj1nolY= +github.com/aymanbagabas/go-udiff v0.3.1/go.mod h1:G0fsKmG+P6ylD0r6N/KgQD/nWzgfnl8ZBcNLgcbrw8E= +github.com/charmbracelet/colorprofile v0.3.3 h1:DjJzJtLP6/NZ8p7Cgjno0CKGr7wwRJGxWUwh2IyhfAI= +github.com/charmbracelet/colorprofile v0.3.3/go.mod h1:nB1FugsAbzq284eJcjfah2nhdSLppN2NqvfotkfRYP4= +github.com/charmbracelet/fang v1.0.0 h1:jESBY40agJOlLYnnv9jE0mLqDGTxEk0hkOnx7YGyRlQ= +github.com/charmbracelet/fang v1.0.0/go.mod h1:P5/DNb9DddQ0Z0dbc0P3ol4/ix5Po7Ofr2KMBfAqoCo= +github.com/charmbracelet/ultraviolet v0.0.0-20251106190538-99ea45596692 h1:r/3jQZ1LjWW6ybp8HHfhrKrwHIWiJhUuY7wwYIWZulQ= +github.com/charmbracelet/ultraviolet v0.0.0-20251106190538-99ea45596692/go.mod h1:Y8B4DzWeTb0ama8l3+KyopZtkE8fZjwRQ3aEAPEXHE0= +github.com/charmbracelet/x/ansi v0.11.0 h1:uuIVK7GIplwX6UBIz8S2TF8nkr7xRlygSsBRjSJqIvA= +github.com/charmbracelet/x/ansi v0.11.0/go.mod h1:uQt8bOrq/xgXjlGcFMc8U2WYbnxyjrKhnvTQluvfCaE= +github.com/charmbracelet/x/exp/charmtone v0.0.0-20250603201427-c31516f43444 h1:IJDiTgVE56gkAGfq0lBEloWgkXMk4hl/bmuPoicI4R0= +github.com/charmbracelet/x/exp/charmtone v0.0.0-20250603201427-c31516f43444/go.mod h1:T9jr8CzFpjhFVHjNjKwbAD7KwBNyFnj2pntAO7F2zw0= +github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f h1:pk6gmGpCE7F3FcjaOEKYriCvpmIN4+6OS/RD0vm4uIA= +github.com/charmbracelet/x/exp/golden v0.0.0-20250806222409-83e3a29d542f/go.mod h1:IfZAMTHB6XkZSeXUqriemErjAWCCzT0LwjKFYCZyw0I= +github.com/charmbracelet/x/term v0.2.2 h1:xVRT/S2ZcKdhhOuSP4t5cLi5o+JxklsoEObBSgfgZRk= +github.com/charmbracelet/x/term v0.2.2/go.mod h1:kF8CY5RddLWrsgVwpw4kAa6TESp6EB5y3uxGLeCqzAI= +github.com/charmbracelet/x/termios v0.1.1 h1:o3Q2bT8eqzGnGPOYheoYS8eEleT5ZVNYNy8JawjaNZY= +github.com/charmbracelet/x/termios v0.1.1/go.mod h1:rB7fnv1TgOPOyyKRJ9o+AsTU/vK5WHJ2ivHeut/Pcwo= +github.com/charmbracelet/x/windows v0.2.2 h1:IofanmuvaxnKHuV04sC0eBy/smG6kIKrWG2/jYn2GuM= +github.com/charmbracelet/x/windows v0.2.2/go.mod h1:/8XtdKZzedat74NQFn0NGlGL4soHB0YQZrETF96h75k= +github.com/clipperhouse/displaywidth v0.4.1 h1:uVw9V8UDfnggg3K2U84VWY1YLQ/x2aKSCtkRyYozfoU= +github.com/clipperhouse/displaywidth v0.4.1/go.mod h1:R+kHuzaYWFkTm7xoMmK1lFydbci4X2CicfbGstSGg0o= +github.com/clipperhouse/stringish v0.1.1 h1:+NSqMOr3GR6k1FdRhhnXrLfztGzuG+VuFDfatpWHKCs= +github.com/clipperhouse/stringish v0.1.1/go.mod h1:v/WhFtE1q0ovMta2+m+UbpZ+2/HEXNWYXQgCt4hdOzA= +github.com/clipperhouse/uax29/v2 v2.3.0 h1:SNdx9DVUqMoBuBoW3iLOj4FQv3dN5mDtuqwuhIGpJy4= +github.com/clipperhouse/uax29/v2 v2.3.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsVRgg6W7ihQeh4g= +github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/lucasb-eyer/go-colorful v1.3.0 h1:2/yBRLdWBZKrf7gB40FoiKfAWYQ0lqNcbuQwVHXptag= +github.com/lucasb-eyer/go-colorful v1.3.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw4= +github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4= +github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw= +github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/mango v0.1.0 h1:DZQK45d2gGbql1arsYA4vfg4d7I9Hfx5rX/GCmzsAvI= +github.com/muesli/mango v0.1.0/go.mod h1:5XFpbC8jY5UUv89YQciiXNlbi+iJgt29VDC5xbzrLL4= +github.com/muesli/mango-cobra v1.2.0 h1:DQvjzAM0PMZr85Iv9LIMaYISpTOliMEg+uMFtNbYvWg= +github.com/muesli/mango-cobra v1.2.0/go.mod h1:vMJL54QytZAJhCT13LPVDfkvCUJ5/4jNUKF/8NC2UjA= +github.com/muesli/mango-pflag v0.1.0 h1:UADqbYgpUyRoBja3g6LUL+3LErjpsOwaC9ywvBWe7Sg= +github.com/muesli/mango-pflag v0.1.0/go.mod h1:YEQomTxaCUp8PrbhFh10UfbhbQrM/xJ4i2PB8VTLLW0= +github.com/muesli/roff v0.1.0 h1:YD0lalCotmYuF5HhZliKWlIx7IEhiXeSfq7hNjFqGF8= +github.com/muesli/roff v0.1.0/go.mod h1:pjAHQM9hdUUwm/krAfrLGgJkXJ+YuhtsfZ42kieB2Ig= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= +github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= +github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY= +github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= +golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= +golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= +golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ= +golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0= +golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/render/render.go b/pkg/render/render.go new file mode 100644 index 0000000..34393b6 --- /dev/null +++ b/pkg/render/render.go @@ -0,0 +1,350 @@ +// Package render turns slices of record structs into one of the output formats +// hackernews-cli supports: table, json, jsonl, csv, tsv, url, and raw. It works +// off struct reflection and json tags, so any record type renders without +// per-type code. +package render + +import ( + "encoding/csv" + "encoding/json" + "fmt" + "io" + "reflect" + "strconv" + "strings" + "text/tabwriter" + "text/template" + "time" +) + +// Format is an output rendering format. +type Format string + +const ( + FormatTable Format = "table" + FormatJSON Format = "json" + FormatJSONL Format = "jsonl" + FormatCSV Format = "csv" + FormatTSV Format = "tsv" + FormatURL Format = "url" + FormatRaw Format = "raw" +) + +// Valid reports whether f is one of the supported formats. +func (f Format) Valid() bool { + switch f { + case FormatTable, FormatJSON, FormatJSONL, FormatCSV, FormatTSV, FormatURL, FormatRaw: + return true + } + return false +} + +// Renderer writes records in a chosen format. +type Renderer struct { + Format Format + Fields []string + NoHeader bool + Template string + w io.Writer +} + +// New builds a Renderer writing to w. +func New(w io.Writer, format Format, fields []string, noHeader bool, tmpl string) *Renderer { + return &Renderer{Format: format, Fields: fields, NoHeader: noHeader, Template: tmpl, w: w} +} + +// Render writes records (a slice of structs, or a single struct) in the configured format. +func (r *Renderer) Render(records any) error { + rv := reflect.ValueOf(records) + if rv.Kind() == reflect.Pointer { + rv = rv.Elem() + } + if rv.Kind() != reflect.Slice { + s := reflect.MakeSlice(reflect.SliceOf(rv.Type()), 1, 1) + s.Index(0).Set(rv) + rv = s + } + n := rv.Len() + items := make([]any, n) + for i := 0; i < n; i++ { + items[i] = rv.Index(i).Interface() + } + + if r.Template != "" { + return r.renderTemplate(items) + } + switch r.Format { + case FormatJSON: + return r.renderJSON(items) + case FormatJSONL: + return r.renderJSONL(items) + case FormatCSV: + return r.renderDelimited(items, ',') + case FormatTSV: + return r.renderDelimited(items, '\t') + case FormatURL: + return r.renderURL(items) + case FormatRaw: + return r.renderRaw(items) + default: + return r.renderTable(items) + } +} + +func (r *Renderer) renderJSON(items []any) error { + enc := json.NewEncoder(r.w) + enc.SetIndent("", " ") + if len(items) == 1 { + return enc.Encode(items[0]) + } + return enc.Encode(items) +} + +func (r *Renderer) renderJSONL(items []any) error { + enc := json.NewEncoder(r.w) + for _, it := range items { + if err := enc.Encode(it); err != nil { + return err + } + } + return nil +} + +func (r *Renderer) renderTemplate(items []any) error { + t, err := template.New("row").Funcs(template.FuncMap{ + "join": func(sep string, v any) string { return joinAny(sep, v) }, + }).Parse(r.Template) + if err != nil { + return fmt.Errorf("parse --template: %w", err) + } + for _, it := range items { + if err := t.Execute(r.w, toAnyMap(it)); err != nil { + return err + } + _, _ = fmt.Fprintln(r.w) + } + return nil +} + +func (r *Renderer) renderURL(items []any) error { + for _, it := range items { + m := toMap(it) + if u := firstNonEmpty(m["url"], m["hn_url"], m["permalink"]); u != "" { + _, _ = fmt.Fprintln(r.w, u) + } + } + return nil +} + +func (r *Renderer) renderRaw(items []any) error { + cols := r.columns(items) + for _, it := range items { + m := toMap(it) + vals := make([]string, 0, len(cols)) + for _, c := range cols { + vals = append(vals, m[c]) + } + _, _ = fmt.Fprintln(r.w, strings.Join(vals, " ")) + } + return nil +} + +func (r *Renderer) renderTable(items []any) error { + if len(items) == 0 { + return nil + } + cols := r.columns(items) + tw := tabwriter.NewWriter(r.w, 0, 4, 2, ' ', 0) + if !r.NoHeader { + _, _ = fmt.Fprintln(tw, strings.Join(upperAll(cols), "\t")) + } + for _, it := range items { + m := toMap(it) + cells := make([]string, len(cols)) + for i, c := range cols { + cells[i] = truncate(m[c], 60) + } + _, _ = fmt.Fprintln(tw, strings.Join(cells, "\t")) + } + return tw.Flush() +} + +func (r *Renderer) renderDelimited(items []any, comma rune) error { + if len(items) == 0 { + return nil + } + cols := r.columns(items) + cw := csv.NewWriter(r.w) + cw.Comma = comma + if !r.NoHeader { + if err := cw.Write(cols); err != nil { + return err + } + } + for _, it := range items { + m := toMap(it) + row := make([]string, len(cols)) + for i, c := range cols { + row[i] = m[c] + } + if err := cw.Write(row); err != nil { + return err + } + } + cw.Flush() + return cw.Error() +} + +func (r *Renderer) columns(items []any) []string { + if len(r.Fields) > 0 { + return r.Fields + } + if len(items) == 0 { + return nil + } + return structJSONKeys(items[0]) +} + +func toAnyMap(v any) any { + data, err := json.Marshal(v) + if err != nil { + return v + } + var m map[string]any + if err := json.Unmarshal(data, &m); err != nil { + return v + } + return m +} + +func joinAny(sep string, v any) string { + switch vv := v.(type) { + case nil: + return "" + case []string: + return strings.Join(vv, sep) + case []any: + parts := make([]string, len(vv)) + for i, e := range vv { + parts[i] = fmt.Sprintf("%v", e) + } + return strings.Join(parts, sep) + default: + return fmt.Sprintf("%v", v) + } +} + +func toMap(v any) map[string]string { + out := map[string]string{} + rv := reflect.ValueOf(v) + if rv.Kind() == reflect.Pointer { + rv = rv.Elem() + } + if rv.Kind() != reflect.Struct { + return out + } + rt := rv.Type() + for i := 0; i < rt.NumField(); i++ { + f := rt.Field(i) + if f.PkgPath != "" { + continue + } + key := jsonKey(f) + if key == "-" { + continue + } + out[key] = formatValue(rv.Field(i)) + } + return out +} + +func structJSONKeys(v any) []string { + rv := reflect.ValueOf(v) + if rv.Kind() == reflect.Pointer { + rv = rv.Elem() + } + if rv.Kind() != reflect.Struct { + return nil + } + rt := rv.Type() + var keys []string + for i := 0; i < rt.NumField(); i++ { + f := rt.Field(i) + if f.PkgPath != "" { + continue + } + key := jsonKey(f) + if key == "-" { + continue + } + keys = append(keys, key) + } + return keys +} + +func jsonKey(f reflect.StructField) string { + tag := f.Tag.Get("json") + if tag == "" { + return f.Name + } + name := strings.Split(tag, ",")[0] + if name == "" { + return f.Name + } + return name +} + +func formatValue(v reflect.Value) string { + switch v.Kind() { + case reflect.String: + return v.String() + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return strconv.FormatInt(v.Int(), 10) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return strconv.FormatUint(v.Uint(), 10) + case reflect.Float32, reflect.Float64: + return strconv.FormatFloat(v.Float(), 'g', -1, 64) + case reflect.Bool: + return strconv.FormatBool(v.Bool()) + case reflect.Slice: + parts := make([]string, v.Len()) + for i := 0; i < v.Len(); i++ { + parts[i] = formatValue(v.Index(i)) + } + return strings.Join(parts, ";") + case reflect.Struct: + if t, ok := v.Interface().(time.Time); ok { + if t.IsZero() { + return "" + } + return t.Format(time.RFC3339) + } + } + return fmt.Sprintf("%v", v.Interface()) +} + +func upperAll(ss []string) []string { + out := make([]string, len(ss)) + for i, s := range ss { + out[i] = strings.ToUpper(s) + } + return out +} + +func firstNonEmpty(ss ...string) string { + for _, s := range ss { + if s != "" { + return s + } + } + return "" +} + +func truncate(s string, n int) string { + s = strings.ReplaceAll(s, "\n", " ") + if len([]rune(s)) <= n { + return s + } + rs := []rune(s) + return string(rs[:n-1]) + "..." +}