@antst/dashi 0.1.0-alpha.15 → 0.1.0-alpha.16
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/lib/input.d.ts +7 -0
- package/lib/input.js +40 -0
- package/lib/renderer.js +1 -0
- package/lib/state.d.ts +3 -0
- package/lib/state.js +1 -0
- package/package.json +1 -1
package/lib/input.d.ts
CHANGED
|
@@ -3,8 +3,15 @@ import { type ComposerCursor, type UiAction, type ViewState } from './state.js';
|
|
|
3
3
|
export interface InputBindings {
|
|
4
4
|
readonly dispatch: (action: UiAction) => void;
|
|
5
5
|
readonly insertNewline?: () => void;
|
|
6
|
+
readonly insertText?: (text: string) => void;
|
|
6
7
|
readonly readComposerCursor?: () => ComposerCursor;
|
|
7
8
|
readonly readState: () => ViewState;
|
|
8
9
|
}
|
|
10
|
+
export declare function droppedPaths(data: string, cwd: string, inspect?: (path: string) => {
|
|
11
|
+
isDirectory(): boolean;
|
|
12
|
+
}): {
|
|
13
|
+
readonly images: readonly string[];
|
|
14
|
+
readonly text: string;
|
|
15
|
+
} | undefined;
|
|
9
16
|
export declare function caretOffset(text: string, cursor: ComposerCursor): number;
|
|
10
17
|
export declare function installInput(tui: TUI, bindings: InputBindings): () => void;
|
package/lib/input.js
CHANGED
|
@@ -1,5 +1,38 @@
|
|
|
1
|
+
import { statSync } from 'node:fs';
|
|
2
|
+
import { isAbsolute, relative, resolve, sep } from 'node:path';
|
|
3
|
+
import { formatFileMention } from '@deepseek-ai/dsh-file-reference/grammar';
|
|
1
4
|
import { isKeyRelease, matchesKey } from '@earendil-works/pi-tui';
|
|
5
|
+
import { isImagePath } from './image-input.js';
|
|
2
6
|
import { PAGE_CELLS } from './state.js';
|
|
7
|
+
export function droppedPaths(data, cwd, inspect = statSync) {
|
|
8
|
+
const [start, end] = ['\u001B[200~', '\u001B[201~'];
|
|
9
|
+
if (!data.startsWith(start) || !data.endsWith(end))
|
|
10
|
+
return undefined;
|
|
11
|
+
const token = /(?:[^\s'\\]+|'[^']*'|\\[\s\S])+/gu;
|
|
12
|
+
const source = data.slice(start.length, -end.length);
|
|
13
|
+
const chunks = source.match(token) ?? [];
|
|
14
|
+
if (chunks.length === 0 || source.replace(token, '').trim() !== '')
|
|
15
|
+
return undefined;
|
|
16
|
+
const paths = chunks.map(chunk => chunk.replace(/'([^']*)'|\\([\s\S])/gu, (_match, quoted, escaped) => quoted ?? escaped ?? ''));
|
|
17
|
+
try {
|
|
18
|
+
const entries = paths.map(value => {
|
|
19
|
+
const absolute = resolve(cwd, value);
|
|
20
|
+
const directory = inspect(absolute).isDirectory();
|
|
21
|
+
if (!directory && isImagePath(absolute))
|
|
22
|
+
return { absolute };
|
|
23
|
+
const local = relative(resolve(cwd), absolute);
|
|
24
|
+
const mention = formatFileMention({ kind: directory ? 'directory' : 'file', path: local !== '' && !isAbsolute(local) && local !== '..' && !local.startsWith(`..${sep}`) ? local : absolute }, false);
|
|
25
|
+
if (mention === undefined)
|
|
26
|
+
throw new Error('path cannot be mentioned');
|
|
27
|
+
return { absolute, mention };
|
|
28
|
+
});
|
|
29
|
+
const mentions = entries.flatMap(entry => entry.mention === undefined ? [] : [entry.mention]);
|
|
30
|
+
return { images: entries.flatMap(entry => entry.mention === undefined ? [entry.absolute] : []), text: mentions.length === 0 ? '' : `${mentions.join(' ')} ` };
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
3
36
|
export function caretOffset(text, cursor) {
|
|
4
37
|
const lines = text.split('\n');
|
|
5
38
|
let offset = 0;
|
|
@@ -16,6 +49,13 @@ export function installInput(tui, bindings) {
|
|
|
16
49
|
const customQuestion = question !== undefined && decision?.kind === 'question'
|
|
17
50
|
&& decision.cursor === question.options.length;
|
|
18
51
|
const completion = state.overlay?.kind === 'list' && state.overlay.purpose === 'completion';
|
|
52
|
+
const dropped = decision === undefined && state.overlay === undefined && state.search === undefined && droppedPaths(data, state.root?.cwd ?? state.cwd);
|
|
53
|
+
if (dropped) {
|
|
54
|
+
bindings.insertText?.(dropped.text);
|
|
55
|
+
for (const path of dropped.images)
|
|
56
|
+
bindings.dispatch({ type: 'attachment-path', path });
|
|
57
|
+
return { consume: true };
|
|
58
|
+
}
|
|
19
59
|
return customQuestion || state.search !== undefined || completion
|
|
20
60
|
|| decision === undefined && state.overlay === undefined ? undefined : { consume: true };
|
|
21
61
|
}
|
package/lib/renderer.js
CHANGED
|
@@ -461,6 +461,7 @@ export function createRenderer(options) {
|
|
|
461
461
|
: action);
|
|
462
462
|
},
|
|
463
463
|
insertNewline: () => { editor.insertTextAtCursor('\n'); },
|
|
464
|
+
insertText: text => { editor.insertTextAtCursor(text); },
|
|
464
465
|
readComposerCursor: () => editor.getCursor(),
|
|
465
466
|
});
|
|
466
467
|
return {
|
package/lib/state.d.ts
CHANGED
|
@@ -272,6 +272,9 @@ export type UiAction = {
|
|
|
272
272
|
readonly source?: string;
|
|
273
273
|
readonly text?: string;
|
|
274
274
|
readonly type: 'attachment-added';
|
|
275
|
+
} | {
|
|
276
|
+
readonly path: string;
|
|
277
|
+
readonly type: 'attachment-path';
|
|
275
278
|
} | {
|
|
276
279
|
readonly index: number;
|
|
277
280
|
readonly type: 'attachment-remove';
|
package/lib/state.js
CHANGED
|
@@ -114,6 +114,7 @@ export function reduce(state, action) {
|
|
|
114
114
|
...(replace ? { composer: action.text } : {}),
|
|
115
115
|
}, [...(replace ? [{ type: 'set-composer', text: action.text }] : []), ...redraw()]];
|
|
116
116
|
}
|
|
117
|
+
case 'attachment-path': return [state, [{ type: 'attach', path: action.path, source: state.composer, text: state.composer }]];
|
|
117
118
|
case 'attachment-remove': {
|
|
118
119
|
if (action.index < 0 || action.index >= state.attachments.length)
|
|
119
120
|
return [state, []];
|