mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-22 01:02:16 +00:00
Add setEditorText/getEditorText to hook UI context, improve custom() API
- Add setEditorText() and getEditorText() to HookUIContext for prompt generator pattern - custom() now accepts async factories for fire-and-forget work - Add CancellableLoader component to tui package - Add BorderedLoader component for hooks with cancel UI - Export HookAPI, HookContext, HookFactory from main package - Update all examples to import from packages instead of relative paths - Update hooks.md and custom-tools.md documentation fixes #350
This commit is contained in:
parent
02d0d6e192
commit
6f7c10e323
39 changed files with 477 additions and 163 deletions
|
|
@ -247,6 +247,26 @@ loader.setMessage("Still loading...");
|
|||
loader.stop();
|
||||
```
|
||||
|
||||
### CancellableLoader
|
||||
|
||||
Extends Loader with Escape key handling and an AbortSignal for cancelling async operations.
|
||||
|
||||
```typescript
|
||||
const loader = new CancellableLoader(
|
||||
tui, // TUI instance for render updates
|
||||
(s) => chalk.cyan(s), // spinner color function
|
||||
(s) => chalk.gray(s), // message color function
|
||||
"Working..." // message
|
||||
);
|
||||
loader.onAbort = () => done(null); // Called when user presses Escape
|
||||
doAsyncWork(loader.signal).then(done);
|
||||
```
|
||||
|
||||
**Properties:**
|
||||
- `signal: AbortSignal` - Aborted when user presses Escape
|
||||
- `aborted: boolean` - Whether the loader was aborted
|
||||
- `onAbort?: () => void` - Callback when user presses Escape
|
||||
|
||||
### SelectList
|
||||
|
||||
Interactive selection list with keyboard navigation.
|
||||
|
|
|
|||
39
packages/tui/src/components/cancellable-loader.ts
Normal file
39
packages/tui/src/components/cancellable-loader.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { isEscape } from "../keys.js";
|
||||
import { Loader } from "./loader.js";
|
||||
|
||||
/**
|
||||
* Loader that can be cancelled with Escape.
|
||||
* Extends Loader with an AbortSignal for cancelling async operations.
|
||||
*
|
||||
* @example
|
||||
* const loader = new CancellableLoader(tui, cyan, dim, "Working...");
|
||||
* loader.onAbort = () => done(null);
|
||||
* doWork(loader.signal).then(done);
|
||||
*/
|
||||
export class CancellableLoader extends Loader {
|
||||
private abortController = new AbortController();
|
||||
|
||||
/** Called when user presses Escape */
|
||||
onAbort?: () => void;
|
||||
|
||||
/** AbortSignal that is aborted when user presses Escape */
|
||||
get signal(): AbortSignal {
|
||||
return this.abortController.signal;
|
||||
}
|
||||
|
||||
/** Whether the loader was aborted */
|
||||
get aborted(): boolean {
|
||||
return this.abortController.signal.aborted;
|
||||
}
|
||||
|
||||
handleInput(data: string): void {
|
||||
if (isEscape(data)) {
|
||||
this.abortController.abort();
|
||||
this.onAbort?.();
|
||||
}
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.stop();
|
||||
}
|
||||
}
|
||||
|
|
@ -9,6 +9,7 @@ export {
|
|||
} from "./autocomplete.js";
|
||||
// Components
|
||||
export { Box } from "./components/box.js";
|
||||
export { CancellableLoader } from "./components/cancellable-loader.js";
|
||||
export { Editor, type EditorTheme } from "./components/editor.js";
|
||||
export { Image, type ImageOptions, type ImageTheme } from "./components/image.js";
|
||||
export { Input } from "./components/input.js";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue