Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module Main where
  2. import Options.Applicative
  3. import Data.Semigroup ((<>))
  4. import System.IO
  5. import Control.Monad
  6. import System.Environment (getArgs)
  7. import Prelude
  8. import System.Directory
  9.  
  10. data Sample = Sample
  11.   { lines      :: Bool
  12.   , bytes      :: Bool
  13.   , chars :: Bool
  14.   , words :: Bool
  15.   , file :: String }
  16.  
  17. data Input
  18.   = FileInput FilePath
  19.   | StdInput
  20.  
  21. sample :: Parser Sample
  22. sample = Sample
  23.       <$> switch
  24.           ( long "lines"
  25.          <> short 'l'
  26.          <> help "Number of lines" )
  27.       <*> switch
  28.           ( long "bytes"
  29.          <> short 'b'
  30.          <> help "Number of bytes" )
  31.       <*> switch
  32.           ( long "chars"
  33.          <> short 'c'
  34.          <> help "Number of chars" )
  35.       <*> switch
  36.           ( long "words"
  37.          <> short 'w'
  38.          <> help "Number of words" )
  39.       <*> argument str (metavar "FILE"
  40.                         <> value "")
  41.      
  42.  
  43.          
  44. main :: IO ()
  45. main = do
  46.   greet =<< execParser opts
  47.   where
  48.     opts = info (sample <**> helper)
  49.       ( fullDesc
  50.      <> progDesc "Print information about FILE" )
  51.  
  52.  
  53. greet :: Sample -> IO ()
  54. greet (Sample False False False False "") = getStdin [True, True, True, True]
  55. greet (Sample a b c d "") = getStdin [a, b, c, d]
  56. greet (Sample False False False False f) = getFile ([True, True, True, True], f)
  57. greet (Sample a b c d f) = getFile ([a, b, c, d], f)
  58. greet _ = return ()
  59.  
  60.  
  61. getFile :: ([Bool], String) -> IO ()
  62. getFile ([a, b, c, d], file) = do
  63.   if a then (lineNumber file) else (putStr "")
  64.   if b then (wordsNumber file) else (putStr "")
  65.   if c then (charsNumber file) else (putStr "")
  66.   if d then (bytesNumber file) else (putStr "")
  67.   putStrLn ""
  68.  
  69. getStdin :: [Bool] -> IO ()
  70. getStdin [a, b, c, d] = do
  71.   text <- hGetContents stdin
  72.   if a then (lineNumber1 text) else (putStr "")
  73.   if b then (wordsNumber1 text) else (putStr "")
  74.   if c then (charsNumber1 text) else (putStr "")
  75.   if d then (bytesNumber1 text) else (putStr "")
  76.   putStrLn ""
  77.  
  78.  
  79. lineNumber :: String -> IO ()
  80. lineNumber file = do
  81.   text <- readFile file
  82.   putStr (show (length (Prelude.lines text)))
  83.   putStr " "
  84.  
  85. wordsNumber :: String -> IO()
  86. wordsNumber file = do
  87.   text <- readFile file
  88.   putStr (show (length (Prelude.words text)))
  89.   putStr " "
  90.  
  91. charsNumber :: String -> IO()
  92. charsNumber file = do
  93.   text <- readFile file
  94.   putStr (show ((length text) - (length (Prelude.lines text)) + 1))
  95.   putStr " "
  96.  
  97. bytesNumber :: String -> IO()
  98. bytesNumber file = do
  99.   x <- getFileSize file
  100.   print x
  101.  
  102. lineNumber1 :: String -> IO ()
  103. lineNumber1 text = do
  104.   putStr (show (length (Prelude.lines text)))
  105.   putStr " "
  106.  
  107. wordsNumber1 :: String -> IO()
  108. wordsNumber1 text = do
  109.   putStr (show (length (Prelude.words text)))
  110.   putStr " "
  111.  
  112. charsNumber1 :: String -> IO()
  113. charsNumber1 text = do
  114.   putStr (show ((length text) - (length (Prelude.lines text)) + 1))
  115.   putStr " "
  116.  
  117. bytesNumber1 :: String -> IO()
  118. bytesNumber1 text = do
  119.   putStr (show (length text))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement