From b72a405534d6be019573ee0a806014e2713fe55e Mon Sep 17 00:00:00 2001 From: T Date: Wed, 27 May 2026 07:59:03 -0600 Subject: .panto/tools/ resolution --- examples/tools/wc.lua | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 examples/tools/wc.lua (limited to 'examples') 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 `/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, +} -- cgit v1.3