{-# LANGUAGE FlexibleInstances, Safe #-} {- This module is part of Chatty. Copyleft (c) 2014 Marvin Cohrs All wrongs reversed. Sharing is an act of love, not crime. Please share Antisplice with everyone you like. Chatty is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Chatty is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with Chatty. If not, see . -} -- | Provides in-haskell implementations for some standard functions module System.Chatty.Commands where import Text.Chatty.Printer import Text.Chatty.Scanner import Text.Chatty.Interactor import Text.Chatty.Finalizer import Text.Chatty.Expansion import Control.Monad import Control.Monad.Trans.Class import Control.Monad.IO.Class import System.IO import System.Directory import System.Posix.Files -- | Like 'cat' on the command line. Accepts a list of filenames. Simple pass-through, if none are provided. cat :: (ChScanner m, ChPrinter m, MonadIO m,Functor m,ChFinalizer m) => [String] -> m () cat [] = mscanL >>= mprint cat [f] = cat [] .<. f cat (f:fs) = cat [f] >> cat fs -- | Like 'cat', but reverses the line order. tac :: (ChFinalizer m,ChScanner m, ChPrinter m, MonadIO m,Functor m) => [String] -> m () tac [] = mscanL >>= (mprint . unlines . reverse . lines) tac fs = cat fs .|. tac [] -- | Pass-through, simultanously writing all input to a given file. tee :: (ChScanner m, ChPrinter m, MonadIO m, Functor m) => String -> m () tee f = do s <- mscanL mprint s .>. f mprint s -- | Prints the given string, after expanding it. echo :: (ChPrinter m,ChExpand m) => String -> m () echo = mprintLn <=< expand -- | Mode for 'wc'. data WcMode = CountChars | CountLines | CountWords -- | Count characters, lines or words of the input. wc :: (ChScanner m, ChPrinter m, MonadIO m, Functor m) => WcMode -> m () wc CountChars = mscanL >>= (mprint . show . length) >> mprint "\n" wc CountLines = mscanL >>= (mprint . show . length . lines) >> mprint "\n" wc CountWords = mscanL >>= (mprint . show . length . words) >> mprint "\n" -- | Change to given directory. cd :: MonadIO m => String -> m () cd = liftIO . setCurrentDirectory -- | Print current working directory. pwd :: (MonadIO m,ChPrinter m) => m () pwd = liftIO getCurrentDirectory >>= mprintLn -- | List directory contents of the given directories (current one, if empty list). ls :: (MonadIO m,ChPrinter m) => [String] -> m () ls [] = liftIO (getDirectoryContents ".") >>= (mprint . unlines) ls [p] = do fs <- liftIO $ getFileStatus p when (isDirectory fs) $ liftIO (getDirectoryContents p) >>= (mprint . unlines) when (isRegularFile fs) $ mprintLn p ls (p:ps) = ls [p] >> ls ps -- | Filters only the first n lines of the input. head :: (ChScanner m,ChPrinter m,MonadIO m,Functor m) => Int -> m () head n = mscanL >>= (mprint . unlines . take n . lines) -- | FIlters only the last n lines of the input. tail :: (ChScanner m,ChPrinter m,MonadIO m,Functor m) => Int -> m () tail n = mscanL >>= (mprint . unlines . reverse . take n . reverse . lines)