Sort and present line-based data as it is read
A simple program written in several languages for the purpose of examining these languages.
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
/livesort_hssp/livesort.py
|
||||||
|
/livesort_hssp/test_livesort.py
|
||||||
|
__pycache__
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
test:
|
||||||
|
python3.8 -m unittest
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm livesort_hssp/test_livesort.py livesort_hssp/livesort.py
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
from hissp.reader import transpile
|
||||||
|
transpile(__package__, "livesort", "test_livesort")
|
||||||
|
del transpile
|
||||||
|
|
||||||
|
from livesort_hssp.livesort import livesort, render
|
||||||
@@ -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"))))
|
||||||
@@ -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))
|
||||||
Reference in New Issue
Block a user