@@ -1,12 +1,14 @@
1{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE ForeignFunctionInterface #-}
2{-# LANGUAGE ForeignFunctionInterface #-}
3module Graphics.Vty.UnicodeWidthTable.Install
3module Graphics.Vty.UnicodeWidthTable.Install
4+ ( TableInstallException(..)
4− ( installUnicodeWidthTable
5+ , installUnicodeWidthTable
5 , isCustomTableReady
6 , isCustomTableReady
9import Control.Monad (when, forM_)
10import Control.Monad (when, forM_)
11+import qualified Control.Exception as E
10import Data.Word (Word8, Word32)
12import Data.Word (Word8, Word32)
11#if !(MIN_VERSION_base(4,11,0))
13#if !(MIN_VERSION_base(4,11,0))
12import Data.Semigroup ((<>))
14import Data.Semigroup ((<>))
@@ -28,6 +30,20 @@ isCustomTableReady = (== 1) <$> c_isCustomTableReady
28tableSize :: Int
30tableSize :: Int
29tableSize = 0x110000
31tableSize = 0x110000
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.
45+instance E.Exception TableInstallException
31-- | Install a custom unicode character width
47-- | Install a custom unicode character width
32-- table. Such tables are obtained with
48-- table. Such tables are obtained with
33-- 'Graphics.Vty.UnicodeWidthTable.Query.buildUnicodeWidthTable' and
49-- 'Graphics.Vty.UnicodeWidthTable.Query.buildUnicodeWidthTable' and
@@ -61,15 +77,14 @@ tableSize = 0x110000
62-- If this function fails for any reason -- if the table cannot be
78-- If this function fails for any reason -- if the table cannot be
63-- installed or is invalid, or if a custom table already exists -- this
79-- 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.
66-- This function is not thread-safe.
82-- This function is not thread-safe.
67installUnicodeWidthTable :: UnicodeWidthTable -> IO ()
83installUnicodeWidthTable :: UnicodeWidthTable -> IO ()
68installUnicodeWidthTable table = do
84installUnicodeWidthTable table = do
69 initResult <- initCustomTable tableSize
85 initResult <- initCustomTable tableSize
70 when (initResult /= 0) $
86 when (initResult /= 0) $
71− error $ "installUnicodeWidthTable: error initializing " <>
87+ E.throw $ TableInitFailure initResult tableSize
72− "custom table, status " <> show initResult
74 forM_ (unicodeWidthTableRanges table) $ \r -> do
89 forM_ (unicodeWidthTableRanges table) $ \r -> do
75 result <- setCustomTableRange (rangeStart r)
90 result <- setCustomTableRange (rangeStart r)
@@ -78,13 +93,11 @@ installUnicodeWidthTable table = do
79 when (result /= 0) $ do
94 when (result /= 0) $ do
80 deallocateCustomTable
95 deallocateCustomTable
81− error $ "installUnicodeWidthTable: error installing range " <>
96+ E.throw $ TableRangeFailure result r
82− show r <> ", status " <> show result
84 actResult <- activateCustomTable
98 actResult <- activateCustomTable
85 when (actResult /= 0) $
99 when (actResult /= 0) $
86− error $ "installUnicodeWidthTable: error activating custom " <>
100+ E.throw $ TableActivationFailure actResult
87− "table, status " <> show actResult
89------------------------------------------------------------------------
102------------------------------------------------------------------------
90-- C imports
103-- C imports