forked from WHC/Hermit
Extensions: skills, plugins, MCP, sub-agents; configurable keys
- Skills (SKILL.md discovery, skill tool, /skills /skill) - plugin.json exec tools with permission prompts, timeouts and workspace trust (/plugins) - MCP stdio client: JSON-RPC initialize/tools/list/tools/call, mcp__server__tool, /mcp and sidebar status - task tool with general/explore/plan and .agent/agents/*.md roles, parallel read-only runs, cancel propagation, /agents - Keymap with Ctrl+X leader, keybinds.json overrides and /keys panel - Collapsed thinking, throttled streaming Markdown, leaner tool output, parallel read-only tools - Spawn: close-on-exec pipes and a process-wide fork lock - Tests for keymap, skills, plugins, MCP, sub-agents, shell compaction; docs/plugins.md
This commit is contained in:
+333
-89
@@ -50,6 +50,93 @@ static int is_regular(const char *full) {
|
||||
|
||||
/* --- read_file ----------------------------------------------------------- */
|
||||
|
||||
#define READ_LINE_MAX 2000
|
||||
|
||||
/* Default-profile read: "path · lines a-b of N" header, `N|` numbers,
|
||||
* long lines cut, and the next offset when more remains. */
|
||||
static int read_compact(FILE *fp, const char *path, long offset, long limit,
|
||||
int numbers, cst_tool_result_t *r) {
|
||||
if (offset <= 0)
|
||||
offset = 1;
|
||||
if (limit <= 0)
|
||||
limit = RANGE_DEFAULT_LINES;
|
||||
cst_buf_t b = {0}, line = {0};
|
||||
long lineno = 0, shown = 0, cut_lines = 0;
|
||||
int full = 0, binary = 0, ch;
|
||||
for (;;) {
|
||||
line.len = 0;
|
||||
int got = 0, cr = 0;
|
||||
size_t len = 0;
|
||||
while ((ch = fgetc(fp)) != EOF) {
|
||||
got = 1;
|
||||
if (ch == 0)
|
||||
binary = 1;
|
||||
if (ch == '\n')
|
||||
break;
|
||||
len++;
|
||||
cr = ch == '\r';
|
||||
if (line.len < READ_LINE_MAX + 8) {
|
||||
char cc = (char)ch;
|
||||
cst_buf_append(&line, &cc, 1);
|
||||
}
|
||||
}
|
||||
if (!got || binary)
|
||||
break;
|
||||
lineno++;
|
||||
if (lineno < offset || shown >= limit || full)
|
||||
continue;
|
||||
len -= (size_t)cr;
|
||||
if (b.len + (len < READ_LINE_MAX ? len : READ_LINE_MAX) > RANGE_OUT_MAX) {
|
||||
full = 1;
|
||||
continue;
|
||||
}
|
||||
if (numbers)
|
||||
cst_buf_printf(&b, "%ld|", lineno);
|
||||
if (len > READ_LINE_MAX) {
|
||||
size_t keep = cst_utf8_floor(line.buf, READ_LINE_MAX);
|
||||
cst_buf_append(&b, line.buf, keep);
|
||||
cst_buf_printf(&b, " … [line cut, +%zu bytes]\n", len - keep);
|
||||
cut_lines++;
|
||||
} else {
|
||||
if (len)
|
||||
cst_buf_append(&b, line.buf, len);
|
||||
cst_buf_puts(&b, "\n");
|
||||
}
|
||||
shown++;
|
||||
}
|
||||
cst_buf_free(&line);
|
||||
if (binary) {
|
||||
cst_buf_free(&b);
|
||||
return tool_set_resultf(r, 1,
|
||||
"error: %.200s is a binary file; inspect it with "
|
||||
"run_shell (file, xxd | head)",
|
||||
path);
|
||||
}
|
||||
long last = offset + shown - 1;
|
||||
cst_buf_t out = {0};
|
||||
if (lineno == 0)
|
||||
cst_buf_printf(&out, "%s · empty file\n", path);
|
||||
else if (shown == 0)
|
||||
cst_buf_printf(&out, "%s · %ld line%s; offset %ld is past the end\n", path,
|
||||
lineno, lineno == 1 ? "" : "s", offset);
|
||||
else if (offset == 1 && last == lineno)
|
||||
cst_buf_printf(&out, "%s · %ld line%s\n", path, lineno,
|
||||
lineno == 1 ? "" : "s");
|
||||
else
|
||||
cst_buf_printf(&out, "%s · lines %ld-%ld of %ld\n", path, offset, last,
|
||||
lineno);
|
||||
if (b.buf)
|
||||
cst_buf_append(&out, b.buf, b.len);
|
||||
cst_buf_free(&b);
|
||||
if (shown && last < lineno)
|
||||
cst_buf_printf(&out, "… %ld more line%s; continue with offset=%ld\n",
|
||||
lineno - last, lineno - last == 1 ? "" : "s", last + 1);
|
||||
if (cut_lines)
|
||||
cst_buf_printf(&out, "(%ld long line%s cut at %d bytes)\n", cut_lines,
|
||||
cut_lines == 1 ? "" : "s", READ_LINE_MAX);
|
||||
return tool_take_buf(r, &out, 0);
|
||||
}
|
||||
|
||||
int cst_tool_read_file2(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
cst_tool_result_t *r) {
|
||||
const char *path = tool_arg(c, "path");
|
||||
@@ -70,8 +157,13 @@ int cst_tool_read_file2(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
|
||||
long offset = tool_arg_long(c, "offset", 0);
|
||||
long limit = tool_arg_long(c, "limit", 0);
|
||||
int numbers = tool_arg_bool(c, "line_numbers", 0);
|
||||
int numbers = tool_arg_bool(c, "line_numbers", ctx->compact);
|
||||
|
||||
if (ctx->compact) {
|
||||
int rc = read_compact(fp, path, offset, limit, numbers, r);
|
||||
fclose(fp);
|
||||
return rc;
|
||||
}
|
||||
if (offset <= 0 && limit <= 0 && !numbers) {
|
||||
fclose(fp);
|
||||
size_t n = 0;
|
||||
@@ -168,6 +260,87 @@ static size_t count_occ(const char *hay, size_t hn, const char *needle,
|
||||
return cnt;
|
||||
}
|
||||
|
||||
/* "12, 40, 77" — line numbers of the first few occurrences. */
|
||||
static void occ_lines(const char *hay, size_t hn, const char *nd, size_t nn,
|
||||
char *out, size_t out_sz) {
|
||||
out[0] = 0;
|
||||
size_t used = 0;
|
||||
long line = 1;
|
||||
size_t last = 0;
|
||||
int k = 0;
|
||||
for (size_t i = 0; nn && i + nn <= hn && k < 6;) {
|
||||
const char *p = memchr(hay + i, nd[0], hn - i - nn + 1);
|
||||
if (!p)
|
||||
break;
|
||||
i = (size_t)(p - hay);
|
||||
if (memcmp(p, nd, nn) != 0) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
for (size_t j = last; j < i; j++)
|
||||
line += hay[j] == '\n';
|
||||
last = i;
|
||||
int w = snprintf(out + used, out_sz - used, "%s%ld", k ? ", " : "", line);
|
||||
if (w < 0 || (size_t)w >= out_sz - used)
|
||||
break;
|
||||
used += (size_t)w;
|
||||
k++;
|
||||
i += nn;
|
||||
}
|
||||
}
|
||||
|
||||
static int trimmed_eq(const char *a, size_t an, const char *b, size_t bn) {
|
||||
while (an && isspace((unsigned char)*a))
|
||||
a++, an--;
|
||||
while (an && isspace((unsigned char)a[an - 1]))
|
||||
an--;
|
||||
while (bn && isspace((unsigned char)*b))
|
||||
b++, bn--;
|
||||
while (bn && isspace((unsigned char)b[bn - 1]))
|
||||
bn--;
|
||||
return an && an == bn && memcmp(a, b, an) == 0;
|
||||
}
|
||||
|
||||
/* old_string was not found: lines whose trimmed text equals old_string's
|
||||
* first non-blank line, as "42, 97" (empty when none). */
|
||||
static void near_lines(const char *hay, size_t hn, const char *old, size_t on,
|
||||
char *out, size_t out_sz) {
|
||||
out[0] = 0;
|
||||
const char *f = old;
|
||||
size_t fl = 0;
|
||||
for (size_t i = 0; i < on;) {
|
||||
const char *e = memchr(old + i, '\n', on - i);
|
||||
size_t end = e ? (size_t)(e - old) : on;
|
||||
int blank = 1;
|
||||
for (size_t j = i; j < end; j++)
|
||||
if (!isspace((unsigned char)old[j]))
|
||||
blank = 0;
|
||||
if (!blank) {
|
||||
f = old + i;
|
||||
fl = end - i;
|
||||
break;
|
||||
}
|
||||
i = end + 1;
|
||||
}
|
||||
if (!fl)
|
||||
return;
|
||||
size_t used = 0;
|
||||
long line = 1;
|
||||
int k = 0;
|
||||
for (size_t i = 0; i < hn && k < 5; line++) {
|
||||
const char *e = memchr(hay + i, '\n', hn - i);
|
||||
size_t end = e ? (size_t)(e - hay) : hn;
|
||||
if (trimmed_eq(hay + i, end - i, f, fl)) {
|
||||
int w = snprintf(out + used, out_sz - used, "%s%ld", k ? ", " : "", line);
|
||||
if (w < 0 || (size_t)w >= out_sz - used)
|
||||
break;
|
||||
used += (size_t)w;
|
||||
k++;
|
||||
}
|
||||
i = end + 1;
|
||||
}
|
||||
}
|
||||
|
||||
static char *replace_all(const char *hay, size_t hn, const char *old,
|
||||
size_t on, const char *nw, size_t nwn, int all,
|
||||
size_t *out_len) {
|
||||
@@ -272,17 +445,29 @@ static int plan_edit(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
}
|
||||
int rc = 0;
|
||||
if (cnt == 0) {
|
||||
snprintf(err, err_sz,
|
||||
"error: old_string not found in %.200s; it must match the file "
|
||||
"exactly (whitespace and indentation included) — re-read the "
|
||||
"file and copy the text",
|
||||
path);
|
||||
char near[64];
|
||||
near_lines(pl->oldc, pl->olen, old, on, near, sizeof(near));
|
||||
if (near[0])
|
||||
snprintf(err, err_sz,
|
||||
"error: old_string not found in %.200s, but its first line "
|
||||
"appears (whitespace aside) at line %s — read_file around "
|
||||
"there and copy the exact text, indentation included",
|
||||
path, near);
|
||||
else
|
||||
snprintf(err, err_sz,
|
||||
"error: old_string not found in %.200s; it must match the file "
|
||||
"exactly (whitespace and indentation included) — re-read the "
|
||||
"file (or grep for a distinctive word) and copy the text",
|
||||
path);
|
||||
rc = -1;
|
||||
} else if (cnt > 1 && !all) {
|
||||
char where[64];
|
||||
occ_lines(pl->oldc, pl->olen, old, on, where, sizeof(where));
|
||||
snprintf(err, err_sz,
|
||||
"error: old_string matches %zu places in %.200s; include more "
|
||||
"surrounding lines to make it unique, or set replace_all=true",
|
||||
cnt, path);
|
||||
"error: old_string matches %zu places in %.200s (lines %s%s); "
|
||||
"include more surrounding lines to make it unique, or set "
|
||||
"replace_all=true",
|
||||
cnt, path, where, cnt > 6 ? ", …" : "");
|
||||
rc = -1;
|
||||
} else {
|
||||
pl->newc = replace_all(pl->oldc, pl->olen, old, on, nw, nwn, all,
|
||||
@@ -665,29 +850,54 @@ static int start_rel(const cst_tool_ctx_t *ctx, const char *path,
|
||||
}
|
||||
|
||||
/* list_dir */
|
||||
#define LIST_COUNT_MAX 100000
|
||||
|
||||
typedef struct {
|
||||
cst_buf_t b;
|
||||
int count;
|
||||
int base_depth;
|
||||
int truncated;
|
||||
int more; /* entries past LIST_MAX (counted, not shown) */
|
||||
int truncated; /* stopped counting */
|
||||
} list_ud_t;
|
||||
|
||||
/* Entries directly inside a directory (for the depth boundary). */
|
||||
static int dir_entries(const char *full) {
|
||||
DIR *d = opendir(full);
|
||||
if (!d)
|
||||
return -1;
|
||||
int n = 0;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(d)) != NULL) {
|
||||
if (strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0)
|
||||
n++;
|
||||
}
|
||||
closedir(d);
|
||||
return n;
|
||||
}
|
||||
|
||||
static int list_cb(walker_t *w, const char *rel, const char *full,
|
||||
const struct stat *st, int depth) {
|
||||
(void)full;
|
||||
list_ud_t *u = w->ud;
|
||||
if (u->count >= LIST_MAX) {
|
||||
u->truncated = 1;
|
||||
return 1;
|
||||
if (++u->more >= LIST_COUNT_MAX) {
|
||||
u->truncated = 1;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
const char *name = strrchr(rel, '/');
|
||||
name = name ? name + 1 : rel;
|
||||
for (int i = 0; i < depth; i++)
|
||||
cst_buf_puts(&u->b, " ");
|
||||
if (S_ISDIR(st->st_mode))
|
||||
cst_buf_printf(&u->b, "%s/%s\n", name,
|
||||
skip_dir_name(name) ? " (skipped)" : "");
|
||||
else if (S_ISLNK(st->st_mode))
|
||||
if (S_ISDIR(st->st_mode)) {
|
||||
int k = -1;
|
||||
if (skip_dir_name(name))
|
||||
cst_buf_printf(&u->b, "%s/ (skipped)\n", name);
|
||||
else if (depth + 1 >= w->max_depth && (k = dir_entries(full)) > 0)
|
||||
cst_buf_printf(&u->b, "%s/ (%d entr%s)\n", name, k,
|
||||
k == 1 ? "y" : "ies");
|
||||
else
|
||||
cst_buf_printf(&u->b, "%s/%s\n", name, k == 0 ? " (empty)" : "");
|
||||
} else if (S_ISLNK(st->st_mode))
|
||||
cst_buf_printf(&u->b, "%s@\n", name);
|
||||
else
|
||||
cst_buf_printf(&u->b, "%s (%lld B)\n", name, (long long)st->st_size);
|
||||
@@ -726,9 +936,10 @@ int cst_tool_list_dir(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
free(w.ig.v);
|
||||
if (u.count == 0)
|
||||
cst_buf_puts(&u.b, "(empty)\n");
|
||||
if (u.truncated)
|
||||
cst_buf_printf(&u.b, "... (stopped after %d entries; list a subdirectory)\n",
|
||||
LIST_MAX);
|
||||
if (u.more)
|
||||
cst_buf_printf(&u.b, "… %s%d more entries not shown (list a subdirectory "
|
||||
"or use a smaller depth)\n",
|
||||
u.truncated ? "over " : "", u.more);
|
||||
return tool_take_buf(r, &u.b, 0);
|
||||
}
|
||||
|
||||
@@ -824,15 +1035,16 @@ static int glob_cb(walker_t *w, const char *rel, const char *full,
|
||||
return 0;
|
||||
}
|
||||
|
||||
static glob_ud_t *g_sort_ud;
|
||||
static int idx_by_mtime(const void *a, const void *b) {
|
||||
int ia = *(const int *)a, ib = *(const int *)b;
|
||||
double d = g_sort_ud->mtimes[ib] - g_sort_ud->mtimes[ia];
|
||||
if (d > 0)
|
||||
return 1;
|
||||
if (d < 0)
|
||||
return -1;
|
||||
return strcmp(g_sort_ud->hits[ia], g_sort_ud->hits[ib]);
|
||||
typedef struct {
|
||||
const char *path;
|
||||
double mtime;
|
||||
} glob_hit_t;
|
||||
|
||||
static int hit_by_mtime(const void *a, const void *b) {
|
||||
const glob_hit_t *x = a, *y = b;
|
||||
if (x->mtime != y->mtime)
|
||||
return x->mtime < y->mtime ? 1 : -1;
|
||||
return strcmp(x->path, y->path);
|
||||
}
|
||||
|
||||
int cst_tool_glob(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
@@ -865,22 +1077,24 @@ int cst_tool_glob(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
free(w.ig.v);
|
||||
|
||||
cst_buf_t b = {0};
|
||||
int *idx = malloc((size_t)(u.n ? u.n : 1) * sizeof(int));
|
||||
if (idx) {
|
||||
for (int i = 0; i < u.n; i++)
|
||||
idx[i] = i;
|
||||
g_sort_ud = &u;
|
||||
qsort(idx, (size_t)u.n, sizeof(int), idx_by_mtime);
|
||||
g_sort_ud = NULL;
|
||||
glob_hit_t *hs = malloc((size_t)(u.n ? u.n : 1) * sizeof(*hs));
|
||||
if (hs) {
|
||||
for (int i = 0; i < u.n; i++) {
|
||||
hs[i].path = u.hits[i];
|
||||
hs[i].mtime = u.mtimes[i];
|
||||
}
|
||||
qsort(hs, (size_t)u.n, sizeof(*hs), hit_by_mtime);
|
||||
for (int i = 0; i < u.n && i < GLOB_MAX; i++)
|
||||
cst_buf_printf(&b, "%s\n", u.hits[idx[i]]);
|
||||
cst_buf_printf(&b, "%s\n", hs[i].path);
|
||||
}
|
||||
free(idx);
|
||||
free(hs);
|
||||
if (u.n == 0)
|
||||
cst_buf_printf(&b, "(no files match %s)\n", pattern);
|
||||
else if (u.n > GLOB_MAX || u.truncated)
|
||||
cst_buf_printf(&b, "... (%s%d matches; showing the %d newest)\n",
|
||||
u.truncated ? "over " : "", u.n, GLOB_MAX);
|
||||
cst_buf_printf(&b, "… %s%d more files (showing the %d newest of %s%d; "
|
||||
"narrow the pattern or path)\n",
|
||||
u.truncated ? "over " : "", u.n - GLOB_MAX, GLOB_MAX,
|
||||
u.truncated ? "over " : "", u.n);
|
||||
for (int i = 0; i < u.n; i++)
|
||||
free(u.hits[i]);
|
||||
free(u.hits);
|
||||
@@ -930,19 +1144,24 @@ int cst_workspace_files(const char *ws, char ***out, int max) {
|
||||
return u.n;
|
||||
}
|
||||
|
||||
/* grep */
|
||||
/* grep: matches grouped by file, "N matches in F files" summary. */
|
||||
#define GREP_PER_FILE 20
|
||||
#define GREP_COUNT_MAX 5000
|
||||
|
||||
typedef struct {
|
||||
regex_t re;
|
||||
const char *glob;
|
||||
int context;
|
||||
cst_buf_t b;
|
||||
int matches;
|
||||
int files;
|
||||
int truncated;
|
||||
int matches; /* counted (up to GREP_COUNT_MAX) */
|
||||
int shown;
|
||||
int files; /* text files searched */
|
||||
int hit_files; /* files with a match */
|
||||
int truncated; /* stopped counting */
|
||||
} grep_ud_t;
|
||||
|
||||
static void put_line(cst_buf_t *b, const char *rel, long ln, char sep,
|
||||
const char *s, size_t n) {
|
||||
static void put_line(cst_buf_t *b, long ln, char sep, const char *s,
|
||||
size_t n) {
|
||||
if (n && s[n - 1] == '\n')
|
||||
n--;
|
||||
if (n && s[n - 1] == '\r')
|
||||
@@ -952,11 +1171,23 @@ static void put_line(cst_buf_t *b, const char *rel, long ln, char sep,
|
||||
n = cst_utf8_floor(s, GREP_LINE_MAX);
|
||||
cut = 1;
|
||||
}
|
||||
cst_buf_printf(b, "%s%c%ld%c", rel, sep, ln, sep);
|
||||
cst_buf_printf(b, "%ld%c", ln, sep);
|
||||
cst_buf_append(b, s, n);
|
||||
cst_buf_puts(b, cut ? " …\n" : "\n");
|
||||
}
|
||||
|
||||
static int line_hit(grep_ud_t *u, const char *s, size_t n) {
|
||||
char tmp[GREP_LINE_MAX * 8];
|
||||
size_t m = n < sizeof(tmp) - 1 ? n : sizeof(tmp) - 1;
|
||||
memcpy(tmp, s, m);
|
||||
tmp[m] = 0;
|
||||
if (m && tmp[m - 1] == '\n')
|
||||
tmp[--m] = 0;
|
||||
if (m && tmp[m - 1] == '\r')
|
||||
tmp[--m] = 0;
|
||||
return regexec(&u->re, tmp, 0, NULL, 0) == 0;
|
||||
}
|
||||
|
||||
static void grep_file(grep_ud_t *u, const char *rel, const char *full) {
|
||||
size_t n = 0;
|
||||
char *buf = tool_slurp(full, &n, READ_FILE_MAX);
|
||||
@@ -996,45 +1227,48 @@ static void grep_file(grep_ud_t *u, const char *rel, const char *full) {
|
||||
nl++;
|
||||
i = end;
|
||||
}
|
||||
int last_printed = -1;
|
||||
for (int i = 0; i < nl && !u->truncated; i++) {
|
||||
char tmp[GREP_LINE_MAX * 8];
|
||||
size_t m = ln[i] < sizeof(tmp) - 1 ? ln[i] : sizeof(tmp) - 1;
|
||||
memcpy(tmp, ls[i], m);
|
||||
tmp[m] = 0;
|
||||
if (m && tmp[m - 1] == '\n')
|
||||
tmp[--m] = 0;
|
||||
if (regexec(&u->re, tmp, 0, NULL, 0) != 0)
|
||||
cst_buf_t fb = {0};
|
||||
int fm = 0, fshown = 0, last_printed = -1;
|
||||
for (int i = 0; i < nl; i++) {
|
||||
if (!line_hit(u, ls[i], ln[i]))
|
||||
continue;
|
||||
if (u->matches >= GREP_MAX_MATCHES) {
|
||||
fm++;
|
||||
if (++u->matches >= GREP_COUNT_MAX)
|
||||
u->truncated = 1;
|
||||
if (fshown < GREP_PER_FILE && u->shown < GREP_MAX_MATCHES) {
|
||||
int from = i - u->context < 0 ? 0 : i - u->context;
|
||||
if (from <= last_printed)
|
||||
from = last_printed + 1;
|
||||
else if (u->context && last_printed >= 0)
|
||||
cst_buf_puts(&fb, "--\n");
|
||||
for (int k = from; k < i; k++)
|
||||
put_line(&fb, k + 1, '-', ls[k], ln[k]);
|
||||
put_line(&fb, i + 1, ':', ls[i], ln[i]);
|
||||
last_printed = i;
|
||||
for (int k = i + 1; k <= i + u->context && k < nl; k++) {
|
||||
if (line_hit(u, ls[k], ln[k]))
|
||||
break; /* printed by the main loop */
|
||||
put_line(&fb, k + 1, '-', ls[k], ln[k]);
|
||||
last_printed = k;
|
||||
}
|
||||
fshown++;
|
||||
u->shown++;
|
||||
}
|
||||
if (u->truncated)
|
||||
break;
|
||||
}
|
||||
int from = i - u->context < 0 ? 0 : i - u->context;
|
||||
if (from <= last_printed)
|
||||
from = last_printed + 1;
|
||||
else if (u->context && last_printed >= 0)
|
||||
cst_buf_puts(&u->b, "--\n");
|
||||
for (int k = from; k < i; k++)
|
||||
put_line(&u->b, rel, k + 1, '-', ls[k], ln[k]);
|
||||
put_line(&u->b, rel, i + 1, ':', ls[i], ln[i]);
|
||||
last_printed = i;
|
||||
u->matches++;
|
||||
int to = i + u->context;
|
||||
for (int k = i + 1; k <= to && k < nl; k++) {
|
||||
/* a later matching line is printed by the main loop */
|
||||
char t2[GREP_LINE_MAX * 8];
|
||||
size_t m2 = ln[k] < sizeof(t2) - 1 ? ln[k] : sizeof(t2) - 1;
|
||||
memcpy(t2, ls[k], m2);
|
||||
t2[m2] = 0;
|
||||
if (m2 && t2[m2 - 1] == '\n')
|
||||
t2[--m2] = 0;
|
||||
if (regexec(&u->re, t2, 0, NULL, 0) == 0)
|
||||
break;
|
||||
put_line(&u->b, rel, k + 1, '-', ls[k], ln[k]);
|
||||
last_printed = k;
|
||||
}
|
||||
}
|
||||
if (fm) {
|
||||
u->hit_files++;
|
||||
if (fm == 1)
|
||||
cst_buf_printf(&u->b, "%s\n", rel);
|
||||
else
|
||||
cst_buf_printf(&u->b, "%s (%d matches)\n", rel, fm);
|
||||
if (fb.buf)
|
||||
cst_buf_append(&u->b, fb.buf, fb.len);
|
||||
if (fm > fshown)
|
||||
cst_buf_printf(&u->b, "… %d more in this file\n", fm - fshown);
|
||||
}
|
||||
cst_buf_free(&fb);
|
||||
free(ls);
|
||||
free(ln);
|
||||
free(buf);
|
||||
@@ -1137,14 +1371,24 @@ int cst_tool_grep(const cst_tool_ctx_t *ctx, const ctl_tool_call_t *c,
|
||||
free(w.ig.v);
|
||||
}
|
||||
regfree(&u.re);
|
||||
if (u.matches == 0)
|
||||
if (u.matches == 0) {
|
||||
cst_buf_free(&u.b);
|
||||
cst_buf_printf(&u.b, "(no matches in %d file%s)\n", u.files,
|
||||
u.files == 1 ? "" : "s");
|
||||
else if (u.truncated)
|
||||
cst_buf_printf(&u.b, "... (stopped at %d matches; narrow the pattern or "
|
||||
"path)\n",
|
||||
GREP_MAX_MATCHES);
|
||||
return tool_take_buf(r, &u.b, 0);
|
||||
return tool_take_buf(r, &u.b, 0);
|
||||
}
|
||||
cst_buf_t out = {0};
|
||||
cst_buf_printf(&out, "%s%d match%s in %d file%s", u.truncated ? "over " : "",
|
||||
u.matches, u.matches == 1 ? "" : "es", u.hit_files,
|
||||
u.hit_files == 1 ? "" : "s");
|
||||
if (u.shown < u.matches)
|
||||
cst_buf_printf(&out, " (showing %d; narrow with path/glob or a tighter "
|
||||
"pattern)",
|
||||
u.shown);
|
||||
cst_buf_puts(&out, "\n");
|
||||
cst_buf_append(&out, u.b.buf, u.b.len);
|
||||
cst_buf_free(&u.b);
|
||||
return tool_take_buf(r, &out, 0);
|
||||
}
|
||||
|
||||
#else /* _WIN32: directory tools are not implemented natively */
|
||||
|
||||
Reference in New Issue
Block a user