@dropsh/plugin-table 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,51 @@
1
+ drupal-cli is dual-licensed. You may use this software under the terms of
2
+ either the MIT License or the GNU General Public License version 2 or any
3
+ later version, at your option.
4
+
5
+ SPDX-License-Identifier: (MIT OR GPL-2.0-or-later)
6
+
7
+ ================================================================================
8
+ MIT License
9
+ ================================================================================
10
+
11
+ Copyright (c) 2026 Christian Wiedemann
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a copy
14
+ of this software and associated documentation files (the "Software"), to deal
15
+ in the Software without restriction, including without limitation the rights
16
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
+ copies of the Software, and to permit persons to whom the Software is
18
+ furnished to do so, subject to the following conditions:
19
+
20
+ The above copyright notice and this permission notice shall be included in all
21
+ copies or substantial portions of the Software.
22
+
23
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
+ SOFTWARE.
30
+
31
+ ================================================================================
32
+ GNU General Public License, version 2 or later
33
+ ================================================================================
34
+
35
+ The full text of the GNU GPL v2 is available at:
36
+ https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
37
+
38
+ The full text of any later GPL version is available at:
39
+ https://www.gnu.org/licenses/
40
+
41
+ Copyright (C) 2026 Christian Wiedemann
42
+
43
+ This program is free software; you can redistribute it and/or modify it under
44
+ the terms of the GNU General Public License as published by the Free Software
45
+ Foundation; either version 2 of the License, or (at your option) any later
46
+ version.
47
+
48
+ This program is distributed in the hope that it will be useful, but WITHOUT
49
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
50
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
51
+ details.
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@dropsh/plugin-table",
3
+ "version": "0.5.2",
4
+ "type": "module",
5
+ "main": "./dist/plugins/table/src/index.js",
6
+ "exports": {
7
+ ".": "./dist/plugins/table/src/index.js",
8
+ "./render": "./dist/plugins/table/src/render-table.js"
9
+ },
10
+ "publishConfig": {
11
+ "access": "public"
12
+ },
13
+ "peerDependencies": {
14
+ "dropsh": "*"
15
+ },
16
+ "devDependencies": {
17
+ "typescript": "^5.6.0",
18
+ "vitest": "^2.1.0",
19
+ "dropsh": "0.5.2"
20
+ },
21
+ "scripts": {
22
+ "build": "tsc",
23
+ "test": "vitest run --passWithNoTests",
24
+ "typecheck": "tsc --noEmit"
25
+ }
26
+ }
package/src/columns.ts ADDED
@@ -0,0 +1,41 @@
1
+ import type { JsonApiResource } from "dropsh/plugin";
2
+
3
+ const PREFERRED = ["title", "name", "label", "status"];
4
+ const MAX_CELL = 40;
5
+ const MAX_COLS = 5;
6
+
7
+ function isScalar(v: unknown): boolean {
8
+ return typeof v === "string" || typeof v === "number" || typeof v === "boolean";
9
+ }
10
+
11
+ export function pickColumns(res: JsonApiResource): string[] {
12
+ const attrs = res.attributes ?? {};
13
+ const scalars = Object.entries(attrs)
14
+ .filter(([, v]) => isScalar(v))
15
+ .map(([k]) => k);
16
+ const preferred = PREFERRED.filter((k) => scalars.includes(k));
17
+ const rest = scalars.filter((k) => !preferred.includes(k));
18
+ return ["id", ...preferred, ...rest].slice(0, MAX_COLS);
19
+ }
20
+
21
+ export function cell(v: unknown): string {
22
+ const s = v === undefined || v === null ? "" : String(v);
23
+ return s.length > MAX_CELL ? `${s.slice(0, MAX_CELL - 1)}…` : s;
24
+ }
25
+
26
+ export function formatTable(headers: string[], rows: string[][]): string {
27
+ const widths = headers.map((h, i) =>
28
+ Math.max(h.length, ...rows.map((r) => (r[i] ?? "").length), 0),
29
+ );
30
+ const bar = (l: string, m: string, r: string) =>
31
+ `${l}${widths.map((w) => "─".repeat(w + 2)).join(m)}${r}`;
32
+ const line = (cells: string[]) =>
33
+ `│ ${cells.map((c, i) => (c ?? "").padEnd(widths[i] ?? 0)).join(" │ ")} │`;
34
+ return [
35
+ bar("┌", "┬", "┐"),
36
+ line(headers),
37
+ bar("├", "┼", "┤"),
38
+ ...rows.map(line),
39
+ bar("└", "┴", "┘"),
40
+ ].join("\n");
41
+ }
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { DropSHPlugin } from "dropsh/plugin";
2
+ import { renderTable } from "./render-table.js";
3
+
4
+ export { renderTable };
5
+
6
+ export function tablePlugin(): DropSHPlugin {
7
+ return {
8
+ id: "table",
9
+ requiredModules: [],
10
+ async extendSchema(_entityType, _bundle, schema) {
11
+ return schema;
12
+ },
13
+ renderers: [{ id: "table", render: renderTable }],
14
+ };
15
+ }
@@ -0,0 +1,30 @@
1
+ import type { JsonApiDocument, RenderContext } from "dropsh/plugin";
2
+ import { cell, formatTable, pickColumns } from "./columns.js";
3
+
4
+ export { formatTable, pickColumns };
5
+
6
+ function isScalar(v: unknown): boolean {
7
+ return typeof v === "string" || typeof v === "number" || typeof v === "boolean";
8
+ }
9
+
10
+ export function renderTable(doc: JsonApiDocument, _ctx: RenderContext): string {
11
+ const data = doc.data;
12
+ if (Array.isArray(data)) {
13
+ if (data.length === 0) return "(0 rows)";
14
+ const first = data[0];
15
+ if (!first) return "(0 rows)";
16
+ const cols = pickColumns(first);
17
+ const rows = data.map((res) =>
18
+ cols.map((c) => cell(c === "id" ? res.id : (res.attributes ?? {})[c])),
19
+ );
20
+ return formatTable(cols, rows);
21
+ }
22
+ const rows: string[][] = [
23
+ ["type", data.type],
24
+ ["id", data.id],
25
+ ];
26
+ for (const [k, v] of Object.entries(data.attributes ?? {})) {
27
+ if (isScalar(v)) rows.push([k, cell(v)]);
28
+ }
29
+ return formatTable(["field", "value"], rows);
30
+ }
@@ -0,0 +1,53 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { pickColumns, renderTable } from "../../src/render-table.js";
3
+
4
+ const ctx = { command: "search" as const };
5
+
6
+ describe("pickColumns", () => {
7
+ it("puts id first and prefers title/status", () => {
8
+ const cols = pickColumns({
9
+ type: "node--article",
10
+ id: "u1",
11
+ attributes: { body: "x", status: true, title: "T" },
12
+ });
13
+ expect(cols[0]).toBe("id");
14
+ expect(cols).toContain("title");
15
+ expect(cols).toContain("status");
16
+ });
17
+ });
18
+
19
+ describe("renderTable", () => {
20
+ it("renders a collection as an aligned box table", () => {
21
+ const out = renderTable(
22
+ {
23
+ data: [
24
+ { type: "node--article", id: "u1", attributes: { title: "Alpha", status: true } },
25
+ { type: "node--article", id: "u2", attributes: { title: "Beta", status: false } },
26
+ ],
27
+ },
28
+ ctx,
29
+ );
30
+ const lines = out.split("\n");
31
+ // biome-ignore lint/style/noNonNullAssertion: split() on a non-empty string always yields at least one element
32
+ expect(lines[0]!.startsWith("┌")).toBe(true);
33
+ expect(out).toContain("u1");
34
+ expect(out).toContain("Alpha");
35
+ // every rendered line is the same visual width
36
+ expect(new Set(lines.map((l) => [...l].length)).size).toBe(1);
37
+ });
38
+
39
+ it("renders an empty collection as (0 rows)", () => {
40
+ expect(renderTable({ data: [] }, ctx)).toBe("(0 rows)");
41
+ });
42
+
43
+ it("renders a single resource as a key/value table", () => {
44
+ const out = renderTable(
45
+ { data: { type: "node--article", id: "u1", attributes: { title: "Solo" } } },
46
+ { command: "read" },
47
+ );
48
+ expect(out).toContain("field");
49
+ expect(out).toContain("value");
50
+ expect(out).toContain("title");
51
+ expect(out).toContain("Solo");
52
+ });
53
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "dist",
5
+ "noEmit": false
6
+ },
7
+ "include": ["src", "tests"]
8
+ }
@@ -0,0 +1,14 @@
1
+ import { resolve } from "node:path";
2
+ import { defineConfig } from "vitest/config";
3
+
4
+ export default defineConfig({
5
+ resolve: {
6
+ alias: {
7
+ "dropsh/plugin": resolve(__dirname, "../../src/plugin-api.ts"),
8
+ },
9
+ },
10
+ test: {
11
+ include: ["tests/unit/**/*.test.ts"],
12
+ environment: "node",
13
+ },
14
+ });