don't use nested show in pretty (issue #518); use string-width for computing width (issue #517)

This commit is contained in:
Alex Shinn 2020-06-24 12:25:36 +09:00
parent cd7480ce45
commit 532fb83e0a
4 changed files with 70 additions and 50 deletions

View file

@ -21,7 +21,7 @@
radix precision decimal-sep decimal-align sign-rule radix precision decimal-sep decimal-align sign-rule
comma-sep comma-rule word-separator? ambiguous-is-wide? comma-sep comma-rule word-separator? ambiguous-is-wide?
;; pretty ;; pretty
pretty pretty-shared pretty-simply pretty-color pretty pretty-shared pretty-simply pretty-with-color
;; columnar ;; columnar
columnar tabular wrapped wrapped/list wrapped/char columnar tabular wrapped wrapped/list wrapped/char
justified from-file line-numbers show-columns justified from-file line-numbers show-columns

View file

@ -53,7 +53,7 @@
(call-with-output-string (lambda (out) (write x out)))) (call-with-output-string (lambda (out) (write x out))))
(define (try-fitted2 proc fail) (define (try-fitted2 proc fail)
(fn (width (orig-output output)) (fn (width string-width (orig-output output))
(let ((out (open-output-string))) (let ((out (open-output-string)))
(call-with-current-continuation (call-with-current-continuation
(lambda (abort) (lambda (abort)
@ -64,7 +64,7 @@
(fn (col) (fn (col)
(let lp ((i 0) (col col)) (let lp ((i 0) (col col))
(let ((nli (string-find/index str #\newline i)) (let ((nli (string-find/index str #\newline i))
(len (string-length str))) (len (string-width str)))
(if (< nli len) (if (< nli len)
(if (> (+ (- nli i) col) width) (if (> (+ (- nli i) col) width)
(abort fail) (abort fail)
@ -88,33 +88,40 @@
proc proc
(try-fitted2 proc (lp (car ls) (cdr ls)))))) (try-fitted2 proc (lp (car ls) (cdr ls))))))
(define (fits-in-width width proc) (define (fits-in-width width proc set-failed!)
(call-with-current-continuation (call-with-current-continuation
(lambda (abort) (lambda (abort)
(show
#f
(fn ((orig-output output)) (fn ((orig-output output))
(define (output* str) (define (output* str)
(each (orig-output str) (each (orig-output str)
(fn (col) (fn (col)
(if (>= col width) (if (>= col width)
(abort #f) (begin (set-failed! #t) (abort #f))
nothing)))) nothing))))
(with ((output output*)) (with ((output output*))
proc)))))) proc)))))
(define (fits-in-columns width ls writer) (define (fits-in-columns width ls writer set-result!)
(let ((max-w (quotient width 2))) (let ((max-w (quotient width 2)))
(fn (string-width)
(let lp ((ls ls) (res '()) (widest 0)) (let lp ((ls ls) (res '()) (widest 0))
(cond (cond
((pair? ls) ((pair? ls)
(let ((str (fits-in-width max-w (writer (car ls))))) (let ((failed? #f))
(and str (call-with-output
(fits-in-width max-w
(writer (car ls))
(lambda (x) (set! failed? x)))
(lambda (str)
(if failed?
(begin
(set-result! #f)
nothing)
(lp (cdr ls) (lp (cdr ls)
(cons str res) (cons str res)
(max (string-length str) widest))))) (max (string-width str) widest)))))))
((null? ls) (cons widest (reverse res))) ((null? ls) (set-result! (cons widest (reverse res))) nothing)
(else #f))))) (else (set-result! #f) nothing))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; style ;; style
@ -258,14 +265,22 @@
(fn (col width string-width) (fn (col width string-width)
(let ((avail (- width col))) (let ((avail (- width col)))
(cond (cond
((and (pair? (cdr ls)) (pair? (cddr ls)) (pair? (cdr (cddr ls))) ((and (pair? (cdr ls)) (pair? (cddr ls)) (pair? (cdr (cddr ls))))
(fits-in-columns width ls (lambda (x) (pp-flat x pp shares)))) (let ((out (open-output-string))
=> (lambda (ls) (result #f))
(call-with-output
(fits-in-columns width ls (lambda (x) (pp-flat x pp shares))
(lambda (res) (set! result res)))
(lambda (str)
(fn ()
(if (not result)
;; no room, print one per line
(joined/shares pp ls shares (make-nl-space col))
;; at least four elements which can be broken into columns ;; at least four elements which can be broken into columns
(let* ((prefix (make-nl-space col)) (let* ((prefix (make-nl-space col))
(widest (+ 1 (car ls))) (widest (+ 1 (car result)))
(columns (quotient width widest))) ; always >= 2 (columns (quotient width widest))) ; always >= 2
(let lp ((ls (cdr ls)) (i 1)) (let lp ((ls (cdr result)) (i 1))
(cond (cond
((null? ls) ((null? ls)
nothing) nothing)
@ -279,7 +294,7 @@
(let ((pad (- widest (string-width (car ls))))) (let ((pad (- widest (string-width (car ls)))))
(each (car ls) (each (car ls)
(make-space pad) (make-space pad)
(lp (cdr ls) (+ i 1)))))))))) (lp (cdr ls) (+ i 1))))))))))))))
(else (else
;; no room, print one per line ;; no room, print one per line
(joined/shares pp ls shares (make-nl-space col)))))) (joined/shares pp ls shares (make-nl-space col))))))
@ -380,4 +395,5 @@
(each (pp obj (extract-shared-objects #f #f)) (each (pp obj (extract-shared-objects #f #f))
fl))) fl)))
(define pretty-color pretty) ;; TODO: add colors
(define pretty-with-color pretty)

View file

@ -8,5 +8,5 @@
(srfi 69) (srfi 69)
(srfi 130) (srfi 130)
(srfi 166 base)) (srfi 166 base))
(export pretty pretty-shared pretty-simply pretty-color) (export pretty pretty-shared pretty-simply pretty-with-color)
(include "pretty.scm")) (include "pretty.scm"))

View file

@ -495,6 +495,10 @@
"(#(0 1) #(2 3) #(4 5) #(6 7) #(8 9) #(10 11) #(12 13) #(14 15) "(#(0 1) #(2 3) #(4 5) #(6 7) #(8 9) #(10 11) #(12 13) #(14 15)
#(16 17) #(18 19))\n") #(16 17) #(18 19))\n")
(test-pretty
"#(#(0 1) #(2 3) #(4 5) #(6 7) #(8 9) #(10 11) #(12 13) #(14 15)
#(16 17) #(18 19))\n")
(test-pretty (test-pretty
"(define (fold kons knil ls) "(define (fold kons knil ls)
(define (loop ls acc) (define (loop ls acc)