-
Notifications
You must be signed in to change notification settings - Fork 0
feat(core): ctx.raw.argv, render helpers, dynamic fullscreen #149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zrosenbauer
merged 5 commits into
main
from
feat/ctx-raw-argv-render-helpers-dynamic-fullscreen
Apr 2, 2026
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8068f63
feat(core): add ctx.raw.argv, render/renderToString helpers, dynamic …
zrosenbauer e5cb3d3
chore: split changeset into per-issue entries
zrosenbauer 4e88f75
refactor(core): capture rawTokens once, avoid duplicate process.argv.…
zrosenbauer 1bdc2aa
refactor(core): use yargs hideBin instead of hardcoded slice(2)
zrosenbauer 56a6c44
fix(core): resolve lint errors in render.test.tsx
zrosenbauer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@kidd-cli/core": minor | ||
| --- | ||
|
|
||
| feat(core): expose `ctx.raw.argv` — a normalized argv where `argv[0]` is always the CLI name (via yargs `$0`), regardless of invocation mode. Middleware can inspect the full invocation without guessing preamble offsets. | ||
|
|
||
| Fixes #146 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@kidd-cli/core": minor | ||
| --- | ||
|
|
||
| feat(core): allow `screen({ fullscreen })` to accept a resolver function `(ctx: ScreenContext) => boolean | Promise<boolean>` for runtime fullscreen decisions based on args, config, or terminal capabilities. | ||
|
|
||
| Fixes #148 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@kidd-cli/core": minor | ||
| --- | ||
|
|
||
| feat(core): export `render()` and `renderToString()` helpers that wrap Ink's render methods with kidd's `KiddProvider` (screen context, output store, screen-backed log/spinner/report). Enables full lifecycle control from normal `command()` handlers. | ||
|
|
||
| Fixes #147 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export { screen } from './screen.js' | ||
| export { render, renderToString } from './render.js' | ||
| export { screen, toScreenContext } from './screen.js' | ||
| export type { ScreenDef, ScreenExit } from './screen.js' | ||
|
|
||
| export { useScreenContext } from './provider.js' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import type { ReactElement } from 'react' | ||
| import React from 'react' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import type { CommandContext, ScreenContext, Store } from '../context/types.js' | ||
|
|
||
| vi.mock(import('ink'), () => ({ | ||
| render: vi.fn(() => ({ | ||
| cleanup: vi.fn(), | ||
| clear: vi.fn(), | ||
| rerender: vi.fn(), | ||
| unmount: vi.fn(), | ||
| waitUntilExit: vi.fn().mockResolvedValue(undefined), | ||
| })), | ||
| renderToString: vi.fn(() => 'rendered-output'), | ||
| })) | ||
|
|
||
| const ink = await import('ink') | ||
| const mockedInkRender = vi.mocked(ink.render) | ||
| const mockedInkRenderToString = vi.mocked(ink.renderToString) | ||
|
|
||
| function StubComponent(): null { | ||
| return null | ||
| } | ||
|
|
||
| function makeStore(): Store { | ||
| const map = new Map<string, unknown>() | ||
| return { | ||
| clear: () => map.clear(), | ||
| delete: (key: string) => map.delete(key), | ||
| get: (key: string) => map.get(key), | ||
| has: (key: string) => map.has(key), | ||
| set: (key: string, value: unknown) => map.set(key, value), | ||
| } as Store | ||
| } | ||
|
|
||
| const baseMeta = Object.freeze({ | ||
| command: ['test'] as readonly string[], | ||
| dirs: Object.freeze({ global: '.cli', local: '.cli' }), | ||
| name: 'test-cli', | ||
| version: '1.0.0', | ||
| }) | ||
|
|
||
| function makeContext(overrides?: Partial<CommandContext>): CommandContext { | ||
| return { | ||
| args: {}, | ||
| colors: {} as CommandContext['colors'], | ||
| config: {}, | ||
| fail: () => { | ||
| throw new Error('fail') | ||
| }, | ||
| format: {} as CommandContext['format'], | ||
| log: {} as CommandContext['log'], | ||
| meta: baseMeta, | ||
| prompts: {} as CommandContext['prompts'], | ||
| raw: Object.freeze({ argv: Object.freeze(['test-cli', 'test']) }), | ||
| status: {} as CommandContext['status'], | ||
| store: makeStore(), | ||
| ...overrides, | ||
| } as CommandContext | ||
| } | ||
|
|
||
| describe('render()', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('should call ink render and return an instance', async () => { | ||
| const { render } = await import('./render.js') | ||
| const instance = await render(<StubComponent />, makeContext()) | ||
|
|
||
| expect(mockedInkRender).toHaveBeenCalledOnce() | ||
| expect(instance).toHaveProperty('waitUntilExit') | ||
| expect(instance).toHaveProperty('unmount') | ||
| }) | ||
|
|
||
| it('should wrap node in KiddProvider with ScreenContext', async () => { | ||
| const { render } = await import('./render.js') | ||
| const ctx = makeContext({ config: { debug: true } }) | ||
|
|
||
| await render(<StubComponent />, ctx) | ||
|
|
||
| const rendered = mockedInkRender.mock.calls[0]![0] as ReactElement | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const providerValue = rendered.props.value as ScreenContext | ||
| expect(providerValue.config).toEqual({ debug: true }) | ||
| expect(providerValue).toHaveProperty('log') | ||
| expect(providerValue).not.toHaveProperty('fail') | ||
| expect(providerValue).not.toHaveProperty('prompts') | ||
| }) | ||
|
|
||
| it('should pass render options through to ink', async () => { | ||
| const { render } = await import('./render.js') | ||
| const options = { debug: true } | ||
|
|
||
| await render(<StubComponent />, makeContext(), options) | ||
|
|
||
| expect(mockedInkRender).toHaveBeenCalledWith(expect.anything(), options) | ||
| }) | ||
| }) | ||
|
|
||
| describe('renderToString()', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| it('should call ink renderToString and return a string', async () => { | ||
| const { renderToString } = await import('./render.js') | ||
| const result = await renderToString(<StubComponent />, makeContext()) | ||
|
|
||
| expect(mockedInkRenderToString).toHaveBeenCalledOnce() | ||
| expect(result).toBe('rendered-output') | ||
| }) | ||
|
|
||
| it('should wrap node in KiddProvider with ScreenContext', async () => { | ||
| const { renderToString } = await import('./render.js') | ||
| const ctx = makeContext({ config: { theme: 'dark' } }) | ||
|
|
||
| await renderToString(<StubComponent />, ctx) | ||
|
|
||
| const rendered = mockedInkRenderToString.mock.calls[0]![0] as ReactElement | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const providerValue = rendered.props.value as ScreenContext | ||
| expect(providerValue.config).toEqual({ theme: 'dark' }) | ||
| expect(providerValue).not.toHaveProperty('colors') | ||
| expect(providerValue).not.toHaveProperty('format') | ||
| }) | ||
|
|
||
| it('should pass options through to ink renderToString', async () => { | ||
| const { renderToString } = await import('./render.js') | ||
| const options = { columns: 120 } | ||
|
|
||
| await renderToString(<StubComponent />, makeContext(), options) | ||
|
|
||
| expect(mockedInkRenderToString).toHaveBeenCalledWith(expect.anything(), options) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import type { Instance, RenderOptions, RenderToStringOptions } from 'ink' | ||
| import type { ReactNode } from 'react' | ||
| import React from 'react' | ||
|
|
||
| import type { CommandContext } from '../context/types.js' | ||
| import { KiddProvider } from './provider.js' | ||
| import { toScreenContext } from './screen.js' | ||
|
|
||
| /** | ||
| * Render a React element as a live Ink terminal application with the kidd | ||
| * screen context injected. | ||
| * | ||
| * Wraps the provided node in a {@link KiddProvider} that supplies the | ||
| * screen context (React-backed `log`, `spinner`, `store`, and any | ||
| * middleware-decorated properties). Returns the raw Ink {@link Instance} | ||
| * so callers have full lifecycle control — `waitUntilExit()`, `unmount()`, | ||
| * `rerender()`, etc. | ||
| * | ||
| * @param node - The React element to render. | ||
| * @param ctx - The command context to convert and inject as screen context. | ||
| * @param options - Optional Ink render options (stdout, stdin, debug, etc.). | ||
| * @returns The Ink render instance. | ||
| */ | ||
| export async function render( | ||
| node: ReactNode, | ||
| ctx: CommandContext, | ||
| options?: RenderOptions | ||
| ): Promise<Instance> { | ||
| const { render: inkRender } = await import('ink') | ||
| const screenCtx = toScreenContext(ctx) | ||
| return inkRender(<KiddProvider value={screenCtx}>{node}</KiddProvider>, options) | ||
| } | ||
|
|
||
| /** | ||
| * Render a React element to a string with the kidd screen context injected. | ||
| * | ||
| * Wraps the provided node in a {@link KiddProvider} and delegates to Ink's | ||
| * `renderToString`. Useful for generating documentation, writing output to | ||
| * files, testing, or scenarios where rendered output is needed as a string | ||
| * without a persistent terminal session. | ||
| * | ||
| * @param node - The React element to render. | ||
| * @param ctx - The command context to convert and inject as screen context. | ||
| * @param options - Optional render-to-string options (columns width). | ||
| * @returns The rendered string output. | ||
| */ | ||
| export async function renderToString( | ||
| node: ReactNode, | ||
| ctx: CommandContext, | ||
| options?: RenderToStringOptions | ||
| ): Promise<string> { | ||
| const { renderToString: inkRenderToString } = await import('ink') | ||
| const screenCtx = toScreenContext(ctx) | ||
| return inkRenderToString(<KiddProvider value={screenCtx}>{node}</KiddProvider>, options) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.