module Graphics.UI.SybWidget.PriLabel
( PriLabel(..)
, Priority(..)
, badConstrLabel, goodConstrLabel, fieldNameLabel, userDefinedLabel
, bestLabel, humanizeLabel
, defaultLabel, labelless
)
where
import Char
data PriLabel = PriLabel { priority :: Priority, labelString :: String } deriving (Show, Eq)
data Priority = BadConstr | GoodConstr | FieldName | UserDefined deriving (Show, Ord, Eq, Bounded, Enum)
badConstrLabel, goodConstrLabel, fieldNameLabel, userDefinedLabel :: String -> PriLabel
badConstrLabel label = PriLabel BadConstr label
goodConstrLabel label = PriLabel GoodConstr label
fieldNameLabel label = PriLabel FieldName label
userDefinedLabel label = PriLabel UserDefined label
defaultLabel :: String -> PriLabel
defaultLabel label = PriLabel BadConstr label
labelless :: PriLabel
labelless = defaultLabel ""
bestLabel :: PriLabel -> PriLabel -> PriLabel
bestLabel left@(PriLabel priL _) right@(PriLabel priR _)
| priL >= priR = left
| otherwise = right
humanizeLabel :: PriLabel -> PriLabel
humanizeLabel (PriLabel pri label) = PriLabel pri label'
where
label' | pri == UserDefined || not (and (map isLegitChar label))
= label
| elem '_' label = (first toUpper . nonCamel) label
| otherwise = camelCase label
isLegitChar x = isAlphaNum x || elem x ['_', '\'']
nonCamel [] = []
nonCamel ('_':[]) = []
nonCamel (x:[]) = x:[]
nonCamel ('_':x:[]) = ' ':toUpper x:[]
nonCamel (x:y:[]) = x:nonCamel [y]
nonCamel ('_':x:y:xs)
| isUpper y = ' ':toUpper x:nonCamel (y:xs)
| otherwise = ' ':toLower x:nonCamel (y:xs)
nonCamel (x:xs) = x:nonCamel xs
camelCase [] = []
camelCase (x:xs) = toUpper x : (seperateWords [] xs)
seperateWords cs [] = cs
seperateWords [] (x:xs)
| isUpper x = ' ':seperateWords [x] xs
| otherwise = x:seperateWords [] xs
seperateWords (c:[]) (x:xs)
| isUpper x = seperateWords (c:x:[]) xs
| otherwise = toLower c:x:seperateWords [] (xs)
seperateWords (cs) (x:xs)
| isUpper x = seperateWords (cs ++ [x]) xs
| otherwise = (init cs) ++ ' ':(toLower $ last cs):x:seperateWords [] xs
first _ [] = []
first f (x:xs) = (f x):xs