aboutsummaryrefslogtreecommitdiff
path: root/examples/wrap/wrap.tm
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-09-01 16:44:58 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-09-01 16:44:58 -0400
commited50c5fefb8892ad2ba5262491669f268ddbd436 (patch)
treee93a6a8b7e963c37a001691751d6845d10e0cbf8 /examples/wrap/wrap.tm
parent02a99d24a310c04622a875dcf4b0c6fd2de71332 (diff)
Overhaul code to stop keeping examples and libraries in the same repo,
but instead spin each out into its own repo.
Diffstat (limited to 'examples/wrap/wrap.tm')
-rw-r--r--examples/wrap/wrap.tm102
1 files changed, 0 insertions, 102 deletions
diff --git a/examples/wrap/wrap.tm b/examples/wrap/wrap.tm
deleted file mode 100644
index bae01739..00000000
--- a/examples/wrap/wrap.tm
+++ /dev/null
@@ -1,102 +0,0 @@
-HELP := "
- wrap: A tool for wrapping lines of text
-
- usage: wrap [--help] [files...] [--width=80] [--inplace=no] [--min_split=3] [--no-rewrap] [--hyphen='-']
- --help: Print this message and exit
- [files...]: The files to wrap (stdin is used if no files are provided)
- --width=N: The width to wrap the text
- --inplace: Whether or not to perform the modification in-place or print the output
- --min-split=N: The minimum amount of text on either end of a hyphenation split
- --rewrap|--no-rewrap: Whether to rewrap text that is already wrapped or only split long lines
- --hyphen='-': The text to use for hyphenation
-"
-
-UNICODE_HYPHEN := "\{hyphen}"
-
-func unwrap(text:Text, preserve_paragraphs=yes, hyphen=UNICODE_HYPHEN -> Text)
- if preserve_paragraphs
- paragraphs := text.split($/{2+ nl}/)
- if paragraphs.length > 1
- return "\n\n".join([unwrap(p, hyphen=hyphen, preserve_paragraphs=no) for p in paragraphs])
-
- return text.replace("$(hyphen)\n", "")
-
-func wrap(text:Text, width:Int, min_split=3, hyphen="-" -> Text)
- if width <= 0
- fail("Width must be a positive integer, not $width")
-
- if 2*min_split - hyphen.length > width
- fail("
- Minimum word split length ($min_split) is too small for the given wrap width ($width)!
-
- I can't fit a $(2*min_split - hyphen.length)-wide word on a line without splitting it,
- ... and I can't split it without splitting into chunks smaller than $min_split.
- ")
-
- lines : @[Text]
- line := ""
- for word in text.split($/{whitespace}/)
- letters := word.split()
- skip if letters.length == 0
-
- while not _can_fit_word(line, letters, width)
- line_space := width - line.length
- if line != "" then line_space -= 1
-
- if min_split > 0 and line_space >= min_split + hyphen.length and letters.length >= 2*min_split
- # Split word with a hyphen:
- split := line_space - hyphen.length
- split = split _max_ min_split
- split = split _min_ (letters.length - min_split)
- if line != "" then line ++= " "
- line ++= ((++: letters.to(split)) or "") ++ hyphen
- letters = letters.from(split + 1)
- else if line == ""
- # Force split word without hyphenation:
- if line != "" then line ++= " "
- line ++= (++: letters.to(line_space)) or ""
- letters = letters.from(line_space + 1)
- else
- pass # Move to next line
-
- lines.insert(line)
- line = ""
-
- if letters.length > 0
- if line != "" then line ++= " "
- line ++= (++: letters) or ""
-
- if line != ""
- lines.insert(line)
-
- return "\n".join(lines)
-
-func _can_fit_word(line:Text, letters:[Text], width:Int -> Bool; inline)
- if line == ""
- return letters.length <= width
- else
- return line.length + 1 + letters.length <= width
-
-func main(files:[Path], width=80, inplace=no, min_split=3, rewrap=yes, hyphen=UNICODE_HYPHEN)
- if files.length == 0
- files = [(/dev/stdin)]
-
- for file in files
- text := file.read() or exit("Could not read file: $file")
-
- if rewrap
- text = unwrap(text)
-
- out := if file.is_file() and inplace
- file
- else
- (/dev/stdout)
-
- first := yes
- wrapped_paragraphs : @[Text]
- for paragraph in text.split($/{2+ nl}/)
- wrapped_paragraphs.insert(
- wrap(paragraph, width=width, min_split=min_split, hyphen=hyphen)
- )
-
- out.write("\n\n".join(wrapped_paragraphs[]) ++ "\n")