49 lines
1.3 KiB
Racket
49 lines
1.3 KiB
Racket
#lang racket/base
|
|
|
|
(require net/url)
|
|
|
|
(provide input-prompt
|
|
valid-http-or-file-url?
|
|
)
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
; goal : Ask the user for input .
|
|
; pre : p is a prompt.
|
|
; post : It should give back the supplied input as string.
|
|
; result : the return value of until.
|
|
; internals:
|
|
;
|
|
; input-prompt displays the given prompt and reads a line
|
|
; of text. After the user presses enter, this line is
|
|
; fed to the until callback. If the until callback returns
|
|
; #f, the prompt is displayed again. Otherwise, the value
|
|
; of until is returned.
|
|
;
|
|
; The programmer must make sure the until returns whatever
|
|
; format is appropriate. In general it will be a string.
|
|
;
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
(define (input-prompt p #:loop-until [until (λ (x) x)])
|
|
(let loop ()
|
|
(display p)
|
|
(flush-output)
|
|
(let ((inp (read-line)))
|
|
(let ((i (until inp)))
|
|
(if i
|
|
i
|
|
(loop)))
|
|
)
|
|
)
|
|
)
|
|
|
|
|
|
(define (valid-http-or-file-url? value)
|
|
(if (not (string? value))
|
|
#f
|
|
(with-handlers ([exn:fail? (λ (exn) #f)])
|
|
(let ((url (string->url value)))
|
|
(and
|
|
(member (url-scheme url) '("http" "https" "file"))
|
|
(string? (url-host url))
|
|
(not (string=? (url-host url) ""))))))) |