From 422c425c7612ca68fd3d4f5a2b7c263afced2593 Mon Sep 17 00:00:00 2001 From: Mike Pelletier Date: Thu, 22 Apr 2021 08:48:41 -0400 Subject: [PATCH] Sort and present line-based data as it is read A simple program written in several languages for the purpose of examining these languages. --- .gitignore | 3 +++ Makefile | 5 ++++ livesort_hssp/__init__.py | 5 ++++ livesort_hssp/livesort.lissp | 44 +++++++++++++++++++++++++++++++ livesort_hssp/test_livesort.lissp | 8 ++++++ 5 files changed, 65 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 livesort_hssp/__init__.py create mode 100644 livesort_hssp/livesort.lissp create mode 100644 livesort_hssp/test_livesort.lissp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d9a7c3d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/livesort_hssp/livesort.py +/livesort_hssp/test_livesort.py +__pycache__ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a3ad7e6 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +test: + python3.8 -m unittest + +clean: + rm livesort_hssp/test_livesort.py livesort_hssp/livesort.py diff --git a/livesort_hssp/__init__.py b/livesort_hssp/__init__.py new file mode 100644 index 0000000..8d51c2b --- /dev/null +++ b/livesort_hssp/__init__.py @@ -0,0 +1,5 @@ +from hissp.reader import transpile +transpile(__package__, "livesort", "test_livesort") +del transpile + +from livesort_hssp.livesort import livesort, render diff --git a/livesort_hssp/livesort.lissp b/livesort_hssp/livesort.lissp new file mode 100644 index 0000000..b06c6b4 --- /dev/null +++ b/livesort_hssp/livesort.lissp @@ -0,0 +1,44 @@ +(hissp.basic.._macro_.prelude) + +(defmacro setq (symbol value) + "lol this isn't even my lisp" + `(setitem (locals) (quote ,symbol) ,value)) + +(define livesort + (lambda (next-line ; iterable function + print ; function to write output + height ; window/screen height in lines + width ; window/screen width in characters + ) + (let (lines (list) ; sorted lines + cursor 0 ; line number to keep on screen and highlight + quitting? False ; set flag to exit loop + ) + ;; the cursor is thought to exist between line `cursor` and `cursor - 1` + (any-for line next-line + (let (lineno (bisect..bisect lines line)) + (unless (gt lineno cursor) + (setq cursor (add cursor 1))) + (.insert lines lineno line) + (print (.format "c{}" (render lines cursor height width)))) + quitting?)))) + +(define render + (lambda (lines ; sequence of sorted lines + cursor ; line to keep on-screen + height ; window height + width ; and width + ) + (.join "\n" + (map (lambda (line) (getitem line (slice 0 width))) + (getitem lines + (let (r (ifloordiv height 2)) + (slice (max 0 (sub cursor r)) + (min (len lines) (add cursor r))))))))) + + +(when (eq __name__ '__main__) + (livesort sys..stdin + sys..stdout.write + (int (|| (os..getenv 'LINES) "24")) + (int (|| (os..getenv 'COLUMNS) "80")))) diff --git a/livesort_hssp/test_livesort.lissp b/livesort_hssp/test_livesort.lissp new file mode 100644 index 0000000..e88375d --- /dev/null +++ b/livesort_hssp/test_livesort.lissp @@ -0,0 +1,8 @@ +(hissp.basic.._macro_.prelude) + +(deftype TestLivesort (unittest..TestCase) + test_render + (lambda (self) + (.assertEqual self "----" (livesort_hssp..render '(.#"-----") 0 4 4)))) + +(when (eq __name__ '__main__) (hissp.reader..transpile __package__ 'test_livesort))