mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-19 13:49:17 +02:00
making (chibi term edit-line) portable, using from (chibi snow interface)
This commit is contained in:
parent
92daa43114
commit
ac6d0124c4
4 changed files with 50 additions and 49 deletions
|
@ -70,24 +70,19 @@
|
||||||
(history-insert! input-history str)
|
(history-insert! input-history str)
|
||||||
(check str (proc str) lp)))))))
|
(check str (proc str) lp)))))))
|
||||||
|
|
||||||
(define (input-hidden prompt)
|
|
||||||
(show #t prompt)
|
|
||||||
(flush-output-port)
|
|
||||||
(let ((res (with-stty '(not echo) (lambda () (read-line)))))
|
|
||||||
(show #t "\n")
|
|
||||||
res))
|
|
||||||
|
|
||||||
(define (input-password cfg name prompt1 . o)
|
(define (input-password cfg name prompt1 . o)
|
||||||
(let ((prompt2 (or (and (pair? o) (car o))
|
(let ((prompt2 (or (and (pair? o) (car o))
|
||||||
(string-append prompt1 " (confirmation): "))))
|
(string-append prompt1 " (confirmation): "))))
|
||||||
(let lp ()
|
(let lp ()
|
||||||
(let ((password (input-hidden prompt1)))
|
(let ((password (edit-line 'hidden?: #t 'prompt: prompt1)))
|
||||||
|
(newline)
|
||||||
(cond
|
(cond
|
||||||
((equal? password "")
|
((equal? password "")
|
||||||
(show #t "password must be non-empty\n")
|
(show #t "password must be non-empty\n")
|
||||||
(lp))
|
(lp))
|
||||||
(else
|
(else
|
||||||
(let ((conf (input-hidden prompt2)))
|
(let ((conf (edit-line 'hidden?: #t 'prompt: prompt2)))
|
||||||
|
(newline)
|
||||||
(cond
|
(cond
|
||||||
((not (equal? password conf))
|
((not (equal? password conf))
|
||||||
(show #t "password didn't match\n")
|
(show #t "password didn't match\n")
|
||||||
|
|
|
@ -4,15 +4,15 @@
|
||||||
restore-history save-history)
|
restore-history save-history)
|
||||||
(import (scheme base) (scheme char) (scheme read) (scheme write)
|
(import (scheme base) (scheme char) (scheme read) (scheme write)
|
||||||
(scheme file) (scheme process-context) (srfi 1)
|
(scheme file) (scheme process-context) (srfi 1)
|
||||||
(chibi config) (chibi pathname) (chibi show))
|
(chibi config) (chibi pathname) (chibi show)
|
||||||
|
(chibi term edit-line))
|
||||||
(cond-expand
|
(cond-expand
|
||||||
(chibi
|
(chibi
|
||||||
(import (chibi filesystem) (chibi stty)))
|
(import (chibi filesystem)))
|
||||||
(chicken
|
(chicken
|
||||||
(import posix stty)
|
(import posix)
|
||||||
(begin
|
(begin
|
||||||
(define (create-directory* dir) (create-directory dir #t))
|
(define (create-directory* dir) (create-directory dir #t))))
|
||||||
(define (edit-line ))))
|
|
||||||
(sagittarius
|
(sagittarius
|
||||||
(import (only (sagittarius) create-directory)
|
(import (only (sagittarius) create-directory)
|
||||||
(chibi string))
|
(chibi string))
|
||||||
|
@ -30,28 +30,5 @@
|
||||||
(and (not (equal? parent dir))
|
(and (not (equal? parent dir))
|
||||||
(not (file-exists? parent))
|
(not (file-exists? parent))
|
||||||
(create-directory* parent mode)
|
(create-directory* parent mode)
|
||||||
(create-directory dir mode))))))))
|
(create-directory dir mode)))))))))))
|
||||||
(define (with-stty spec thunk)
|
|
||||||
(thunk)))))
|
|
||||||
(cond-expand
|
|
||||||
(chibi
|
|
||||||
(import (chibi term edit-line)))
|
|
||||||
(else
|
|
||||||
(begin
|
|
||||||
(define (get-key ls key . o)
|
|
||||||
(let ((x (memq key ls)))
|
|
||||||
(if (and x (pair? (cdr x))) (cadr x) (and (pair? o) (car o)))))
|
|
||||||
(define (edit-line . args)
|
|
||||||
(let ((in (if (and (pair? args) (input-port? (car args)))
|
|
||||||
(car args)
|
|
||||||
(current-input-port)))
|
|
||||||
(out (if (and (eq? in (car args))
|
|
||||||
(pair? (cdr args))
|
|
||||||
(output-port? (cadr args)))
|
|
||||||
(cadr args)
|
|
||||||
(current-output-port)))
|
|
||||||
(prompter (get-key args 'prompt: "> ")))
|
|
||||||
(display (if (procedure? prompter) (prompter) prompter) out)
|
|
||||||
(flush-output-port out)
|
|
||||||
(read-line in))))))
|
|
||||||
(include "interface.scm"))
|
(include "interface.scm"))
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
;;;; edit-line.scm - pure scheme line editor
|
;;;; edit-line.scm - pure scheme line editor
|
||||||
;;
|
;;
|
||||||
;; Copyright (c) 2011-2012 Alex Shinn. All rights reserved.
|
;; Copyright (c) 2011-2017 Alex Shinn. All rights reserved.
|
||||||
;; BSD-style license: http://synthcode.com/license.txt
|
;; BSD-style license: http://synthcode.com/license.txt
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
@ -11,7 +11,7 @@
|
||||||
(write-char #\[ out)
|
(write-char #\[ out)
|
||||||
(if arg (display arg out))
|
(if arg (display arg out))
|
||||||
(write-char ch out)
|
(write-char ch out)
|
||||||
(flush-output out))
|
(flush-output-port out))
|
||||||
|
|
||||||
;; we use zero-based columns
|
;; we use zero-based columns
|
||||||
(define (terminal-goto-col out n) (terminal-escape out #\G (+ n 1)))
|
(define (terminal-goto-col out n) (terminal-escape out #\G (+ n 1)))
|
||||||
|
@ -430,7 +430,7 @@
|
||||||
(if (< (buffer-row buf) (buffer-max-row buf))
|
(if (< (buffer-row buf) (buffer-max-row buf))
|
||||||
(terminal-up out (- (buffer-max-row buf) (buffer-row buf))))))
|
(terminal-up out (- (buffer-max-row buf) (buffer-row buf))))))
|
||||||
(terminal-goto-col out (buffer-col buf))
|
(terminal-goto-col out (buffer-col buf))
|
||||||
(flush-output out)
|
(flush-output-port out)
|
||||||
(buffer-cleared?-set! buf #f)))
|
(buffer-cleared?-set! buf #f)))
|
||||||
|
|
||||||
(define (buffer-refresh buf out)
|
(define (buffer-refresh buf out)
|
||||||
|
@ -481,7 +481,7 @@
|
||||||
(not (string-index #\newline x))))
|
(not (string-index #\newline x))))
|
||||||
;; fast path - append to end of buffer w/o wrapping to next line
|
;; fast path - append to end of buffer w/o wrapping to next line
|
||||||
(display x out)
|
(display x out)
|
||||||
(flush-output out)
|
(flush-output-port out)
|
||||||
(buffer-col-set! buf (+ (buffer-col buf) len))
|
(buffer-col-set! buf (+ (buffer-col buf) len))
|
||||||
(buffer-cleared?-set! buf #f))
|
(buffer-cleared?-set! buf #f))
|
||||||
(else
|
(else
|
||||||
|
@ -662,7 +662,7 @@
|
||||||
(((buffer-complete? buf) buf)
|
(((buffer-complete? buf) buf)
|
||||||
(command/end-of-line ch buf out return)
|
(command/end-of-line ch buf out return)
|
||||||
(display "\r\n" out)
|
(display "\r\n" out)
|
||||||
(flush-output out)
|
(flush-output-port out)
|
||||||
(return))
|
(return))
|
||||||
(else
|
(else
|
||||||
(command/self-insert ch buf out return)))))
|
(command/self-insert ch buf out return)))))
|
||||||
|
@ -679,7 +679,7 @@
|
||||||
(display "^C" out)
|
(display "^C" out)
|
||||||
(newline out)
|
(newline out)
|
||||||
(stty out '(icanon isig echo))
|
(stty out '(icanon isig echo))
|
||||||
(exit))
|
(return '^C))
|
||||||
|
|
||||||
(define (command/beep ch buf out return)
|
(define (command/beep ch buf out return)
|
||||||
(write-char (integer->char 7) out))
|
(write-char (integer->char 7) out))
|
||||||
|
@ -785,6 +785,7 @@
|
||||||
(single-line? (get-key args 'single-line?: #f))
|
(single-line? (get-key args 'single-line?: #f))
|
||||||
(fresh-line (get-key args 'fresh-line: #f))
|
(fresh-line (get-key args 'fresh-line: #f))
|
||||||
(no-stty? (get-key args 'no-stty?: #f))
|
(no-stty? (get-key args 'no-stty?: #f))
|
||||||
|
(hidden? (get-key args 'hidden?: #f))
|
||||||
(keymap0 (get-key args 'keymap:
|
(keymap0 (get-key args 'keymap:
|
||||||
(if (get-key args 'catch-control-c?: #f)
|
(if (get-key args 'catch-control-c?: #f)
|
||||||
standard-cancel-keymap
|
standard-cancel-keymap
|
||||||
|
@ -797,6 +798,7 @@
|
||||||
(let* ((width (or terminal-width (get-terminal-width out) 80))
|
(let* ((width (or terminal-width (get-terminal-width out) 80))
|
||||||
(prompt (if (procedure? prompter) (prompter) prompter))
|
(prompt (if (procedure? prompter) (prompter) prompter))
|
||||||
(done? #f)
|
(done? #f)
|
||||||
|
(tmp-out (if hidden? (open-output-string) out))
|
||||||
(return (lambda o (set! done? (if (pair? o) (car o) #t)))))
|
(return (lambda o (set! done? (if (pair? o) (car o) #t)))))
|
||||||
;; Maybe start at a fresh line.
|
;; Maybe start at a fresh line.
|
||||||
(cond
|
(cond
|
||||||
|
@ -815,7 +817,7 @@
|
||||||
(buffer-single-line?-set! buf single-line?)
|
(buffer-single-line?-set! buf single-line?)
|
||||||
(if single-line? (buffer-start-set! buf (buffer-min buf)))
|
(if single-line? (buffer-start-set! buf (buffer-min buf)))
|
||||||
(buffer-refresh buf out)
|
(buffer-refresh buf out)
|
||||||
(flush-output out)
|
(flush-output-port out)
|
||||||
((if no-stty? (lambda (out f) (f)) with-raw-io)
|
((if no-stty? (lambda (out f) (f)) with-raw-io)
|
||||||
out
|
out
|
||||||
(lambda ()
|
(lambda ()
|
||||||
|
@ -833,9 +835,9 @@
|
||||||
(buffer-clear buf out)
|
(buffer-clear buf out)
|
||||||
(print-exception exn out)
|
(print-exception exn out)
|
||||||
(buffer-draw buf out)))
|
(buffer-draw buf out)))
|
||||||
(x ch buf out return))
|
(x ch buf tmp-out return))
|
||||||
(flush-output out)
|
(flush-output-port tmp-out)
|
||||||
(buffer-refresh buf out)
|
(buffer-refresh buf tmp-out)
|
||||||
(if done?
|
(if done?
|
||||||
(and (not (eq? done? 'eof)) (buffer->string buf))
|
(and (not (eq? done? 'eof)) (buffer->string buf))
|
||||||
(lp keymap)))
|
(lp keymap)))
|
||||||
|
|
|
@ -7,5 +7,32 @@
|
||||||
buffer-clear buffer-refresh buffer-draw
|
buffer-clear buffer-refresh buffer-draw
|
||||||
buffer-row buffer-col
|
buffer-row buffer-col
|
||||||
make-keymap make-standard-keymap)
|
make-keymap make-standard-keymap)
|
||||||
(import (chibi) (chibi ast) (chibi stty) (chibi process) (srfi 9) (srfi 33))
|
(import (scheme base) (scheme char) (scheme write))
|
||||||
|
(cond-expand
|
||||||
|
((library (srfi 33))
|
||||||
|
(import (srfi 33)))
|
||||||
|
(else
|
||||||
|
(import (srfi 60))))
|
||||||
|
(cond-expand
|
||||||
|
(chibi
|
||||||
|
(import (chibi stty)))
|
||||||
|
(chicken
|
||||||
|
(import stty))
|
||||||
|
(else
|
||||||
|
(define (with-stty spec thunk)
|
||||||
|
(thunk))))
|
||||||
|
(cond-expand
|
||||||
|
(chibi
|
||||||
|
(import (only (chibi) protect print-exception)
|
||||||
|
(chibi ast)))
|
||||||
|
(else
|
||||||
|
(begin
|
||||||
|
(define-syntax protect
|
||||||
|
(syntax-rules () (protect . x) (guard . x)))
|
||||||
|
(define (print-exception exn . o)
|
||||||
|
(let ((out (if (pair? o) (car o) (current-error-port))))
|
||||||
|
(write exn out)
|
||||||
|
(newline out)))
|
||||||
|
(define (exception? x) #f)
|
||||||
|
(define (exception-kind x) #f))))
|
||||||
(include "edit-line.scm"))
|
(include "edit-line.scm"))
|
||||||
|
|
Loading…
Add table
Reference in a new issue