2
2
module MyGrep.Search (search ) where
3
3
4
4
import MyGrep.Common.Types
5
- import MyGrep.Common.Utils (trim )
5
+ import MyGrep.Common.Utils (trim , fancyPrint )
6
6
import qualified Data.Text.IO as T
7
7
import qualified Data.Text as T
8
8
import System.Directory
@@ -11,38 +11,48 @@ import Data.Maybe (fromMaybe)
11
11
import Control.Exception
12
12
import System.FilePath
13
13
14
- search :: Args -> IO [ ResultLine ]
14
+ search :: Args -> IO ()
15
15
search Args {.. } = do
16
16
case searchLocationType of
17
17
File -> searchInFile $ fromMaybe " " searchLocation
18
18
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))
21
20
where
22
- searchInDirectory :: FilePath -> IO [ ResultLine ]
21
+ searchInDirectory :: FilePath -> IO ()
23
22
searchInDirectory searchLocation' = do
24
23
files_ <- listDirectory searchLocation'
25
24
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)
30
32
31
- searchInFile :: FilePath -> IO [ ResultLine ]
33
+ searchInFile :: FilePath -> IO ()
32
34
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 ])
34
37
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
37
40
38
- searchString :: Int -> FilePath -> [T. Text ] -> [ ResultLine ]
39
- searchString _ _ [] = []
41
+ searchString :: Int -> FilePath -> [T. Text ] -> IO ()
42
+ searchString _ _ [] = pure ()
40
43
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
42
47
case res of
43
48
[] -> 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