colorized.scm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. ;;; guile-openai --- An OpenAI API client for Guile
  2. ;;; Copyright © 2023 Andrew Whatson <whatson@tailcall.au>
  3. ;;;
  4. ;;; This file is part of guile-openai.
  5. ;;;
  6. ;;; guile-openai is free software: you can redistribute it and/or modify
  7. ;;; it under the terms of the GNU Affero General Public License as
  8. ;;; published by the Free Software Foundation, either version 3 of the
  9. ;;; License, or (at your option) any later version.
  10. ;;;
  11. ;;; guile-openai is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;; Affero General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU Affero General Public
  17. ;;; License along with guile-openai. If not, see
  18. ;;; <https://www.gnu.org/licenses/>.
  19. (define-module (openai utils colorized)
  20. #:use-module (ice-9 colorized)
  21. #:export (color-stream)
  22. #:re-export (add-color-scheme!))
  23. (define color-stream
  24. ;;; XXX: The guile-colorized library renders objects into a string
  25. ;;; before colorization, which breaks objects with streaming display.
  26. ;;; This custom color hack behaves correctly in the repl, but doesn't
  27. ;;; respect the port argument to colorize. Needs fixing upstream.
  28. (let ((color-scheme-obj (@@ (ice-9 colorized) color-scheme-obj))
  29. (color-scheme-color (@@ (ice-9 colorized) color-scheme-color))
  30. (color-scheme-control (@@ (ice-9 colorized) color-scheme-control)))
  31. (lambda (cs)
  32. (let* ((dummy ((color-func)
  33. (color-scheme-color cs) "x"
  34. (color-scheme-control cs)))
  35. (parts (string-split dummy #\x)))
  36. (display (car parts))
  37. (display (color-scheme-obj cs))
  38. (display (cadr parts))
  39. ""))))