@@ -60,15 +60,19 @@ module Draw (
60
60
61
61
62
62
63
- import Control.Monad
64
- import Data.Default.Class
65
- import Data.Foldable
66
- import Data.List
67
- import Graphics.Rendering.Cairo as C hiding (x , y )
68
- import Graphics.Rendering.Cairo.Matrix (Matrix (.. ))
69
- import System.Directory
70
- import System.FilePath
71
-
63
+ import Control.Monad
64
+ import qualified Data.ByteString.Lazy as BSL
65
+ import Data.Default.Class
66
+ import Data.Foldable
67
+ import Data.Int
68
+ import Data.List
69
+ import Graphics.Rendering.Cairo as C hiding (x , y )
70
+ import Graphics.Rendering.Cairo.Matrix (Matrix (.. ))
71
+ import System.Directory
72
+ import System.FilePath
73
+ import Text.Printf
74
+
75
+ import Data.Crc32
72
76
import Draw.Color
73
77
import Draw.Color.Schemes.Continuous
74
78
import Draw.Color.Schemes.Discrete
@@ -217,6 +221,10 @@ coordinateSystem cosy = do
217
221
218
222
-- | Render pictures for Haddock with doctests. Nomenclature: the 'FilePath' for
219
223
-- /Foo.Bar.Baz/ is /Foo\/Bar\/Baz\/pic_name.svg/.
224
+ --
225
+ -- Prints status information about the generated file so that doctests fail when
226
+ -- the file contents change. Inspect the new output and update the output if the
227
+ -- result is OK.
220
228
haddockRender :: FilePath -> Int -> Int -> Render () -> IO ()
221
229
haddockRender filename w h actions = do
222
230
let filepath = " docs/haddock/" ++ filename
@@ -244,7 +252,7 @@ haddockRender filename w h actions = do
244
252
haddockAxes (Vec2 5 5 ) 15
245
253
246
254
normalizeSvgFile filepath
247
- putStrLn filepath
255
+ haddockPrintInfo filepath
248
256
249
257
haddockGrid :: Int -> Int -> Render ()
250
258
haddockGrid w h = grouped (paintWithAlpha 0.1 ) $ do
@@ -296,6 +304,20 @@ haddockAxes start len = grouped (paintWithAlpha 0.5) $ do
296
304
| otherwise = mirrorYCoords
297
305
in G. transform (G. translate (start +. Vec2 0 (len+ 5 )) <> G. scale 2 <> directionFlip) y'
298
306
307
+ haddockPrintInfo :: FilePath -> IO ()
308
+ haddockPrintInfo filepath = do
309
+ contents <- BSL. readFile filepath
310
+ printf " Generated file: size %s, crc32: %s" (humanFilesize (BSL. length contents)) (show (crc32 contents))
311
+
312
+ humanFilesize :: Int64 -> String
313
+ humanFilesize = go suffixes
314
+ where
315
+ go [] size = printf " Oh wow this file is %d byte large I ran out of suffixes" size
316
+ go (suffix: rest) size
317
+ | size < 1000 = show size ++ suffix
318
+ | otherwise = go rest (size `div` 1000 )
319
+ suffixes = [" B" , " KB" , " MB" , " GB" , " TB" , " PB" ] -- That should suffice.
320
+
299
321
-- | 'Vec2'-friendly version of Cairo’s 'moveTo'.
300
322
moveToVec :: Vec2 -> Render ()
301
323
moveToVec (Vec2 x y) = moveTo x y
0 commit comments