summaryrefslogtreecommitdiff
path: root/examples/tools
diff options
context:
space:
mode:
authorT <t@tjp.lol>2026-05-27 07:59:03 -0600
committerT <t@tjp.lol>2026-05-27 07:59:24 -0600
commitb72a405534d6be019573ee0a806014e2713fe55e (patch)
tree7c4b8685f2d98382b5c0798e7ce1034cc1617bb2 /examples/tools
parent2b937beb0b5635d89df84cfd112dabca34018afe (diff)
.panto/tools/ resolution
Diffstat (limited to 'examples/tools')
-rw-r--r--examples/tools/wc.lua39
1 files changed, 39 insertions, 0 deletions
diff --git a/examples/tools/wc.lua b/examples/tools/wc.lua
new file mode 100644
index 0000000..33e2326
--- /dev/null
+++ b/examples/tools/wc.lua
@@ -0,0 +1,39 @@
+-- Single-tool form: a Lua file under `tools/` returns one registration
+-- table, and panto registers it as if `panto.register_tool` had been
+-- called on the value. Drop this file into one of:
+-- ${XDG_CONFIG_HOME:-$HOME/.config}/panto/tools/wc.lua (user-global)
+-- ./.panto/tools/wc.lua (project-local)
+--
+-- Or use the directory form `<somewhere>/tools/wc/init.lua` if you want
+-- to `require` sibling modules from the same directory.
+
+return {
+ name = "wc",
+ description = "Count bytes, characters, words, and lines in a string. " ..
+ "Like `wc`, but operates on text passed in directly rather than on files.",
+ schema = {
+ type = "object",
+ properties = {
+ text = {
+ type = "string",
+ description = "The text to measure.",
+ },
+ },
+ required = { "text" },
+ },
+ handler = function(input)
+ local text = input.text
+ local bytes = #text
+ -- UTF-8 character count: utf8.len returns nil on invalid sequences;
+ -- fall back to byte count in that case so we always return something.
+ local chars = utf8.len(text) or bytes
+ local _, words = text:gsub("%S+", "")
+ local _, newlines = text:gsub("\n", "")
+ -- Match `wc -l` semantics: count newlines, not lines. A trailing
+ -- non-newline-terminated line is *not* counted, matching wc(1).
+ return string.format(
+ "bytes=%d chars=%d words=%d lines=%d",
+ bytes, chars, words, newlines
+ )
+ end,
+}