From 06e062b0a51c616ab01b6248565884c7346ae33c Mon Sep 17 00:00:00 2001 From: Matthieu Jolimaitre Date: Thu, 26 Jun 2025 14:07:21 +0200 Subject: [PATCH] Initial commit: LWP (Line Wrapping Program) with deno compile and formatting Added support for deno compilation and formatting to .deno.json file. Implemented basic line wrapping functionality in src/lwrap.ts, using Wrapper class to wrap lines at 80 characters. --- .gitignore | 1 + build | 8 ++++++ deno.json | 7 +++++ src/lwrap.ts | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++ test/input.txt | 16 +++++++++++ 5 files changed, 107 insertions(+) create mode 100644 .gitignore create mode 100755 build create mode 100644 deno.json create mode 100755 src/lwrap.ts create mode 100644 test/input.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c41cc9e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target \ No newline at end of file diff --git a/build b/build new file mode 100755 index 0000000..4897739 --- /dev/null +++ b/build @@ -0,0 +1,8 @@ +#!/usr/bin/bash +set -e +cd "$(dirname "$(realpath "$0")")" + + +rm -fr target +mkdir -p target +deno compile --output target/lwrap src/lwrap.ts diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..9f4c051 --- /dev/null +++ b/deno.json @@ -0,0 +1,7 @@ +{ + "fmt": { + "useTabs": true, + "lineWidth": 120, + "semiColons": false + } +} diff --git a/src/lwrap.ts b/src/lwrap.ts new file mode 100755 index 0000000..e9cf3bf --- /dev/null +++ b/src/lwrap.ts @@ -0,0 +1,75 @@ +#!/usr/bin/env -S deno run + +async function main() { + const input = readable_text(Deno.stdin.readable) + const output = writable_text(Deno.stdout.writable).getWriter() + const wrapper = new Wrapper(80) + for await (const chunk of input) { + const wrapped = wrapper.wrap(chunk) + for (const chunk of wrapped) await output.write(chunk) + } +} + +function readable_text(readable: ReadableStream>): ReadableStream { + const decoder = new TextDecoderStream() + readable.pipeTo(decoder.writable) + return decoder.readable +} + +function writable_text(writable: WritableStream>): WritableStream { + const decoder = new TextEncoderStream() + decoder.readable.pipeTo(writable) + return decoder.writable +} + +class Wrapper { + current_length = 0 + prev_sep = "" + + public constructor( + private max_width: number, + ) {} + + *wrap(chunk: string) { + let is_first = true + for (const line of chunk.split("\n")) { + this.current_length = 0 + if (is_first) is_first = false + else yield "\n" + for (const word of this.split(line)) { + const length_after = this.current_length + word.word.length + this.prev_sep.length + if (length_after > 80) { + yield "\n" + yield word.word + this.prev_sep = word.sep + this.current_length = word.word.length + } else { + yield this.prev_sep + yield word.word + this.prev_sep = word.sep + this.current_length = length_after + } + } + } + } + + *split(chunk: string) { + const separators = [" "] + let word_start = 0 + let index = 0 + for (const char of chunk) { + if (separators.includes(char)) { + const word = chunk.substring(word_start, index) + const sep = char + yield { word, sep } + word_start = index + 1 + } + index += 1 + } + const word = chunk.substring(word_start, index) + const sep = "" + if (word_start != index) yield { word, sep } + } +} + +if (import.meta.main) await main() diff --git a/test/input.txt b/test/input.txt new file mode 100644 index 0000000..6f2c944 --- /dev/null +++ b/test/input.txt @@ -0,0 +1,16 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. + +Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, consectetur adipiscing elit. + + +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor +incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis +nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. +Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu +fugiat nulla + + Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu +fugiat nulla pariatur. Ut enim ad minim veniam, quis nostrud exercitation +ullamco laboris nisi ut aliquip ex ea commodo consequat. Sed do eiusmod tempor +incididunt ut labore et dolore magna aliqua. Lorem ipsum dolor sit amet, +consectetur adipiscing \ No newline at end of file