Skip to content

Instantly share code, notes, and snippets.

@b4284
Last active July 20, 2017 15:38
Show Gist options
  • Select an option

  • Save b4284/fc75ad396e670161592eba4dc13601bb to your computer and use it in GitHub Desktop.

Select an option

Save b4284/fc75ad396e670161592eba4dc13601bb to your computer and use it in GitHub Desktop.
Mimic GNU yes' behavior of cmdline arguments.
(use-modules (ice-9 binary-ports)
(rnrs bytevectors))
(define (build-string s n)
(let* ((s2 (string-append s "\n"))
(s2len (string-length s2)))
(let A ((s3 (string-copy s2))
(s3len s2len))
(if (> (+ s3len s2len) n)
s3
(A (string-append s3 s2) (+ s3len s2len))))))
(define (yes args)
(let ((args-str
(let ((t (cdr args)))
(if (null? t) "y" (string-join t " ")))))
(let ((stdout (current-output-port))
(yyy (string->utf8 (build-string args-str 8192))))
(setvbuf stdout 'block 8192)
(let A ()
(put-bytevector stdout yyy)
(A)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment