ztcl::code grep
Searches the files of the tree and gives the hits by file.
ztcl::code grep PATTERN ?PATH ...? ?-re? ?-w? ?-i? ?-glob G? ?-c N? ?-n MAX? ?-files?
Description
PATTERN is literal unless -re, which makes it a regular expression. -w matches whole words and -i ignores case. -glob G keeps the files that match G: a pattern with no / matches the name of the file, and a pattern with a / matches the full path. G can have several patterns with commas, and -glob can come more than once. -c N shows N lines before and after each hit, with - after the number of a line of context. -n MAX shows at most MAX hits (60). -files gives the count of hits of each file in place of the lines.
With no PATH, the search is in the current folder. The files are the files of git ls-files -co --exclude-standard in a git tree. Otherwise a walk of the folders skips the folders whose names start with a dot and the build folders. A search skips a binary file, and a file larger than 4 MB that no PATH names. A line of a result loses the white space at its ends. The last line gives the counts.
Returns
The hits, or no match.
Examples
Each example of this page runs after
package require ztcl::code
# A folder with three files, outside a git tree.
set dir /tmp/ztcl-ref-code-[pid]
file delete -force $dir
file mkdir $dir
cd $dir
proc put {path text} {
set ch [open $path w]
puts -nonewline $ch $text
close $ch
}
put a.zig "const std = @import(\"std\");\n\npub const Point = struct {\n x: i32,\n\n pub fn len(p: Point) i32 {\n return p.x;\n }\n};\n"
put b.txt "one\ntwo\ntwo\n"
put c.md "# Title\n\n## Part\n\ntext\n"
# The tests use no model.
set ::ztcl::code::backend {http ""}
set ::ztcl::code::llm_url http://127.0.0.1:9/v1/chat/completions
.
% ztcl::code grep two b.txt
b.txt
2: two
3: two
2 hits in 1 file
% ztcl::code grep {fn \w+} . -re -files
a.zig 1
1 hit in 1 file
% ztcl::code grep zzz .
no match
Errors
% ztcl::code grep
grep PATTERN ?PATH ...? ?-re? ?-w? ?-i? ?-glob G? ?-c N? ?-n MAX? ?-files?
-errorcode ZTCL CODE USAGE
% ztcl::code grep x nope
no file or folder nope
-errorcode ZTCL CODE NOFILE
Facts
- Capability
file. A new interpreter does not have it until the host turns it on.- Tcl release
- Tcl 9.1 and 8.6.
- Since
- ztcl 1.0.0-beta.2