对齐zed的ls命令输出格式
This commit is contained in:
@@ -419,22 +419,35 @@ async function toolEditFile(ctx, args) {
|
|||||||
return '已成功应用 ' + applied + ' 处编辑到 ' + relOf(ctx.taskDir, abs);
|
return '已成功应用 ' + applied + ' 处编辑到 ' + relOf(ctx.taskDir, abs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 对齐 Zed list_directory 的输出格式:
|
||||||
|
// # Folders:
|
||||||
|
// <dir>/<name>
|
||||||
|
//
|
||||||
|
// # Files:
|
||||||
|
// <dir>/<name>
|
||||||
|
// 条目为相对任务目录的完整路径(不带尾部斜杠),目录/文件分节、各自排序,空节省略。
|
||||||
async function toolListDirectory(ctx, args) {
|
async function toolListDirectory(ctx, args) {
|
||||||
// 对齐 ls 语义:path 缺省或为空字符串时等同于当前目录(任务目录)。
|
// 对齐 ls 语义:path 缺省或为空字符串时等同于当前目录(任务目录)。
|
||||||
const p = typeof args.path === 'string' && args.path.trim() ? args.path : '.';
|
const p = typeof args.path === 'string' && args.path.trim() ? args.path : '.';
|
||||||
const abs = resolveInTask(ctx.taskDir, p);
|
const abs = resolveInTask(ctx.taskDir, p);
|
||||||
const entries = await fsp.readdir(abs, { withFileTypes: true });
|
const entries = await fsp.readdir(abs, { withFileTypes: true });
|
||||||
|
const rel = relOf(ctx.taskDir, abs);
|
||||||
const dirs = []; const files = [];
|
const dirs = []; const files = [];
|
||||||
for (const e of entries) {
|
for (const e of entries) {
|
||||||
if (e.isDirectory()) dirs.push(e.name + '/');
|
const full = rel === '.' ? e.name : rel + '/' + e.name;
|
||||||
else files.push(e.name);
|
if (e.isDirectory()) dirs.push(full);
|
||||||
|
else files.push(full);
|
||||||
}
|
}
|
||||||
dirs.sort(); files.sort();
|
dirs.sort(); files.sort();
|
||||||
const all = dirs.concat(files);
|
const total = dirs.length + files.length;
|
||||||
const shown = all.slice(0, 500);
|
if (total === 0) return 'Directory is empty';
|
||||||
let out = '目录 ' + relOf(ctx.taskDir, abs) + ' 下的文件与子目录:\n' + shown.join('\n');
|
const shownDirs = dirs.slice(0, 500);
|
||||||
if (all.length > shown.length) out += '\n...(共 ' + all.length + ' 项,已截断)';
|
const shownFiles = files.slice(0, 500 - shownDirs.length);
|
||||||
if (all.length === 0) out = '(目录为空)';
|
const sections = [];
|
||||||
|
if (shownDirs.length) sections.push('# Folders:\n' + shownDirs.join('\n'));
|
||||||
|
if (shownFiles.length) sections.push('# Files:\n' + shownFiles.join('\n'));
|
||||||
|
let out = sections.join('\n\n');
|
||||||
|
if (shownDirs.length + shownFiles.length < total) out += '\n\n...(' + total + ' entries total, truncated)';
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user