Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-mcp

Model Context Protocol server for Pudu language documentation and compiler tools

0.1.1Apache-2.01

InstallClose

TextBounds.pudu

Pudu34 lines1.2 KB

GitHub ↗
1/** @Utils.TextBounds.Helper — bounded text and match snippets */2module PuduLangMcp.Utils.TextBounds34import Std.Math as Math5import Std.Text as Text67/// Text of at most `limit` characters, with a line saying how much was cut.8export 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}1415/// `width` characters around the first match of `needle`, on one line.16export 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}2930/// Text with line breaks and tabs folded into single spaces.31export fn singleLine(text: Str) -> Str {32  Text.join(&Text.words(text.replace("\r", " ").replace("\n", " ").replace("\t", " ")), " ")33}34