@@ -1,12 +1,14 @@
11{-# LANGUAGE CPP #-}
22{-# LANGUAGE ForeignFunctionInterface #-}
33module Graphics.Vty.UnicodeWidthTable.Install
4− ( installUnicodeWidthTable
4+ ( TableInstallException(..)
5+ , installUnicodeWidthTable
56 , isCustomTableReady
67 )
78where
89
910import Control.Monad (when, forM_)
11+import qualified Control.Exception as E
1012import Data.Word (Word8, Word32)
1113#if !(MIN_VERSION_base(4,11,0))
1214import Data.Semigroup ((<>))
@@ -28,6 +30,20 @@ isCustomTableReady = (== 1) <$> c_isCustomTableReady
2830tableSize :: Int
2931tableSize = 0x110000
3032
33+-- | Exception type raised by 'installUnicodeWidthTable'.
34+data TableInstallException =
35+ TableInitFailure Int Int
36+ -- ^ The width table could not be initialized. Args: failure status,
37+ -- requested table size.
38+ | TableRangeFailure Int WidthTableRange
39+ -- ^ A code point range could not be configured. Args: failure
40+ -- status, offending range.
41+ | TableActivationFailure Int
42+ -- ^ The table could not be activated. Args: failure status.
43+ deriving (Eq, Show)
44+
45+instance E.Exception TableInstallException
46+
3147-- | Install a custom unicode character width
3248-- table. Such tables are obtained with
3349-- 'Graphics.Vty.UnicodeWidthTable.Query.buildUnicodeWidthTable' and
@@ -61,15 +77,14 @@ tableSize = 0x110000
6177--
6278-- If this function fails for any reason -- if the table cannot be
6379-- installed or is invalid, or if a custom table already exists -- this
64−-- will raise an exception by calling 'error'.
80+-- will raise a 'TableInstallException' exception.
6581--
6682-- This function is not thread-safe.
6783installUnicodeWidthTable :: UnicodeWidthTable -> IO ()
6884installUnicodeWidthTable table = do
6985 initResult <- initCustomTable tableSize
7086 when (initResult /= 0) $
71− error $ "installUnicodeWidthTable: error initializing " <>
72− "custom table, status " <> show initResult
87+ E.throw $ TableInitFailure initResult tableSize
7388
7489 forM_ (unicodeWidthTableRanges table) $ \r -> do
7590 result <- setCustomTableRange (rangeStart r)
@@ -78,13 +93,11 @@ installUnicodeWidthTable table = do
7893
7994 when (result /= 0) $ do
8095 deallocateCustomTable
81− error $ "installUnicodeWidthTable: error installing range " <>
82− show r <> ", status " <> show result
96+ E.throw $ TableRangeFailure result r
8397
8498 actResult <- activateCustomTable
8599 when (actResult /= 0) $
86− error $ "installUnicodeWidthTable: error activating custom " <>
87− "table, status " <> show actResult
100+ E.throw $ TableActivationFailure actResult
88101
89102------------------------------------------------------------------------
90103-- C imports