summaryrefslogtreecommitdiff
path: root/unicode.hs
blob: 4aa23b331f7589ddbf7034f08ffc9994fcd14c7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
{-# LANGUAGE ForeignFunctionInterface,CPP #-}

module Unicode where

import Data.List
import Data.Maybe
import qualified Data.Text as T
import qualified Data.Text.IO as IO
import Foreign.C.String
import Numeric
import System.Directory
import System.Environment.Blank
import System.IO
import System.IO.Unsafe

xdgDataHome :: IO [Char]
xdgDataHome = (getEnv "XDG_DATA_HOME") >>= \s-> return
  ((case s of
      Nothing -> "~/.local/share"
      Just x -> if x == "" then "~/.local/share" else x)
    ++ "/pickme")

dataFile :: [Char] -> IO [Char]
dataFile name = xdgDataHome >>= (return . flip (++) ("/" ++ name))

unicodeDataPath :: IO [Char]
unicodeDataPath = dataFile "UnicodeData.txt"

scsvField :: Int -> T.Text -> T.Text
scsvField n t = (T.splitOn (T.pack ";") t)!!n

unicodeCharacters :: IO [T.Text]
unicodeCharacters = unicodeDataPath >>= IO.readFile >>= return . T.lines

characterNames :: IO [T.Text]
characterNames = unicodeCharacters >>= \cs -> return $ map (scsvField 1) cs

searchCharacterByName :: CString -> IO CString
searchCharacterByName s = peekCString s >>= \z ->
  characterNames >>= \cns ->
                       unicodeCharacters >>= \ucs ->
                                               newCString $ map
                                               (\x -> toEnum . fst . (!!0) . readHex $ T.unpack (ucs!!(fromJust $ elemIndex x cns)))
                                               (filter (\s' -> T.isInfixOf ((T.toUpper . T.pack) z) s') cns)

foreign export ccall searchCharacterByName :: CString -> IO CString

favoritesPath :: IO [Char]
favoritesPath = dataFile "favorites"

favoriteEmojis :: IO CString
favoriteEmojis = favoritesPath >>= doesFileExist >>= \ bool -> case bool of
  True -> favoritesPath >>= flip openFile ReadMode >>= hGetLine >>= newCString
  False -> newCString ""


foreign export ccall favoriteEmojis :: IO CString