cgrep 6.6.7 → 6.6.8
raw patch · 6 files changed
+57/−38 lines, 6 files
Files
- README.md +7/−4
- cgrep.cabal +1/−1
- src/CGrep/Output.hs +44/−30
- src/CmdOptions.hs +2/−1
- src/Main.hs +1/−1
- src/Options.hs +2/−1
README.md view
@@ -7,7 +7,7 @@ Usage ----- -Cgrep 6.6.7. Usage: cgrep [OPTION] [PATTERN] files...+Cgrep 6.6.8. Usage: cgrep [OPTION] [PATTERN] files... cgrep [OPTIONS] [ITEM] @@ -72,15 +72,18 @@ --color Use colors to highlight the matching strings --no-color Do not use colors (override configAutoColor) -h --no-filename Suppress the file name prefix on output- -N --no-linenumber Suppress the line number on output lines+ --no-numbers Suppress both line and column numbers on output+ --no-column Suppress the column number on output --count Print only a count of matching lines per file --filename-only Print only the name of files containing matches --format=STRING Format output. Var: #f #n #l #t ## #, #; #0 #1... e.g. "#f:#n #0 #1" --json Format output as json object --xml Format output as xml document- --vim Invoke vim program by passing the files that- match+ --editor Run the editor specified by EDITOR var., passing+ the files that match+ --vim Run vim the editor passing the files that match+ Concurrency:
cgrep.cabal view
@@ -1,6 +1,6 @@ Name: cgrep Description: Cgrep: a context-aware grep for source codes-Version: 6.6.7+Version: 6.6.8 Synopsis: Command line tool Homepage: http://awgn.github.io/cgrep/ License: GPL-2
src/CGrep/Output.hs view
@@ -20,6 +20,7 @@ putPrettyHeader, putPrettyFooter, prettyOutput,+ showFileName, showFile, showBold) where @@ -30,6 +31,7 @@ import Language.Haskell.Interpreter #endif +import Control.Monad import Control.Monad.Trans.Reader import Control.Monad.IO.Class @@ -120,14 +122,13 @@ defaultOutput xs = do opt <- ask case () of- _ | Options{ no_filename = False, no_linenumber = False , count = False } <- opt -> return $ map (\(Output f n l ts) -> showFile opt f ++ ":" ++ show n ++ ":" ++ showTokens opt ts ++ showLine opt ts l) xs- | Options{ no_filename = False, no_linenumber = True , count = False } <- opt -> return $ map (\(Output f _ l ts) -> showFile opt f ++ ":" ++ showTokens opt ts ++ showLine opt ts l) xs- | Options{ no_filename = True , no_linenumber = False , count = False } <- opt -> return $ map (\(Output _ n l ts) -> show n ++ ":" ++ showTokens opt ts ++ showLine opt ts l) xs- | Options{ no_filename = True , no_linenumber = True , count = False } <- opt -> return $ map (\(Output _ _ l ts) -> showTokens opt ts ++ showLine opt ts l) xs+ _ | Options{ no_filename = False, no_numbers = False , count = False } <- opt -> return $ map (\out -> mconcat $ [showFile, showSep ":", showLineCol, showSep ":", showTokens, showLine ] `ap` [opt] `ap` [out]) xs+ | Options{ no_filename = False, no_numbers = True , count = False } <- opt -> return $ map (\out -> mconcat $ [showFile, showSep ":", showTokens, showLine] `ap` [opt] `ap` [out] ) xs+ | Options{ no_filename = True , no_numbers = False , count = False } <- opt -> return $ map (\out -> mconcat $ [showLineCol, showSep ":", showTokens, showLine] `ap` [opt] `ap` [out] ) xs+ | Options{ no_filename = True , no_numbers = True , count = False } <- opt -> return $ map (\out -> mconcat $ [showTokens, showLine] `ap` [opt] `ap` [out]) xs | Options{ count = True } <- opt -> do let gs = groupBy (\(Output f1 _ _ _) (Output f2 _ _ _) -> f1 == f2) xs- return $ map (\ys@(y:_) -> showFile opt (outputFilename y) ++ ":" ++ show (length ys)) gs+ return $ map (\ys@(y:_) -> showFile opt y ++ ":" ++ show (length ys)) gs | otherwise -> undefined- where outputFilename (Output f _ _ _) = f jsonOutput :: (Monad m) => [Output] -> ReaderT Options m [String]@@ -159,13 +160,13 @@ formatOutput :: (Monad m) => Output -> ReaderT Options m String-formatOutput (Output f n l ts) = do+formatOutput out = do opt <- ask return $ replace (fromJust $ format opt) [- ("#f", showFile opt f),- ("#n", show n),- ("#l", showLine opt ts l),+ ("#f", showFile opt out),+ ("#n", showLineCol opt out),+ ("#l", showLine opt out), ("#t", show ts'), ("##", unwords ts'), ("#,", intercalate "," ts'),@@ -181,7 +182,7 @@ ("#8", atDef "" ts' 8), ("#9", atDef "" ts' 9) ]- where ts' = map snd ts+ where ts' = map snd (outTokens out) replace :: String -> [(String, String)] -> String@@ -199,12 +200,12 @@ let cmds = map mkCmd outs out <- runInterpreter $ setImports ["Prelude", "Data.List"] >> mapM (`interpret` (as :: String)) cmds return $ either ((:[]) . show) id out- where mkCmd (Output f n l ts) = "let a # b = a !! b " ++- "; file = " ++ show (showFile opt f) ++- "; row = " ++ show n ++- "; line = " ++ show (showLine opt ts l) ++- "; tokens = " ++ show (map snd ts) ++ " in " ++- (fromJust $ hint opt)+ where mkCmd out@(Output f n l ts) = "let a # b = a !! b " +++ "; file = " ++ show (showFile opt out) +++ "; row = " ++ show n +++ "; line = " ++ show (showLine opt ts l) +++ "; tokens = " ++ show (map snd ts) ++ " in " +++ (fromJust $ hint opt) #endif blue, bold, resetTerm :: String@@ -217,30 +218,43 @@ type ColorString = String -showColor :: Options -> ColorString -> String -> String-showColor Options { color = c, no_color = c'} colorCode f- | c && not c'= colorCode ++ f ++ resetTerm- | otherwise = f+showSep :: String -> Options -> Output -> String+showSep xs _ _ = xs -showTokens :: Options -> [Token] -> String-showTokens Options { show_match = st } xs- | st = show (map snd xs)+showFile :: Options -> Output -> String+showFile opt = showFileName opt . outFilePath+++showLineCol :: Options -> Output -> String+showLineCol Options{no_numbers = True } _ = ""+showLineCol Options{no_numbers = False, no_column = True } (Output _ n _ _) = show n+showLineCol Options{no_numbers = False, no_column = False } (Output _ n _ ts) = show n ++ ":" ++ show ((+1) . fst . head $ ts)+++showTokens :: Options -> Output -> String+showTokens Options { show_match = st } out+ | st = show (map snd (outTokens out)) | otherwise = "" +showLine :: Options -> Output -> String+showLine Options { color = c, no_color = c' } out+ | c && not c'= hilightLine (sortBy (flip compare `on` (length . snd )) (outTokens out)) (C.unpack (outLine out))+ | otherwise = C.unpack (outLine out) -showFile :: Options -> String -> String-showFile opt = showColor opt (bold ++ blue) +showFileName :: Options -> String -> String+showFileName opt = showColor opt (bold ++ blue) + showBold :: Options -> String -> String showBold opt = showColor opt bold -showLine :: Options -> [Token] -> Line8 -> String-showLine Options { color = c, no_color = c' } ts l- | c && not c'= hilightLine (sortBy (flip compare `on` (length . snd )) ts) (C.unpack l)- | otherwise = C.unpack l+showColor :: Options -> ColorString -> String -> String+showColor Options { color = c, no_color = c'} colorCode f+ | c && not c'= colorCode ++ f ++ resetTerm+ | otherwise = f hilightLine :: [Token] -> String -> String
src/CmdOptions.hs view
@@ -60,7 +60,8 @@ , color = False &= help "Use colors to highlight the matching strings" &= explicit &= name "color" , no_color = False &= help "Do not use colors (override configAutoColor)" &= explicit &= name "no-color" , no_filename = False &= help "Suppress the file name prefix on output" &= explicit &= name "h" &= name "no-filename"- , no_linenumber= False &= help "Suppress the line number on output lines" &= explicit &= name "N" &= name "no-linenumber"+ , no_numbers = False &= help "Suppress both line and column numbers on output" &= explicit &= name "no-numbers"+ , no_column = False &= help "Suppress the column number on output" &= explicit &= name "no-column" , count = False &= help "Print only a count of matching lines per file" &= explicit &= name "count" , filename_only = False &= help "Print only the name of files containing matches" &= explicit &= name "filename-only" , editor = False &= help "Run the editor specified by EDITOR var., passing the files that match" &= explicit &=name "editor"
src/Main.hs view
@@ -150,7 +150,7 @@ out <- fmap (take (max_count opts)) (runReaderT (runCgrep x patterns) (sanitizeOptions x opts)) unless (null out) $ atomically $ writeTChan out_chan out) )- (\e -> let msg = show (e :: SomeException) in hPutStrLn stderr (showFile opts (getTargetName (head fs)) ++ ": exception: " ++ if length msg > 80 then take 80 msg ++ "..." else msg))+ (\e -> let msg = show (e :: SomeException) in hPutStrLn stderr (showFileName opts (getTargetName (head fs)) ++ ": exception: " ++ if length msg > 80 then take 80 msg ++ "..." else msg)) when (null fs) $ left ()
src/Options.hs view
@@ -63,7 +63,8 @@ , color :: Bool , no_color :: Bool , no_filename :: Bool- , no_linenumber :: Bool+ , no_numbers :: Bool+ , no_column :: Bool , count :: Bool , filename_only :: Bool #ifdef ENABLE_HINT