aboutsummaryrefslogtreecommitdiff
path: root/module/vulgar.scm
blob: 80bff5f623f01e0d9676500a2464bb514fe9eaab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)))))))