Skip to content

Instantly share code, notes, and snippets.

@thumphries
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thumphries/c31d3235fabe885662f9 to your computer and use it in GitHub Desktop.
Save thumphries/c31d3235fabe885662f9 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
import Data.QRCode
import Data.Text (Text, pack, unpack)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Word8
import System.Environment
import System.IO
main = do args <- getArgs
qr (head args) >>= mapM_ T.putStrLn
qr :: String -> IO [Text]
qr s = do qcode <- encodeString s Nothing QR_ECLEVEL_L QR_MODE_EIGHT True
let words = toMatrix qcode
dimh = length . head $ words
border = replicate dimh 0x00 :: [Word8]
words' = border : words ++ [border]
bordered = map (\arr -> 0x00 : arr ++ [0x00]) words'
return $ map qrFmt bordered
qrFmt :: [Word8] -> Text
qrFmt q = T.append (foldr (\c t -> colorise c `T.append` t) "" q) "\x1b[49m"
where colorise c = T.append "\x1b[" $ if c == 0x01
then "40m " -- black
else "47m " -- white
@thumphries
Copy link
Author

I don't remember why this uses Text. Unicode I guess!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment