@@ -1,6 +1,7 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2module Graphics.Vty.UnicodeWidthTable.IO
2module Graphics.Vty.UnicodeWidthTable.IO
3 ( readUnicodeWidthTable
3 ( readUnicodeWidthTable
4+ , parseUnicodeWidthTable
4 , writeUnicodeWidthTable
5 , writeUnicodeWidthTable
@@ -21,20 +22,23 @@ import Graphics.Vty.UnicodeWidthTable.Types
21-- This either returns a successfully parsed table or a table parsing
22-- This either returns a successfully parsed table or a table parsing
22-- error message. This does not handle I/O exceptions.
23-- error message. This does not handle I/O exceptions.
23readUnicodeWidthTable :: FilePath -> IO (Either String UnicodeWidthTable)
24readUnicodeWidthTable :: FilePath -> IO (Either String UnicodeWidthTable)
24−readUnicodeWidthTable path = do
25+readUnicodeWidthTable path = parseUnicodeWidthTable <$> BSL.readFile path
25− body <- BSL.readFile path
26+
27+-- | Parse a binary unicode width table.
28+parseUnicodeWidthTable :: BSL.ByteString -> Either String UnicodeWidthTable
29+parseUnicodeWidthTable bs =
26− case runGetOrFail tableParser body of
30+ case runGetOrFail tableParser bs of
27 Left (_, _, msg) ->
31 Left (_, _, msg) ->
28− return $ Left msg
32+ Left msg
30 -- Even if we parsed a table, leftover bytes indicate something
34 -- Even if we parsed a table, leftover bytes indicate something
31 -- could be wrong.
35 -- could be wrong.
32 Right (remainingBytes, _, _) | not (BSL.null remainingBytes) ->
36 Right (remainingBytes, _, _) | not (BSL.null remainingBytes) ->
33− return $ Left $ "Error: " <> show (BSL.length remainingBytes) <>
37+ Left $ "Error: " <> show (BSL.length remainingBytes) <>
34− " byte(s) left unconsumed"
38+ " byte(s) left unconsumed"
36 Right (_, _, table) ->
40 Right (_, _, table) ->
37− return $ Right table
41+ Right table
39-- | Write the unicode width table to the specified path.
43-- | Write the unicode width table to the specified path.