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