From 0f6a5833fcb94606ab425fc145713b38171b6a21 Mon Sep 17 00:00:00 2001 From: Tobias Hammer Date: Mon, 7 Jan 2013 10:40:08 +0100 Subject: [PATCH] readline/readline: add `readline-newline' and `readline-redisplay' mzrl.rkt: Add and provide readline-newline and readline-readisplay. readline.scrbl: Documentation and example code. --- collects/readline/mzrl.rkt | 14 +++++++++++++- collects/readline/readline.scrbl | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/collects/readline/mzrl.rkt b/collects/readline/mzrl.rkt index 5348578d8b..4e58577b27 100644 --- a/collects/readline/mzrl.rkt +++ b/collects/readline/mzrl.rkt @@ -4,7 +4,8 @@ (provide readline readline-bytes add-history add-history-bytes history-length history-get history-delete - set-completion-function!) + set-completion-function! + readline-newline readline-redisplay) ;; libtermcap needed on some platforms (define libtermcap (with-handlers ([exn:fail? void]) (ffi-lib "libtermcap"))) @@ -117,3 +118,14 @@ ;; make it possible to run Scheme threads while waiting for input (set-ffi-obj! "rl_event_hook" libreadline (_fun -> _int) (lambda () (sync/enable-break real-input-port) 0)) + + +;; force cursor on a new line +(define readline-newline + (get-ffi-obj "rl_crlf" libreadline (_fun -> _void) + (lambda () + (get-ffi-obj "rl_newline" libreadline (_fun -> _void))))) + +;; force redisplay of prompt and current user input +(define readline-redisplay + (get-ffi-obj "rl_forced_update_display" libreadline (_fun -> _void))) diff --git a/collects/readline/readline.scrbl b/collects/readline/readline.scrbl index ba836d00f6..f4ea232895 100644 --- a/collects/readline/readline.scrbl +++ b/collects/readline/readline.scrbl @@ -5,6 +5,7 @@ readline/pread readline/readline racket/contract + ffi/unsafe/atomic (except-in ffi/unsafe ->))) @(define readline "Readline") @@ -240,6 +241,32 @@ Sets @|readline|'s @tt{rl_completion_entry_function} to from @racketmodname[ffi/unsafe], determines the type of value supplied to the @racket[proc].} +@defproc[(readline-newline) void?]{ + +Sets the cursor to the start of a new line.} + + +@defproc[(readline-redisplay) void?]{ + +Forces a redisplay of the @|readline| prompt and current user input. + +The @racket[readline-redisplay] function can be used together with +@racket[readline-newline] to prevent a background thread from +cluttering up the user input by interleaving its output. For example, +an unsafe wrapper function for the thread's output might look like the +following: + +@racketblock[ +(define (with-thread-safe-output output-thunk) + (dynamic-wind + (lambda () + (start-atomic) + (readline-newline)) + output-thunk + (lambda () + (readline-redisplay) + (end-atomic))))]} + @section[#:tag "readline-license"]{License Issues}