Skip to content

Commit 42b84c7

Browse files
committed
Make haddockRender report file size and CRC32 (instead of filename)
1 parent 68bf4a5 commit 42b84c7

File tree

3 files changed

+94
-10
lines changed

3 files changed

+94
-10
lines changed

package.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies:
1818
- base >= 4.7 && < 5
1919

2020
- alfred-margaret
21+
- bytestring
2122
- cairo
2223
- colour
2324
- containers

src/Data/Crc32.hs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module Data.Crc32 (
2+
crc32
3+
, Crc32(..)
4+
) where
5+
6+
7+
8+
import Data.Bits
9+
import Data.ByteString.Lazy (ByteString)
10+
import qualified Data.ByteString.Lazy as BSL
11+
import Data.Vector (Vector, (!))
12+
import qualified Data.Vector as V
13+
import Data.Word
14+
import Text.Printf
15+
16+
17+
18+
table :: Vector Word32
19+
table = createCrc32Table 0xedb88320
20+
21+
createCrc32Table :: Word32 -> Vector Word32
22+
createCrc32Table poly = V.generate 256 $ \n -> V.foldl'
23+
(\acc _ -> if odd acc
24+
then poly `xor` shiftR acc 1
25+
else shiftR acc 1
26+
)
27+
(fromIntegral n)
28+
(V.enumFromN 0 8)
29+
30+
-- fn crc32_compute_table() -> [u32; 256] {
31+
-- let mut crc32_table = [0; 256];
32+
--
33+
-- for n in 0..256 {
34+
-- crc32_table[n as usize] = (0..8).fold(n as u32, |acc, _| {
35+
-- match acc & 1 {
36+
-- 1 => 0xedb88320 ^ (acc >> 1),
37+
-- _ => acc >> 1,
38+
-- }
39+
-- });
40+
-- }
41+
--
42+
-- crc32_table
43+
-- }
44+
45+
newtype Crc32 = Crc32 Word32
46+
deriving (Eq, Ord)
47+
48+
instance Show Crc32 where show (Crc32 crc) = printf "%#08x" crc
49+
50+
-- | CRC32 checksum.
51+
--
52+
-- >>> import Data.ByteString.Lazy.Char8
53+
-- >>> crc32 (pack "The quick brown fox jumps over the lazy dog")
54+
-- 0x414fa339
55+
--
56+
-- >>> crc32 (pack "123456789")
57+
-- 0xcbf43926
58+
crc32 :: ByteString -> Crc32
59+
crc32 bs =
60+
let Crc32 crcResult = BSL.foldl' (\(Crc32 crc) byte -> Crc32 (xor (crc `shiftR` 8) (table ! fromIntegral (fromIntegral (crc .&. 0xff) `xor` byte)))) (Crc32 0xffffffff) bs
61+
in Crc32 (complement crcResult)

src/Draw.hs

+32-10
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,19 @@ module Draw (
6060

6161

6262

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
7276
import Draw.Color
7377
import Draw.Color.Schemes.Continuous
7478
import Draw.Color.Schemes.Discrete
@@ -217,6 +221,10 @@ coordinateSystem cosy = do
217221

218222
-- | Render pictures for Haddock with doctests. Nomenclature: the 'FilePath' for
219223
-- /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.
220228
haddockRender :: FilePath -> Int -> Int -> Render () -> IO ()
221229
haddockRender filename w h actions = do
222230
let filepath = "docs/haddock/" ++ filename
@@ -244,7 +252,7 @@ haddockRender filename w h actions = do
244252
haddockAxes (Vec2 5 5) 15
245253

246254
normalizeSvgFile filepath
247-
putStrLn filepath
255+
haddockPrintInfo filepath
248256

249257
haddockGrid :: Int -> Int -> Render ()
250258
haddockGrid w h = grouped (paintWithAlpha 0.1) $ do
@@ -296,6 +304,20 @@ haddockAxes start len = grouped (paintWithAlpha 0.5) $ do
296304
| otherwise = mirrorYCoords
297305
in G.transform (G.translate (start +. Vec2 0 (len+5)) <> G.scale 2 <> directionFlip) y'
298306

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+
299321
-- | 'Vec2'-friendly version of Cairo’s 'moveTo'.
300322
moveToVec :: Vec2 -> Render ()
301323
moveToVec (Vec2 x y) = moveTo x y

0 commit comments

Comments
 (0)