
TextBounds.pudu
Pudu34 lines1.2 KB
1/** @Utils.TextBounds.Helper — bounded text and match snippets */2module PuduLangMcp.Utils.TextBounds34import Std.Math as Math5import Std.Text as Text678export fn bounded(text: Str, limit: Int) -> Str {9 let total = text.length()10 if total <= limit { return text }11 let kept = text.take(limit)12 kept + "\n[truncated: " + show(total - kept.length()) + " more characters]"13}141516export fn snippet(text: Str, needle: Str, width: Int) -> Str {17 let flat = singleLine(text)18 let found = if needle.isEmpty() { None } else { Text.find(flat.toLower(), needle.toLower()) }19 match found {20 case None => if flat.length() > width { flat.take(width) + "…" } else { flat }21 case Some(at) => {22 let start = Math.max(at - width / 2, 0)23 let before = if start > 0 { "…" } else { "" }24 let after = if start + width < flat.length() { "…" } else { "" }25 before + flat.slice(start, start + width) + after26 }27 }28}293031export fn singleLine(text: Str) -> Str {32 Text.join(&Text.words(text.replace("\r", " ").replace("\n", " ").replace("\t", " ")), " ")33}34