You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.1 KiB
Plaintext

#! /home/samhunter/public_bin/slope
; prattle - small realtime chat for localhost
; author: samhunter@rawtext.club
; https://git.rawtext.club/samhunter/prattle
;
; client
;
(define me (env "USER"))
(define inp (string-format "/join %v\n" me))
(define l #f)
(define screen-width (car (term-size)))
(define screen-height (car (reverse (term-size))))
(define feed 1)
(define run #t)
(define log-file (path-abs (timestamp->date (timestamp) "~/chatlog-%Y%M%D.txt")))
(define writer
(lambda (sock)
(for () (run) ; endless loop
(if (and inp (> (length inp) 0))
(begin
(cond
((regex-match? "^/exit" inp)
(safe-exit))
((regex-match? "^/clear" inp)
(write "\27[0;0H\27[2J:" stderr))
((regex-match? "^/.*" inp)
(write (string-format "%v\n" inp) sock))
(else
(write (string-format "%v: %v\n" me inp) sock)))
(set! inp #f))
(sleep 100)))))
(define reader
(lambda (sock)
(write "\27[0;0H\27[2J:" stderr)
(for () (run) ; endless loop
(if (open? sock)
(begin
(set! l (read-line sock))
(if l
(begin
(write (string-format "\27[s\27[1B\27[%vM\27[%v;0H" feed (- screen-height 1)) stderr)
(write l)
(file-append-to log-file
(string-format "%v|%v\n" (timestamp) (regex-replace "[\r\n]" l "")))
(write "\n\27[u" stderr))))))))
(define get-inp
(lambda ()
(for () (run) ; endless loop
(begin0
(set! inp (read-line))
(write (string-format "\27[0;0H\27[2K:"))))))
(define safe-exit
(lambda ()
(exception-mode-pass)
(write (string-format "### %v left the chat\n" me) sock)
(set! run #f)
; don't pay attention to the stupid shit I'm doing to stderr and stdout here
(write "\r\27[2K" stderr)
(write (string-format "/leave %v\n" me) sock)
(close stdout)
(close stderr)
(close sock)
(exit)))
(define sock (net-conn "localhost" "1168"))
(signal-catch-sigint safe-exit)
(coeval
[ get-inp ]
[ reader sock ]
[ writer sock ]
)