diff --git a/main.rkt b/main.rkt index 7027e0d..11ac98c 100644 --- a/main.rkt +++ b/main.rkt @@ -5,6 +5,7 @@ "private/config.rkt" "private/diff.rkt" "private/info.rkt" + "private/utils.rkt" simple-log racket/string net/sendurl @@ -62,9 +63,7 @@ ; result : The entered line or an EOF object. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (git-prompt p) - (display p) - (flush-output) - (read-line)) + (input-prompt p)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; goal : Add --porcelain to a Git argument list when it is absent. diff --git a/private/diff.rkt b/private/diff.rkt index fe93843..9f726fb 100644 --- a/private/diff.rkt +++ b/private/diff.rkt @@ -6,10 +6,13 @@ xml racket/string "config.rkt" + "utils.rkt" ) (provide diff->html - show->html) + show->html + config-diff2html + ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Helper functions @@ -57,6 +60,39 @@ ;; Exported functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(define (config-diff2html) + + (define (checker f) + (λ (inp) + (let ((url (string-trim inp))) + (if (string=? url "") + (f) + (if (valid-http-or-file-url? url) + url + (begin + (displayln "! Not a valid url, please input a valid url") + #f))))) + ) + + (define (inp name f) + (input-prompt (string-join + (list + (format "Give the url for the ~a" name) + "Enter keeps the current value:" + (string-append " - " (f)) + ">") + "\n") + #:loop-until (checker f))) + + (let ((h-css (inp "Highlighting CSS" highlight-css)) + (d-css (inp "Diff2Html CSS" diff2html-min-css)) + (d-js (inp "Diff2Html UI Javascript" diff2html-ui-min-js)) + ) + (cfg-set! 'diff 'highlight-css h-css) + (cfg-set! 'diff 'diff2html-min-css d-css) + (cfg-set! 'diff 'diff2html-ui-min-js d-js)) + ) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; goal : Render a Git diff in a temporary HTML file. ; pre : diff is a unified Git diff string. diff --git a/private/utils.rkt b/private/utils.rkt index d3b5556..c85e595 100644 --- a/private/utils.rkt +++ b/private/utils.rkt @@ -1,4 +1,32 @@ #lang racket/base -(provide config) +(require net/url) +(provide input-prompt + valid-http-or-file-url? + ) + +(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) ""))))))) \ No newline at end of file