Commit 2415aefd authored by Andrew Gibiansky's avatar Andrew Gibiansky

closes #142

parent 268bf239
...@@ -30,9 +30,7 @@ instance IHaskellDisplay (Renderable a) where ...@@ -30,9 +30,7 @@ instance IHaskellDisplay (Renderable a) where
chartData :: Renderable a -> FileFormat -> IO DisplayData chartData :: Renderable a -> FileFormat -> IO DisplayData
chartData renderable format = do chartData renderable format = do
-- Switch to a temporary directory so that any files we create aren't switchToTmpDir
-- visible. On Unix, this is usually /tmp.
try (getTemporaryDirectory >>= setCurrentDirectory) :: IO (Either SomeException ())
-- Write the PNG image. -- Write the PNG image.
let filename = ".ihaskell-chart.png" let filename = ".ihaskell-chart.png"
......
...@@ -20,9 +20,7 @@ instance IHaskellDisplay (Diagram Cairo R2) where ...@@ -20,9 +20,7 @@ instance IHaskellDisplay (Diagram Cairo R2) where
diagramData :: Diagram Cairo R2 -> OutputType -> IO DisplayData diagramData :: Diagram Cairo R2 -> OutputType -> IO DisplayData
diagramData renderable format = do diagramData renderable format = do
-- Switch to a temporary directory so that any files we create aren't switchToTmpDir
-- visible. On Unix, this is usually /tmp.
try (getTemporaryDirectory >>= setCurrentDirectory) :: IO (Either SomeException ())
-- Compute width and height. -- Compute width and height.
let w = width renderable let w = width renderable
......
...@@ -29,9 +29,8 @@ instance IHaskellDisplay (Image PixelCMYK16) where display = displayImageAsJpg . ...@@ -29,9 +29,8 @@ instance IHaskellDisplay (Image PixelCMYK16) where display = displayImageAsJpg .
-- main rendering function -- main rendering function
displayImageAsJpg :: DynamicImage -> IO Display displayImageAsJpg :: DynamicImage -> IO Display
displayImageAsJpg renderable = do displayImageAsJpg renderable = do
-- Switch to a temporary directory so that any files we create aren't switchToTmpDir
-- visible. On Unix, this is usually /tmp.
try (getTemporaryDirectory >>= setCurrentDirectory) :: IO (Either SomeException ())
let filename = ".ihaskell.juicypixels.jpg" let filename = ".ihaskell.juicypixels.jpg"
-- Write the image -- Write the image
saveJpgImage 95 filename renderable saveJpgImage 95 filename renderable
......
...@@ -91,7 +91,8 @@ library ...@@ -91,7 +91,8 @@ library
stm -any, stm -any,
text -any, text -any,
utf8-string -any, utf8-string -any,
vector -any vector -any,
temporary ==1.2.*
exposed-modules: IHaskell.Display exposed-modules: IHaskell.Display
IHaskell.Convert IHaskell.Convert
......
...@@ -9,6 +9,7 @@ module IHaskell.Display ( ...@@ -9,6 +9,7 @@ module IHaskell.Display (
Display(..), Display(..),
DisplayData(..), DisplayData(..),
printDisplay, printDisplay,
switchToTmpDir,
-- Internal only use -- Internal only use
displayFromChan, displayFromChan,
...@@ -23,6 +24,10 @@ import qualified Data.ByteString.Base64 as Base64 ...@@ -23,6 +24,10 @@ import qualified Data.ByteString.Base64 as Base64
import qualified Data.ByteString.Char8 as Char import qualified Data.ByteString.Char8 as Char
import Data.Aeson (Value) import Data.Aeson (Value)
import System.Directory(getTemporaryDirectory, setCurrentDirectory)
import System.IO.Temp(createTempDirectory)
import Control.Concurrent.STM.TChan import Control.Concurrent.STM.TChan
import System.IO.Unsafe (unsafePerformIO) import System.IO.Unsafe (unsafePerformIO)
...@@ -122,3 +127,14 @@ unfoldM f = maybe (return []) (\r -> (r:) <$> unfoldM f) =<< f ...@@ -122,3 +127,14 @@ unfoldM f = maybe (return []) (\r -> (r:) <$> unfoldM f) =<< f
-- notebook once the current execution call ends. -- notebook once the current execution call ends.
printDisplay :: IHaskellDisplay a => a -> IO () printDisplay :: IHaskellDisplay a => a -> IO ()
printDisplay disp = display disp >>= atomically . writeTChan displayChan printDisplay disp = display disp >>= atomically . writeTChan displayChan
-- | Convenience function for client libraries. Switch to a temporary
-- directory so that any files we create aren't visible. On Unix, this is
-- usually /tmp.
switchToTmpDir = void (try switchDir :: IO (Either SomeException ()))
where
switchDir =
getTemporaryDirectory >>=
flip createTempDirectory "ihaskell-display-tmp" >>=
setCurrentDirectory
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment