ls

tcmts.cli.main.ls · #2x2umcjf · term · runs on the server

uses tcmts.cli.style.dim, which uses node:util

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { cutSignature, SIGNATURE_WIDTH } from "#codebase/tcmts/cli/main";
import { dim, red } from "#codebase/tcmts/cli/style";
import { shortHash } from "#codebase/tcmts/core/hash";
import { sameSpace } from "#codebase/tcmts/core/types";
import type { DefKind } from "#codebase/tcmts/core/types";

export function ls(cb: Codebase, ns = "") {
  const rows = cb.store
    .names(undefined, true)
    .filter((n) => !ns || n.path === ns || n.path.startsWith(ns + "."));
  if (!rows.length) return console.log(dim(ns ? `nothing under ${ns}` : "the codebase is empty"));
  const width = Math.max(...rows.map((r) => r.path.length));
  const kindsAt = new Map<string, DefKind[]>();
  for (const r of rows) kindsAt.set(r.path, [...(kindsAt.get(r.path) ?? []), r.kind]);
  for (const r of rows) {
    const rec = cb.store.getDef(r.hash)!;
    const sig = cutSignature(
      rec.kind === "type" ? rec.signature : `: ${cb.store.shownSignature(rec)}`,
      Math.max(40, SIGNATURE_WIDTH - width),
    );
    // conflicted only with definitions in its own space: a type isn't part of a clash between two values
    const conflict =
      kindsAt.get(r.path)!.filter((k) => sameSpace(k, r.kind)).length > 1
        ? red(" (conflicted)")
        : "";
    console.log(`${r.path.padEnd(width)}  ${dim(shortHash(r.hash))}  ${dim(sig)}${conflict}`);
  }
}

uses

used by