Commit 2db683f4 authored by Nicolas Lenz's avatar Nicolas Lenz
Browse files

Convert API to Text instead of String, v3.0.0

parent ddbd211a
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Mock (styles)
import Data.List
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Char
import Data.Maybe
import System.Environment
......@@ -10,22 +13,24 @@ import System.Environment
-- |Main function.
main :: IO ()
main = do
args <- getArgs
args' <- getArgs
let args = map T.pack args' -- Transform String arguments into Text
case args of
[] -> putStrLn help
["--help"] -> putStrLn help
[] -> T.putStrLn help
["--help"] -> T.putStrLn help
[style] -> do
input <- getContents -- Read from stdin
putStrLn $ handle style [input]
(style:text) -> putStrLn $ handle style text
input <- T.getContents -- Read from stdin
T.putStrLn $ handle style [input]
(style:str) -> T.putStrLn $ handle style str
-- |Returns an IO action handling the given list of arguments.
handle :: String -> [String] -> String
handle style = fromMaybe (const help) (lookup style styles) . dropWhileEnd isSpace . intercalate " "
handle :: T.Text -> [T.Text] -> T.Text
handle style = fromMaybe (const help) (lookup style styles) . T.dropWhileEnd isSpace . T.intercalate " "
-- |Help string.
help :: String
help = "Mock - a program to transform text.\n\
\\n\
\Usage: mock [STYLE] [TEXT]\n\
\Styles: " ++ (intercalate ", " $ map fst styles)
help :: T.Text
help = T.unlines [
"Mock 3.0.0 - a program to transform text.",
"",
"Usage: mock [STYLE] [TEXT]",
"Styles: " `T.append` (T.intercalate ", " $ map fst styles)]
......@@ -4,10 +4,10 @@ cabal-version: 2.2
--
-- see: https://github.com/sol/hpack
--
-- hash: 386f26c4a9d9d6902416cd8ff7c1215a43e9a87f681dba72300c5060f7419567
-- hash: 17611a7df00c9bd277a9091735ad4a0705598afdc3db7b64ac5852af2f642fa8
name: mock
version: 2.0.0
version: 3.0.0
synopsis: GrEAt HAskeLL PrOGRaM to trANsForm tEXT
description: Please see the README at <https://git.eisfunke.com/software/mock#readme>
category: String
......
name: mock
version: 2.0.0
version: 3.0.0
license: WTFPL
git: "https://git.eisfunke.com/software/mock"
author: "Nicolas Lenz"
......@@ -17,7 +17,6 @@ dependencies:
- base >= 4.7 && < 5
- random
- hashable
- time
- text
library:
......
module Mock (styles, mockAlternate, mockRandom, letterspace, toDS) where
{-# LANGUAGE OverloadedStrings #-}
module Mock (styles, mockAlternate, mockRandom, letterspace, toDouble) where
import Data.Char
import Data.List
import qualified Data.Text as T
import Data.Hashable
import System.Random
-- |List of possible mock style names and their functions.
styles :: [(String, String -> String)]
styles :: [(T.Text, T.Text -> T.Text)]
styles = [
("random", mockRandom),
("alternate", mockAlternate),
("space", letterspace 1),
("space2", letterspace 2),
("space3", letterspace 3),
("lines", intersperse '\n'),
("upper", map toUpper),
("lower", map toLower),
("double", map toDS)]
("lines", T.intersperse '\n'),
("upper", T.map toUpper),
("lower", T.map toLower),
("double", T.map toDouble)]
-- |Transforms a String into uppercase where the corresponding list is True. For False the String isn't changed.
toUpperBy :: String -> [Bool] -> String
toUpperBy (c:cs) (True:bs) = toUpper c : toUpperBy cs bs
toUpperBy (c:cs) (False:bs) = c : toUpperBy cs bs
toUpperBy (cs) [] = cs
toUpperBy [] _ = []
toUpperBy :: [Bool] -> T.Text -> T.Text
toUpperBy bs = T.pack . zipWith f bs . T.unpack where
f :: Bool -> Char -> Char
f True c = toUpper c
f False c = c
-- |Transforms every other of the Chars of a String into uppercase. The other Chars aren't changed.
mockAlternate :: String -> String
mockAlternate str = toUpperBy str $ intersperse True $ repeat False
mockAlternate :: T.Text -> T.Text
mockAlternate = toUpperBy $ intersperse True $ repeat False
-- |Tansforms random (that is, random per input String) Chars of a String into uppercase.
mockRandom :: String -> String
mockRandom str = toUpperBy str (randoms $ mkStdGen (hash str))
mockRandom :: T.Text -> T.Text
mockRandom txt = toUpperBy (randoms $ mkStdGen (hash txt)) txt
-- |Letterspaces a String with the given number of blanks between the Chars.
letterspace :: Int -> String -> String
letterspace n = intercalate (replicate n ' ') . map (\c -> [c])
letterspace :: Int -> T.Text -> T.Text
letterspace n = T.pack . intercalate (replicate n ' ') . map (\c -> [c]) . T.unpack
-- |Transforms a character into its double-struck variant (if it is alphanumeric, else it is left unchanged).
toDS :: Char -> Char
toDS 'C' = chr 8450
toDS 'H' = chr 8461
toDS 'N' = chr 8469
toDS 'P' = chr 8473
toDS 'Q' = chr 8474
toDS 'R' = chr 8477
toDS 'Z' = chr 8484
toDS c
toDouble :: Char -> Char
toDouble 'C' = chr 8450
toDouble 'H' = chr 8461
toDouble 'N' = chr 8469
toDouble 'P' = chr 8473
toDouble 'Q' = chr 8474
toDouble 'R' = chr 8477
toDouble 'Z' = chr 8484
toDouble c
| 48 <= ord c && ord c <= 57 = chr $ ord c - 48 + 120792 -- Number
| 65 <= ord c && ord c <= 90 = chr $ ord c - 65 + 120120 -- Uppercase letter
| 97 <= ord c && ord c <= 122 = chr $ ord c - 97 + 120146 -- Lowercase letter
toDS c = c
toDouble c = c
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment