blob: 2cdcadbb74c71ba33ae1ae90ca9baa334e1521c4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
;;; images.rkt -- support for image handline
;; Copyright (C) 2012, 2014 Jose Antonio Ortega Ruiz
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the Modified BSD License. You should
;; have received a copy of the license along with this program. If
;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
;; Authors: Michael Wilber, Jose Antonio Ortega Ruiz <jao@gnu.org>
;; Start date: Sun Sep 2, 2012 18:54
#lang racket/base
(require racket/file file/convertible racket/pretty)
(provide image-cache
maybe-print-image
maybe-write-image
make-port-print-handler)
(define image-cache
(let ([ensure-dir (lambda (dir)
(if (path-string? dir)
(begin (make-directory* dir)
(if (path? dir) (path->string dir) dir))
(path->string (find-system-path 'temp-dir))))])
(make-parameter (ensure-dir #f) ensure-dir)))
(define (save-tmpimage imgbytes)
;; Save imgbytes to a new temporary file and return the filename
(define filename (make-temporary-file "geiser-img-~a.png" #f (image-cache)))
(with-output-to-file filename #:exists 'truncate
(lambda () (display imgbytes)))
(format "#<Image: ~a>" filename))
(define (maybe-save-image value)
(and (convertible? value)
;; (The above could be problematic if a future version of racket
;; suddenly decides it can "convert" strings to picts)
(save-tmpimage (convert value 'png-bytes))))
(define (maybe-print-image value)
(cond [(maybe-save-image value) => (lambda (s) (printf "~a\n" s))]
[else (unless (void? value)
(pretty-print value))]))
(define (maybe-write-image value)
(write (or (maybe-save-image value) value)))
(define (make-port-print-handler ph)
(lambda (value port . rest)
(apply ph (or (maybe-save-image value) value) port rest)))
|