on 021a059 help sign in
vty/src/Graphics/Text/Width.hs
34 lines 1.2 kB history raw
1-- Copyright 2009 Corey O'Connor
2{-# OPTIONS_GHC -D_XOPEN_SOURCE #-}
3{-# LANGUAGE ForeignFunctionInterface #-}
4module Graphics.Text.Width ( wcwidth
5 , wcswidth
6 , safeWcwidth
7 , safeWcswidth
8 )
9 where
11foreign import ccall unsafe "vty_mk_wcwidth" wcwidth :: Char -> Int
13wcswidth :: String -> Int
14wcswidth = sum . map wcwidth
16-- XXX: Characters with unknown widths occupy 1 column?
17--
18-- Not sure if this is actually correct. I presume there is a replacement character that is output
19-- by the terminal instead of the character and this replacement character is 1 column wide. If this
20-- is not true for all terminals then a per-terminal replacement character width needs to be
21-- implemented.
23-- | Returns the display width of a character. Assumes all characters with unknown widths are 0 width
24safeWcwidth :: Char -> Int
25safeWcwidth c = case wcwidth c of
26 i | i < 0 -> 0
27 | otherwise -> i
29-- | Returns the display width of a string. Assumes all characters with unknown widths are 0 width
30safeWcswidth :: String -> Int
31safeWcswidth str = case wcswidth str of
32 i | i < 0 -> 0
33 | otherwise -> i