#lang scheme/base
(require srfi/13
"base.ss"
"convert.ss")
(define (string+false? item)
(or (string? item) (not item)))
(define (ensure-string str)
(if (bytes? str)
(bytes->string/utf-8 str)
str))
(define string-length/c
(case-lambda
[(num)
(flat-named-contract
(format "(string-length/c ~a)" num)
(lambda (item)
(and (string? item)
(<= (string-length item) num))))]
[(min max)
(flat-named-contract
(format "(string-length/c ~a ~a)" min max)
(lambda (item)
(and (string? item)
(>= (string-length item) min)
(<= (string-length item) max))))]))
(define (string-delimit items delimiter #:prefix [prefix #f] #:suffix [suffix #f])
(define delimited (string-join items delimiter))
(if prefix
(if suffix
(string-append prefix delimited suffix)
(string-append prefix delimited))
(if suffix
(string-append delimited suffix)
delimited)))
(define MAX-TAB-NAME-CHARS 20)
(define (string-ellipsify str [max-length 20] [ellipsis "..."])
(define str-length
(string-length str))
(define ellipsis-length
(string-length ellipsis))
(cond [(<= str-length ellipsis-length) str]
[(<= max-length ellipsis-length) str]
[(> str-length max-length)
(let ([trim-length (- max-length ellipsis-length)])
(string-append (string-trim-right (string-take str trim-length)) ellipsis))]
[else str]))
(provide symbol+false->string+false
string+false->symbol+false
string+false->number+false
number+false->string+false)
(provide/contract
[string+false? procedure?]
[ensure-string procedure?]
[string-length/c (-> natural-number/c flat-contract?)]
[string-delimit (->* ((listof string?) string?) (#:prefix (or/c string? false/c) #:suffix (or/c string? false/c)) string?)]
[string-ellipsify (->* (string?) (integer? string?) string?)])