diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-09 15:28:48 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-09 15:28:48 -0400 |
| commit | 81bd84042352724f60b2c7fe5394f069b9a75880 (patch) | |
| tree | 4cde6f660e92ed960522d6b53737602f6a6f42ba /examples/wrap.tm | |
| parent | 02eefdd52c7d1a8f5ac896c22d5ff6aff065bf72 (diff) | |
Improve wrap example and fix it to no longer use files module
Diffstat (limited to 'examples/wrap.tm')
| -rw-r--r-- | examples/wrap.tm | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/examples/wrap.tm b/examples/wrap.tm index 59218de7..6a7131cc 100644 --- a/examples/wrap.tm +++ b/examples/wrap.tm @@ -1,16 +1,16 @@ HELP := " wrap: A tool for wrapping lines of text that are piped in through standard input - usage: wrap [--help] [--width=80] [--min_split=3] [--no-rewrap] [--hyphen='-'] + usage: wrap [--help] [--file=/dev/stdin] [--width=80] [--inplace=no] [--min_split=3] [--no-rewrap] [--hyphen='-'] --help: Print this message and exit + --file: The file to wrap --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 " -file := use ./file.tm - UNICODE_HYPHEN := Text.from_codepoint_names(["hyphen"]) func unwrap(text:Text, preserve_paragraphs=yes, hyphen="-")->Text: @@ -77,16 +77,26 @@ func _can_fit_word(line:Text, letters:[Text], width:Int; inline)->Bool: else: return line.length + 1 + letters.length <= width -func main(width=80, min_split=3, rewrap=yes, hyphen="-"): - when file.read("/dev/stdin") is Failure(reason): - fail(reason) - is Success(text): +func main(files:[Path], width=80, inplace=no, min_split=3, rewrap=yes, hyphen="-"): + if files.length == 0: + files = [(/dev/stdin)] + + for file in files: + text := file:read() + 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}/): - if not first: - say(\n, newline=no) - say(wrap(paragraph, width=width, min_split=min_split, hyphen=hyphen)) - first = no + wrapped_paragraphs:insert( + wrap(paragraph, width=width, min_split=min_split, hyphen=hyphen) + ) + + out:write(\n\n:join(wrapped_paragraphs) ++ \n) |
