Convert print-source-listings to use keywords.

This commit is contained in:
Dominik Pantůček 2023-03-28 18:51:13 +02:00
parent 7a0db40062
commit ffc5442c17
3 changed files with 57 additions and 46 deletions

View file

@ -97,7 +97,7 @@ testing.so: testing.o
testing.o: testing.import.scm testing.o: testing.import.scm
testing.import.scm: $(TESTING-SOURCES) testing.import.scm: $(TESTING-SOURCES)
LISTING-SOURCES=listing.scm testing.import.scm LISTING-SOURCES=listing.scm testing.import.scm ansi.import.scm
listing.so: listing.o listing.so: listing.o
listing.o: listing.import.scm listing.o: listing.import.scm

View file

@ -36,7 +36,9 @@
(chicken base) (chicken base)
(chicken string) (chicken string)
(chicken format) (chicken format)
testing) (chicken keyword)
testing
ansi)
;; Returns the number of digits required to represent a given number ;; Returns the number of digits required to represent a given number
;; in decimal format. ;; in decimal format.
@ -108,46 +110,58 @@
;; Prints and highlights a selection of source listing lines and ;; Prints and highlights a selection of source listing lines and
;; their optional context. ;; their optional context.
(define (print-source-listing lines highlights context hl-pre hl-post ctx-pre ctx-post ellipsis) (define (print-source-listing lines highlights . args)
(let ((digits (number-digits (length lines)))) (let ((highlight-rules (get-keyword #:highlight-rules args
(let loop ((lines lines) (lambda ()
(number 1) '((error (ansi #:bold #:red) (ansi #:default))
(printed-something #f) (warning (ansi #:yellow) (ansi #:default))
(was-printing #f)) (info (ansi #:cyan) (ansi #:default))
(when (not (null? lines)) ))))
(let* ((content? (in-highlights? number highlights)) (ellipsis (get-keyword #:ellipsis args (lambda () "...")))
(context? (and (not content?) (ctx-pre (get-keyword #:context-pre args (lambda () "")))
(line-near-targets? number highlights context))) (ctx-post (get-keyword #:context-post args (lambda () "")))
(print? (or content? context?))) (hl-pre (get-keyword #:highlight-pre args (lambda () a:error)))
(cond (print? (hl-post (get-keyword #:highlight-post args (lambda () a:default)))
(when (and printed-something (context (get-keyword #:context args (lambda () 3))))
(not was-printing)) (let ((digits (number-digits (length lines))))
(print ellipsis)) (let loop ((lines lines)
(if content? (number 1)
(display hl-pre) (printed-something #f)
(when context? (was-printing #f))
(display ctx-pre))) (when (not (null? lines))
(display (sprintf "~A~A~A" (let* ((content? (in-highlights? number highlights))
(format-line-number number digits) (context? (and (not content?)
(car lines) (line-near-targets? number highlights context)))
(let ((comment (highlight-comment number highlights))) (print? (or content? context?)))
(if comment (cond (print?
(sprintf " # <<< ~A" comment) (when (and printed-something
"")))) (not was-printing))
(if content? (print ellipsis))
(display hl-post) (if content?
(when context? (display hl-pre)
(display ctx-post))) (when context?
(newline) (display ctx-pre)))
(loop (cdr lines) (display (sprintf "~A~A~A"
(+ number 1) (format-line-number number digits)
#t (car lines)
#t)) (let ((comment (highlight-comment number highlights)))
(else (if comment
(loop (cdr lines) (sprintf " # <<< ~A" comment)
(+ number 1) ""))))
printed-something (if content?
#f)))))))) (display hl-post)
(when context?
(display ctx-post)))
(newline)
(loop (cdr lines)
(+ number 1)
#t
#t))
(else
(loop (cdr lines)
(+ number 1)
printed-something
#f)))))))))
;; Performs self-tests of the listing module. ;; Performs self-tests of the listing module.
(define (listing-tests!) (define (listing-tests!)

View file

@ -139,10 +139,7 @@
(print-source-listing (print-source-listing
lines lines
hls hls
-1 #:context -1
a:error a:default
"" "" ; Not used
"..." ; Not used
))) )))
) )