on 44c99ed help sign in
vty/src/Graphics/Text/Width.hs
29 lines 1.1 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 = max 0 . wcwidth
27-- | Returns the display width of a string. Assumes all characters with unknown widths are 0 width
28safeWcswidth :: String -> Int
29safeWcswidth = sum . map safeWcwidth