on master help sign in
Merge pull request #105 from glguy/fix-wcwidth
e729e2aJonathan Daugherty
src/Graphics/Text/Width.hs+8−8
@@ -8,7 +8,12 @@ module Graphics.Text.Width ( wcwidth
8 )
8 )
9 where
9 where
10
10
11+import Foreign.C.Types (CInt(..))
12+
11−foreign import ccall unsafe "vty_mk_wcwidth" wcwidth :: Char -> Int
13+foreign import ccall unsafe "vty_mk_wcwidth" c_wcwidth :: Char -> CInt
14+
15+wcwidth :: Char -> Int
16+wcwidth = fromIntegral . c_wcwidth
12
17
13wcswidth :: String -> Int
18wcswidth :: String -> Int
14wcswidth = sum . map wcwidth
19wcswidth = sum . map wcwidth
@@ -22,13 +27,8 @@ wcswidth = sum . map wcwidth
22
27
23-- | Returns the display width of a character. Assumes all characters with unknown widths are 0 width
28-- | Returns the display width of a character. Assumes all characters with unknown widths are 0 width
24safeWcwidth :: Char -> Int
29safeWcwidth :: Char -> Int
25−safeWcwidth c = case wcwidth c of
30+safeWcwidth = max 0 . wcwidth
26− i | i < 0 -> 0
27− | otherwise -> i
28
31
29-- | Returns the display width of a string. Assumes all characters with unknown widths are 0 width
32-- | Returns the display width of a string. Assumes all characters with unknown widths are 0 width
30safeWcswidth :: String -> Int
33safeWcswidth :: String -> Int
31−safeWcswidth str = case wcswidth str of
34+safeWcswidth = sum . map safeWcwidth
32− i | i < 0 -> 0
33− | otherwise -> i
34−