@antst/dashi 0.1.0-alpha.15 → 0.1.0-alpha.17
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 +10 -6
- 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
|
@@ -104,6 +104,13 @@ function codeBlockOptions(text) {
|
|
|
104
104
|
};
|
|
105
105
|
});
|
|
106
106
|
}
|
|
107
|
+
function exitChord(state) {
|
|
108
|
+
if (state.exitArmed)
|
|
109
|
+
return reduce(state, { type: 'exit' });
|
|
110
|
+
return [{ ...state, exitArmed: true, rewindArmed: false }, [
|
|
111
|
+
{ type: 'exit-timer', armed: true }, ...redraw(),
|
|
112
|
+
]];
|
|
113
|
+
}
|
|
107
114
|
export function reduce(state, action) {
|
|
108
115
|
const head = state.decisions[0];
|
|
109
116
|
switch (action.type) {
|
|
@@ -114,6 +121,7 @@ export function reduce(state, action) {
|
|
|
114
121
|
...(replace ? { composer: action.text } : {}),
|
|
115
122
|
}, [...(replace ? [{ type: 'set-composer', text: action.text }] : []), ...redraw()]];
|
|
116
123
|
}
|
|
124
|
+
case 'attachment-path': return [state, [{ type: 'attach', path: action.path, source: state.composer, text: state.composer }]];
|
|
117
125
|
case 'attachment-remove': {
|
|
118
126
|
if (action.index < 0 || action.index >= state.attachments.length)
|
|
119
127
|
return [state, []];
|
|
@@ -167,16 +175,12 @@ export function reduce(state, action) {
|
|
|
167
175
|
...state, attachments: [], composer: '', exitArmed: false, recall: undefined, rewindArmed: false,
|
|
168
176
|
}, [{ type: 'set-composer', text: '' }, ...redraw()]];
|
|
169
177
|
}
|
|
170
|
-
|
|
171
|
-
return reduce(state, { type: 'exit' });
|
|
172
|
-
return [{ ...state, exitArmed: true, rewindArmed: false }, [{ type: 'exit-timer', armed: true }, ...redraw()]];
|
|
178
|
+
return exitChord(state);
|
|
173
179
|
}
|
|
174
180
|
case 'ctrl-d':
|
|
175
181
|
if (state.composer !== '')
|
|
176
182
|
return [state, []];
|
|
177
|
-
|
|
178
|
-
return reduce(state, { type: 'exit' });
|
|
179
|
-
return reduce(state, { type: 'ctrl-c' });
|
|
183
|
+
return exitChord(state);
|
|
180
184
|
case 'decision-cancel': {
|
|
181
185
|
if (head === undefined)
|
|
182
186
|
return [state, []];
|