aboutsummaryrefslogtreecommitdiff
path: root/module/vulgar.scm
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@hornquist.se>2019-06-01 21:56:52 +0200
committerHugo Hörnquist <hugo@hornquist.se>2019-06-01 21:56:58 +0200
commitb0d552ffe3e336606de907897181a0f0718b3149 (patch)
treef08d02b8bdead2106f1ca2377cd422f3694e3aa2 /module/vulgar.scm
parentRename {terminal => vulgar}. (diff)
downloadcalp-b0d552ffe3e336606de907897181a0f0718b3149.tar.gz
calp-b0d552ffe3e336606de907897181a0f0718b3149.tar.xz
Reword terminal output to better modularization.
Diffstat (limited to 'module/vulgar.scm')
-rw-r--r--module/vulgar.scm44
1 files changed, 44 insertions, 0 deletions
diff --git a/module/vulgar.scm b/module/vulgar.scm
new file mode 100644
index 00000000..80bff5f6
--- /dev/null
+++ b/module/vulgar.scm
@@ -0,0 +1,44 @@
+;;; Commentary:
+
+;; I don't curse, I'm just vulgar.
+
+;;; Code:
+
+(define-module (vulgar)
+ #:use-module (srfi srfi-60)
+ #:use-module (vulgar termios)
+ #:use-module (util)
+ #:export (with-vulgar))
+
+(define-public (cls)
+ (display "\x1b[H") ; Move cursor to the origin
+ (display "\x1b[J") ; Clear everything after cursor
+ )
+
+(define-syntax with-vulgar
+ (syntax-rules ()
+ ((_ thunk)
+ (let* ((ifd (current-input-port))
+ (ofd (current-output-port))
+ (iattr (make-termios))
+ (oattr (make-termios))
+ iattr* oattr*)
+ (dynamic-wind
+ (lambda ()
+ (tcgetattr! iattr ifd)
+ (tcgetattr! oattr ofd)
+
+ ;; Store current settings to enable resetting the terminal later
+ (set! iattr* (copy-termios iattr))
+ (set! oattr* (copy-termios oattr))
+
+ (let ((bits (bitwise-not (bitwise-ior ECHO ICANON))))
+ (set! (lflag iattr) (bitwise-and (lflag iattr) bits))
+ (set! (lflag oattr) (bitwise-and (lflag oattr) bits)))
+
+ (tcsetattr! iattr ifd)
+ (tcsetattr! oattr ofd))
+ thunk
+ (lambda ()
+ (tcsetattr! iattr* ifd)
+ (tcsetattr! oattr* ofd)))))))