Nicer api and added back scroll-up/scroll-down

This commit is contained in:
Vivianne 2024-02-11 00:02:09 -05:00
parent 164639f223
commit 1a560ea3d3
1 changed files with 41 additions and 31 deletions

View File

@ -15,6 +15,8 @@
show-cursor show-cursor
save-cursor-position save-cursor-position
restore-cursor-position restore-cursor-position
scroll-up
scroll-down
cursor-up cursor-up
cursor-down cursor-down
cursor-forward cursor-forward
@ -68,118 +70,126 @@
(define %SHOW-CURSOR "?25h") (define %SHOW-CURSOR "?25h")
(define %HIDE-CURSOR "?25l") (define %HIDE-CURSOR "?25l")
(define (reset port) (define* (reset #:optional (port #t))
"Reset the terminal to its default style, removing any active styles" "Reset the terminal to its default style, removing any active styles"
(format port (string-append %CSI "0m"))) (format port (string-append %CSI "0m")))
(define (set-foreground-color port r g b) (define* (set-foreground-color hex #:optional (port #t))
"Sets the default foreground color" "Sets the default foreground color"
(format port (string-append %OSC %SET-FOREGROUND-COLOR) (make-color r g b))) (format port (string-append %OSC %SET-FOREGROUND-COLOR) (color-styled (make-foreground hex))))
(define (set-background-color port r g b) (define* (set-background-color hex #:optional (port #t))
"Sets the default background color" "Sets the default background color"
(format port (string-append %OSC %SET-BACKGROUND-COLOR) (make-color r g b))) (format port (string-append %OSC %SET-BACKGROUND-COLOR) (color-styled (make-background hex))))
(define (set-cursor-color port r g b) (define* (set-cursor-color hex #:optional (port #t))
"Sets the cursor color" "Sets the cursor color"
(format port (string-append %OSC %SET-CURSOR-COLOR) (make-color r g b))) (format port (string-append %OSC %SET-CURSOR-COLOR) (color-styled (make-foreground hex))))
(define (restore-screen port) (define* (restore-screen #:optional (port #t))
"Restores a previously saved screen state" "Restores a previously saved screen state"
(format port (string-append %CSI %RESTORE-SCREEN))) (format port (string-append %CSI %RESTORE-SCREEN)))
(define (save-screen port) (define* (save-screen #:optional (port #t))
"Saves the screen state" "Saves the screen state"
(format port (string-append %CSI %SAVE-SCREEN))) (format port (string-append %CSI %SAVE-SCREEN)))
(define (alt-screen port) (define* (alt-screen #:optional (port #t))
"Switches to the alternate screen buffer. The former view can be restored with exit-alt-screen" "Switches to the alternate screen buffer. The former view can be restored with exit-alt-screen"
(format port (string-append %CSI %ALT-SCREEN))) (format port (string-append %CSI %ALT-SCREEN)))
(define (exit-alt-screen port) (define* (exit-alt-screen #:optional (port #t))
"Exits the alternate screen buffer and returns to the former terminal window" "Exits the alternate screen buffer and returns to the former terminal window"
(format port (string-append %CSI %EXIT-ALT-SCREEN))) (format port (string-append %CSI %EXIT-ALT-SCREEN)))
(define (clear-screen port) (define* (clear-screen #:optional (port #t))
"Clears the visible portion of the terminal" "Clears the visible portion of the terminal"
(format port (string-append %CSI %ERASE-DISPLAY) 2) (format port (string-append %CSI %ERASE-DISPLAY) 2)
(move-cursor port 1 1)) (move-cursor port 1 1))
(define (move-cursor port y x)
(define* (scroll-up #:optional (amount 1) (port #t))
(format port (string-append %CSI %SCROLL-UP) amount))
(define* (scroll-down #:optional (amount 1) (port #t))
(format port (string-append %CSI %SCROLL-DOWN) amount))
(define* (move-cursor y x #:optional (port #t))
"Moves the cursor to a given position" "Moves the cursor to a given position"
(format port (string-append %CSI %CURSOR-POSITION) y x)) (format port (string-append %CSI %CURSOR-POSITION) y x))
(define (hide-cursor port) (define* (hide-cursor #:optional (port #t))
"Hides the cursor" "Hides the cursor"
(format port (string-append %CSI %HIDE-CURSOR))) (format port (string-append %CSI %HIDE-CURSOR)))
(define (show-cursor port) (define* (show-cursor #:optional (port #t))
"Shows the cursor" "Shows the cursor"
(format port (string-append %CSI %SHOW-CURSOR))) (format port (string-append %CSI %SHOW-CURSOR)))
(define (save-cursor-position port) (define* (save-cursor-position #:optional (port #t))
"Saves the cursor position" "Saves the cursor position"
(format port (string-append %CSI %SAVE-CURSOR-POSITION))) (format port (string-append %CSI %SAVE-CURSOR-POSITION)))
(define (restore-cursor-position port) (define* (restore-cursor-position #:optional (port #t))
"Restores a saved cursor position" "Restores a saved cursor position"
(format port (string-append %CSI %RESTORE-CURSOR-POSITION))) (format port (string-append %CSI %RESTORE-CURSOR-POSITION)))
(define* (cursor-up port #:optional (distance 1)) (define* (cursor-up #:optional (distance 1) (port #t))
"Moves the cursor up a given number of lines" "Moves the cursor up a given number of lines"
(format port (string-append %CSI %CURSOR-UP) distance)) (format port (string-append %CSI %CURSOR-UP) distance))
(define* (cursor-down port #:optional (distance 1)) (define* (cursor-down #:optional (distance 1) (port #t))
"Moves the cursor down a given number of lines" "Moves the cursor down a given number of lines"
(format port (string-append %CSI %CURSOR-DOWN) distance)) (format port (string-append %CSI %CURSOR-DOWN) distance))
(define* (cursor-forward port #:optional (distance 1)) (define* (cursor-forward #:optional (distance 1) (port #t))
"Moves the cursor forward a given number of lines" "Moves the cursor forward a given number of lines"
(format port (string-append %CSI %CURSOR-FORWARD) distance)) (format port (string-append %CSI %CURSOR-FORWARD) distance))
(define* (cursor-back port #:optional (distance 1)) (define* (cursor-back #:optional (distance 1) (port #t))
"Moves the cursor backwards a given number of lines" "Moves the cursor backwards a given number of lines"
(format port (string-append %CSI %CURSOR-BACK) distance)) (format port (string-append %CSI %CURSOR-BACK) distance))
(define* (cursor-next-line port #:optional (distance 1)) (define* (cursor-next-line #:optional (distance 1) (port #t))
"Moves the cursor down a given number of lines and places it at the beginning of the line" "Moves the cursor down a given number of lines and places it at the beginning of the line"
(format port (string-append %CSI %CURSOR-NEXT-LINE) distance)) (format port (string-append %CSI %CURSOR-NEXT-LINE) distance))
(define* (cursor-prev-line port #:optional (distance 1)) (define* (cursor-prev-line #:optional (distance 1) (port #t))
"Moves the cursor up a given number of lines and places it at the beginning of the line" "Moves the cursor up a given number of lines and places it at the beginning of the line"
(format port (string-append %CSI %CURSOR-PREV-LINE) distance)) (format port (string-append %CSI %CURSOR-PREV-LINE) distance))
(define (clear-line port) (define* (clear-line #:optional (port #t))
"Clears the current line" "Clears the current line"
(format port (string-append %CSI %ERASE-ENTIRE-LINE))) (format port (string-append %CSI %ERASE-ENTIRE-LINE)))
(define (clear-line-left port) (define* (clear-line-left #:optional (port #t))
"Clears the line to the left of the cursor" "Clears the line to the left of the cursor"
(format port (string-append %CSI %ERASE-LINE-LEFT))) (format port (string-append %CSI %ERASE-LINE-LEFT)))
(define (clear-line-right port) (define* (clear-line-right #:optional (port #t))
"Clears the line to the right of the cursor" "Clears the line to the right of the cursor"
(format port (string-append %CSI %ERASE-LINE-RIGHT))) (format port (string-append %CSI %ERASE-LINE-RIGHT)))
(define (clear-lines port n) (define* (clear-lines n #:optional (port #t))
"Clears a given number of lines" "Clears a given number of lines"
(define clear (format #f (string-append %CSI %ERASE-LINE) 2)) (define clear (format #f (string-append %CSI %ERASE-LINE) 2))
(define up (format #f (string-append %CSI %CURSOR-UP) 1)) (define up (format #f (string-append %CSI %CURSOR-UP) 1))
(define l (make-list n (string-append up clear))) (define l (make-list n (string-append up clear)))
(format port (string-append clear (string-join l "")))) (format port (string-append clear (string-join l ""))))
(define (change-scrolling-region port top bottom) (define* (change-scrolling-region top bottom #:optional (port #t))
"Sets the scrolling region of the terminal" "Sets the scrolling region of the terminal"
(format port (string-append %CSI %CHANGE-SCROLLING-REGION) top bottom)) (format port (string-append %CSI %CHANGE-SCROLLING-REGION) top bottom))
(define (insert-lines port n) (define* (insert-lines n #:optional (port #t))
"Inserts the given number of lines at the top of the scrollable region, pushing lines below down" "Inserts the given number of lines at the top of the scrollable region, pushing lines below down"
(format port (string-append %CSI %INSERT-LINE) n)) (format port (string-append %CSI %INSERT-LINE) n))
(define (delete-lines port n) (define* (delete-lines n #:optional (port #t))
"Deletes the given number of lines, pulling any lines in the scrollable region below up" "Deletes the given number of lines, pulling any lines in the scrollable region below up"
(format port (string-append %CSI %DELETE-LINE) n)) (format port (string-append %CSI %DELETE-LINE) n))
(define (set-window-title port title) (define* (set-window-title title #:optional (port #t))
"Sets the terminal window title" "Sets the terminal window title"
(format port (string-append %OSC %SET-WINDOW-TITLE) title)) (format port (string-append %OSC %SET-WINDOW-TITLE) title))