| 1 | /** @jsxImportSource @alchemy.run/sigil */ |
| 2 | import { AnsiText } from "@alchemy.run/sigil"; |
| 3 | import { inspect } from "node:util"; |
| 4 | import type { ReactNode } from "react"; |
| 5 | import { Box, useCliEnvironment } from "../ui/index.ts"; |
| 6 | |
| 7 | export interface StackOutputsProps { |
| 8 | readonly value: unknown; |
| 9 | readonly offset?: number; |
| 10 | readonly limit?: number; |
| 11 | } |
| 12 | |
| 13 | export function StackOutputs({ |
| 14 | value, |
| 15 | offset = 0, |
| 16 | limit = Number.POSITIVE_INFINITY, |
| 17 | }: StackOutputsProps) { |
| 18 | const { colors } = useCliEnvironment(); |
| 19 | const output = inspect(value, { colors }); |
| 20 | const lines = output.split("\n").slice(offset, offset + limit); |
| 21 | |
| 22 | return ( |
| 23 | <Box flexDirection="column"> |
| 24 | {lines.map((line, index) => ( |
| 25 | <AnsiText key={offset + index} wrap="none"> |
| 26 | {line || " "} |
| 27 | </AnsiText> |
| 28 | ))} |
| 29 | </Box> |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | export const stackOutputLineCount = (value: unknown): number => |
| 34 | inspect(value, { colors: false }).split("\n").length; |
| 35 | |
| 36 | export const stackOutputsView = (value: unknown): ReactNode => ( |
| 37 | <StackOutputs value={value} /> |
| 38 | ); |