22module MyGrep.Search (search ) where
33
44import MyGrep.Common.Types
5- import MyGrep.Common.Utils (trim )
5+ import MyGrep.Common.Utils (trim , fancyPrint )
66import qualified Data.Text.IO as T
77import qualified Data.Text as T
88import System.Directory
@@ -11,38 +11,48 @@ import Data.Maybe (fromMaybe)
1111import Control.Exception
1212import System.FilePath
1313
14- search :: Args -> IO [ ResultLine ]
14+ search :: Args -> IO ()
1515search Args {.. } = do
1616 case searchLocationType of
1717 File -> searchInFile $ fromMaybe " " searchLocation
1818 Directory -> searchInDirectory $ fromMaybe " " searchLocation
19- STDIN -> searchString 0 " " . T. lines <$> T. getContents
20- _ -> pure [] -- impossible case
19+ STDIN -> T. getContents >>= (\ content -> searchString 0 " " (T. lines content))
2120 where
22- searchInDirectory :: FilePath -> IO [ ResultLine ]
21+ searchInDirectory :: FilePath -> IO ()
2322 searchInDirectory searchLocation' = do
2423 files_ <- listDirectory searchLocation'
2524 let files = filter (\ x -> head x /= ' .' ) files_ -- filtering out hidden directories
26- mconcat $ map (\ file -> do
27- res <- doesDirectoryExist (searchLocation' </> file)
28- (if res then searchInDirectory (searchLocation' </> file) else searchInFile (searchLocation' </> file))
29- ) files
25+ mapM_ go files
26+ where
27+ go :: FilePath -> IO ()
28+ go fp = do
29+ let fullPath = searchLocation' </> fp
30+ doesDirectoryExist fullPath >>= (\ isDir -> if isDir then searchInDirectory fullPath
31+ else searchInFile fullPath)
3032
31- searchInFile :: FilePath -> IO [ ResultLine ]
33+ searchInFile :: FilePath -> IO ()
3234 searchInFile fileName = do
33- eContent <- try $ T. lines <$> T. readFile fileName :: IO (Either IOError [T. Text ])
35+ eContent <- try $ T. lines <$> T. readFile fileName
36+ :: IO (Either IOError [T. Text ])
3437 case eContent of
35- Left _ -> pure [] -- print (e :: IOError)
36- Right content -> pure $ searchString 0 fileName content
38+ Left _ -> pure () -- print (e :: IOError)
39+ Right content -> searchString 0 fileName content
3740
38- searchString :: Int -> FilePath -> [T. Text ] -> [ ResultLine ]
39- searchString _ _ [] = []
41+ searchString :: Int -> FilePath -> [T. Text ] -> IO ()
42+ searchString _ _ [] = pure ()
4043 searchString n fileName (hayStack: hayStacks) = do
41- let res = if CaseInSensitive `elem` flags then indices (T. toLower searchTerm) (T. toLower hayStack) else indices searchTerm hayStack
44+ let res = if CaseInSensitive `elem` flags
45+ then indices (T. toLower searchTerm) (T. toLower hayStack)
46+ else indices searchTerm hayStack
4247 case res of
4348 [] -> searchString (n+ 1 ) fileName hayStacks
44- _ -> ResultLine {
45- file= Just fileName
46- , lineNumber= if LineNumber `elem` flags then Just n else Nothing
47- , line= trim hayStack
48- } : searchString (n+ 1 ) fileName hayStacks
49+ _ -> do
50+ let
51+ resultLine = ResultLine {
52+ file= Just fileName
53+ , lineNumber= if LineNumber `elem` flags
54+ then Just n else Nothing
55+ , line= trim hayStack
56+ }
57+ fancyPrint resultLine
58+ searchString (n+ 1 ) fileName hayStacks
0 commit comments