cgrep 6.6.22 → 6.6.23
raw patch · 11 files changed
+201/−134 lines, 11 filesdep +aesondep +yamldep ~ansi-terminaldep ~arraydep ~async
Dependencies added: aeson, yaml
Dependency ranges changed: ansi-terminal, array, async, bytestring, cmdargs, containers, directory, dlist, either, filepath, ghc-prim, mtl, regex-base, regex-pcre, regex-posix, safe, split, stm, stringsearch, unix-compat, unordered-containers
Files
- README.md +2/−2
- cgrep.cabal +26/−25
- src/CGrep/CGrep.hs +14/−13
- src/CGrep/Common.hs +9/−9
- src/CGrep/Filter.hs +59/−54
- src/CGrep/Lang.hs +12/−5
- src/CGrep/Output.hs +9/−9
- src/CGrep/Token.hs +8/−5
- src/CmdOptions.hs +1/−1
- src/Config.hs +59/−10
- src/Main.hs +2/−1
README.md view
@@ -7,7 +7,7 @@ Usage ----- -Cgrep 6.6.22. Usage: cgrep [OPTION] [PATTERN] files...+Cgrep 6.6.23. Usage: cgrep [OPTION] [PATTERN] files... cgrep [OPTIONS] [ITEM] @@ -70,7 +70,7 @@ --show-match Show list of matching tokens --color Use colors to highlight the matching strings- --no-color Do not use colors (override configAutoColor)+ --no-color Do not use colors (override config file) -h --no-filename Suppress the file name prefix on output --no-numbers Suppress both line and column numbers on output --no-column Suppress the column number on output
cgrep.cabal view
@@ -1,6 +1,6 @@ Name: cgrep Description: Cgrep: a context-aware grep for source codes-Version: 6.6.22+Version: 6.6.23 Synopsis: Command line tool Homepage: http://awgn.github.io/cgrep/ License: GPL-2@@ -8,7 +8,7 @@ Author: Nicola Bonelli Maintainer: Nicola Bonelli <nicola@pfq.io> Category: Utils-Build-type: Simple+Build-type: Simple Stability: Experimental Build-type: Simple Extra-source-files: README.md@@ -18,7 +18,6 @@ Main-Is: Main.hs Hs-Source-Dirs: src Default-Extensions: CPP- Other-Extensions: DeriveDataTypeable, ViewPatterns, MagicHash, @@ -52,30 +51,32 @@ CGrep.Strategy.Regex Paths_cgrep Build-Depends: base < 5.0, - cmdargs >=0.10, - bytestring >=0.10, - directory >=1.2, - filepath >=1.3, - stm >=2.1, - containers >=0.5, - array >=0.4, - ghc-prim >=0.2, - dlist >=0.3, - ansi-terminal >=0.5, - split >=0.2, - safe >=0.3, - stringsearch >=0.3, - unordered-containers >=0.1, - regex-base >=0.90,- regex-posix >=0.90,- regex-pcre >=0.90,- either >= 4.0,- mtl >= 2.0,- unix-compat >= 0.4,- async >= 2.0,+ cmdargs, + bytestring, + directory, + filepath, + stm, + containers, + array, + ghc-prim, + dlist, + ansi-terminal, + split, + safe, + stringsearch, + unordered-containers, + regex-base,+ regex-posix,+ regex-pcre,+ either,+ mtl,+ unix-compat,+ async, utf8-string, unicode-show, transformers,- process+ process,+ aeson,+ yaml Ghc-options: -O2 -Wall -threaded -rtsopts -with-rtsopts=-N Default-language: Haskell2010
src/CGrep/CGrep.hs view
@@ -16,8 +16,12 @@ -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- -module CGrep.CGrep (sanitizeOptions, runCgrep, isRegexp) where+{-# LANGUAGE RecordWildCards #-} +module CGrep.CGrep ( sanitizeOptions+ , runCgrep+ , isRegexp) where+ import qualified CGrep.Strategy.BoyerMoore as BoyerMoore import qualified CGrep.Strategy.Levenshtein as Levenshtein import qualified CGrep.Strategy.Regex as Regex@@ -55,16 +59,15 @@ hasTokenizerOpt :: Options -> Bool-hasTokenizerOpt Options- { identifier = i- , keyword = k- , directive = d- , header = h- , number = n- , string = s- , char = c- , oper = o- } = i || k || d || h || n || s || c || o+hasTokenizerOpt Options{..} =+ identifier ||+ keyword ||+ directive ||+ header ||+ number ||+ string ||+ char ||+ oper isRegexp :: Options -> Bool@@ -81,5 +84,3 @@ | (not . isRegexp) opt -> CppTokenizer.search filename patterns | isRegexp opt -> Regex.search filename patterns | otherwise -> undefined--
src/CGrep/Common.hs view
@@ -16,14 +16,15 @@ -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- -module CGrep.Common (Text8,- getTargetName,- getTargetContents,- shallowSearch,- runSearch,- expandMultiline,- ignoreCase,- trim, trim8) where+module CGrep.Common ( Text8+ , getTargetName+ , getTargetContents+ , shallowSearch+ , runSearch+ , expandMultiline+ , ignoreCase+ , trim+ , trim8) where import qualified Data.ByteString.Char8 as C import qualified Data.ByteString.Search as SC@@ -81,4 +82,3 @@ ignoreCase opt | ignore_case opt = C.map toLowercase | otherwise = id-
src/CGrep/Filter.hs view
@@ -16,9 +16,15 @@ -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- -{-# LANGUAGE ViewPatterns, MagicHash, BangPatterns #-}+{-# LANGUAGE RecordWildCards #-}+{-# LANGUAGE ViewPatterns #-}+{-# LANGUAGE BangPatterns #-}+{-# LANGUAGE MagicHash #-} -module CGrep.Filter (Context(..), ContextFilter(..), contextFilter, mkContextFilter) where+module CGrep.Filter ( Context(..)+ , ContextFilter(..)+ , contextFilter+ , mkContextFilter) where import CGrep.Common (Text8) @@ -39,8 +45,6 @@ type FilterFunction = ContextFilter -> Text8 -> Text8-- type StringBoundary = (String, String) @@ -72,10 +76,10 @@ -- mkContextFilter :: Options -> ContextFilter-mkContextFilter opt =- if not (code opt || comment opt || literal opt)+mkContextFilter Options{..} =+ if not (code || comment || literal) then ContextFilter { getFilterCode = True, getFilterComment = True, getFilterLiteral = True }- else ContextFilter { getFilterCode = code opt, getFilterComment = comment opt, getFilterLiteral = literal opt }+ else ContextFilter { getFilterCode = code , getFilterComment = comment , getFilterLiteral = literal } contextFilter :: Maybe Lang -> ContextFilter -> Text8 -> Text8@@ -92,7 +96,8 @@ -- contextFilterFun :: ParConf -> ContextFilter -> Text8 -> Text8-contextFilterFun conf filt txt = fst $ C.unfoldrN (C.length txt) (contextFilterImpl conf) (txt, filt, ParState CodeState False 0)+contextFilterFun conf filt txt =+ fst $ C.unfoldrN (C.length txt) (contextFilterImpl conf) (txt, filt, ParState CodeState False 0) type ParData = (Text8, ContextFilter, ParState)@@ -105,9 +110,7 @@ !c' = if display s' || isSpace x then x else ' ' contextFilterImpl _ _ = undefined - {-# INLINE displayContext #-}- displayContext :: ContextState -> ContextFilter -> Bool displayContext CodeState (ContextFilter b _ _ ) = b displayContext (CommState _) (ContextFilter _ b _ ) = b@@ -177,10 +180,11 @@ filterFunctionMap :: Map.Map Lang FilterFunction -mkContextFilterFun :: [StringBoundary] -> [StringBoundary] -> FilterFunction-mkContextFilterFun cs ls = contextFilterFun (ParConf (map (\(a,b) -> Boundary (C.pack a) (C.pack b)) cs)- (map (\(a,b) -> Boundary (C.pack a) (C.pack b)) ls)- (mkBloom (cs ++ ls)))+mkFilterFunction :: [StringBoundary] -> [StringBoundary] -> FilterFunction+mkFilterFunction cs ls =+ contextFilterFun (ParConf (map (\(a,b) -> Boundary (C.pack a) (C.pack b)) cs)+ (map (\(a,b) -> Boundary (C.pack a) (C.pack b)) ls)+ (mkBloom (cs ++ ls))) mkBloom :: [StringBoundary] -> UArray Char Bool@@ -188,44 +192,45 @@ filterFunctionMap = Map.fromList- [ (Assembly, mkContextFilterFun [("#", "\n"), (";", "\n"), ("|", "\n"), ("!", "\n"), ("/*", "*/")] [("\"", "\"")] )- , (Awk, mkContextFilterFun [("#", "\n")] [("\"", "\"")] )- , (C, mkContextFilterFun [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )- , (CMake, mkContextFilterFun [("#", "\n")] [("\"", "\"")] )- , (Cabal, mkContextFilterFun [("--", "\n")] [("\"", "\"")] )- , (Chapel, mkContextFilterFun [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )- , (Clojure, mkContextFilterFun [(";", "\n")] [("\"", "\"")] )- , (Coffee, mkContextFilterFun [("###", "###"), ("#", "\n")] [("\"", "\"")] )- , (Conf, mkContextFilterFun [("#", "\n")] [("'", "'"), ("\"", "\"")] )- , (Cpp, mkContextFilterFun [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )- , (Csharp, mkContextFilterFun [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )- , (Css, mkContextFilterFun [("/*", "*/")] [("\"", "\"")] )- , (D, mkContextFilterFun [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )- , (Elixir, mkContextFilterFun [("#", "\n")] [("\"", "\"")] )- , (Erlang, mkContextFilterFun [("%", "\n")] [("\"", "\"")] )- , (Fsharp, mkContextFilterFun [("(*", "*)"), ("//", "\n")] [("\"", "\"")] )- , (Go, mkContextFilterFun [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )- , (Haskell, mkContextFilterFun [("{-", "-}"), ("--", "\n")] [("\"", "\""), ("[r|", "|]"), ("[q|", "|]"), ("[s|", "|]"), ("[here|","|]"), ("[i|", "|]")] )- , (Html, mkContextFilterFun [("<!--", "-->")] [("\"", "\"")] )- , (Idris, mkContextFilterFun [("{-", "-}"), ("--", "\n"), ("|||", "\n")] [("\"", "\"")] )- , (Java, mkContextFilterFun [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )- , (Javascript, mkContextFilterFun [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )- , (Latex, mkContextFilterFun [("%", "\n")] [("\"", "\"")] )- , (Lua, mkContextFilterFun [("--[[","--]]"), ("--", "\n")] [("'", "'"), ("\"", "\""), ("[===[", "]===]"), ("[==[", "]==]"), ("[=[", "]=]"), ("[[", "]]") ] )- , (Make, mkContextFilterFun [("#", "\n")] [("'", "'"), ("\"", "\"")] )- , (OCaml, mkContextFilterFun [("(*", "*)")] [("\"", "\"")] )- , (ObjectiveC, mkContextFilterFun [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )- , (PHP, mkContextFilterFun [("/*", "*/"), ("//", "\n"), ("#", "\n") ] [("'", "'"), ("\"", "\"")] )- , (Perl, mkContextFilterFun [("=pod", "=cut"), ("#", "\n")] [("'", "'"), ("\"", "\"")] )- , (Python, mkContextFilterFun [("#", "\n")] [("\"\"\"", "\"\"\""), ("'''", "'''"), ("'", "'"), ("\"", "\"")] )- , (Ruby, mkContextFilterFun [("=begin", "=end"), ("#", "\n")] [("'", "'"), ("\"", "\""), ("%|", "|"), ("%q(", ")"), ("%Q(", ")") ])- , (Scala, mkContextFilterFun [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )- , (Shell, mkContextFilterFun [("#", "\n")] [("'", "'"), ("\"", "\"")] )- , (Tcl, mkContextFilterFun [("#", "\n")] [("\"", "\"")] )- , (VHDL, mkContextFilterFun [("--", "\n")] [("\"", "\"")] )- , (Verilog, mkContextFilterFun [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )- , (Vim, mkContextFilterFun [("\"", "\n")] [("'", "'")] )- , (Yaml, mkContextFilterFun [("#", "\n")] [("\"", "\"")] )+ [ (Assembly, mkFilterFunction [("#", "\n"), (";", "\n"), ("|", "\n"), ("!", "\n"), ("/*", "*/")] [("\"", "\"")] )+ , (Awk, mkFilterFunction [("#", "\n")] [("\"", "\"")] )+ , (C, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (CMake, mkFilterFunction [("#", "\n")] [("\"", "\"")] )+ , (Cabal, mkFilterFunction [("--", "\n")] [("\"", "\"")] )+ , (Chapel, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (Clojure, mkFilterFunction [(";", "\n")] [("\"", "\"")] )+ , (Coffee, mkFilterFunction [("###", "###"), ("#", "\n")] [("\"", "\"")] )+ , (Conf, mkFilterFunction [("#", "\n")] [("'", "'"), ("\"", "\"")] )+ , (Cpp, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (Csharp, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (Css, mkFilterFunction [("/*", "*/")] [("\"", "\"")] )+ , (D, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (Dart, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\""), ("'", "'")] )+ , (Elixir, mkFilterFunction [("#", "\n")] [("\"", "\"")] )+ , (Erlang, mkFilterFunction [("%", "\n")] [("\"", "\"")] )+ , (Fsharp, mkFilterFunction [("(*", "*)"), ("//", "\n")] [("\"", "\"")] )+ , (Go, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (Haskell, mkFilterFunction [("{-", "-}"), ("--", "\n")] [("\"", "\""), ("[r|", "|]"), ("[q|", "|]"), ("[s|", "|]"), ("[here|","|]"), ("[i|", "|]")] )+ , (Html, mkFilterFunction [("<!--", "-->")] [("\"", "\"")] )+ , (Idris, mkFilterFunction [("{-", "-}"), ("--", "\n"), ("|||", "\n")] [("\"", "\"")] )+ , (Java, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (Javascript, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (Kotlin, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\""), ("'","'"), ("\"\"\"", "\"\"\"")] )+ , (Latex, mkFilterFunction [("%", "\n")] [("\"", "\"")] )+ , (Lua, mkFilterFunction [("--[[","--]]"), ("--", "\n")] [("'", "'"), ("\"", "\""), ("[===[", "]===]"), ("[==[", "]==]"), ("[=[", "]=]"), ("[[", "]]") ] )+ , (Make, mkFilterFunction [("#", "\n")] [("'", "'"), ("\"", "\"")] )+ , (OCaml, mkFilterFunction [("(*", "*)")] [("\"", "\"")] )+ , (ObjectiveC, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (PHP, mkFilterFunction [("/*", "*/"), ("//", "\n"), ("#", "\n") ] [("'", "'"), ("\"", "\"")] )+ , (Perl, mkFilterFunction [("=pod", "=cut"), ("#", "\n")] [("'", "'"), ("\"", "\"")] )+ , (Python, mkFilterFunction [("#", "\n")] [("\"\"\"", "\"\"\""), ("'''", "'''"), ("'", "'"), ("\"", "\"")] )+ , (Ruby, mkFilterFunction [("=begin", "=end"), ("#", "\n")] [("'", "'"), ("\"", "\""), ("%|", "|"), ("%q(", ")"), ("%Q(", ")") ])+ , (Scala, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (Shell, mkFilterFunction [("#", "\n")] [("'", "'"), ("\"", "\"")] )+ , (Swift, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (Tcl, mkFilterFunction [("#", "\n")] [("\"", "\"")] )+ , (VHDL, mkFilterFunction [("--", "\n")] [("\"", "\"")] )+ , (Verilog, mkFilterFunction [("/*", "*/"), ("//", "\n")] [("\"", "\"")] )+ , (Vim, mkFilterFunction [("\"", "\n")] [("'", "'")] )+ , (Yaml, mkFilterFunction [("#", "\n")] [("\"", "\"")] ) ]--
src/CGrep/Lang.hs view
@@ -16,8 +16,12 @@ -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- -module CGrep.Lang (Lang(..), langMap, getFileLang, splitLangList,- dumpLangMap, dumpLangRevMap) where+module CGrep.Lang ( Lang(..)+ , langMap+ , getFileLang+ , splitLangList+ , dumpLangMap+ , dumpLangRevMap) where import qualified Data.Map as Map import System.FilePath(takeExtension, takeFileName)@@ -28,9 +32,10 @@ import Options import Util + data Lang = Assembly | Awk | C | CMake | Cabal | Chapel | Clojure | Coffee | Conf | Cpp | Csharp | Css |- D | Elixir | Erlang | Fortran | Fsharp | Go | Haskell | Html | Idris | Java | Javascript | Latex |- Lua | Make | OCaml | ObjectiveC | PHP | Perl | Python | Ruby | Scala | Shell | Tcl |+ D | Dart | Elixir | Erlang | Fortran | Fsharp | Go | Haskell | Html | Idris | Java | Javascript | Kotlin |+ Latex | Lua | Make | OCaml | ObjectiveC | PHP | Perl | Python | Ruby | Scala | Shell | Swift | Tcl | Text | VHDL | Verilog | Vim | Yaml deriving (Read, Show, Eq, Ord, Bounded) @@ -64,6 +69,7 @@ , (Csharp, [Ext "cs", Ext "CS"]) , (Css, [Ext "css"]) , (D, [Ext "d", Ext "D"])+ , (Dart, [Ext "dart"]) , (Elixir, [Ext "ex", Ext "exs"]) , (Erlang, [Ext "erl", Ext "ERL",Ext "hrl", Ext "HRL"]) , (Fortran, [Ext "f", Ext "for", Ext "ftn",@@ -77,6 +83,7 @@ , (Idris, [Ext "idr", Ext "lidr"]) , (Java, [Ext "java"]) , (Javascript,[Ext "js"])+ , (Kotlin, [Ext "kt", Ext "kts", Ext "ktm"]) , (Latex, [Ext "latex", Ext "tex"]) , (Lua, [Ext "lua"]) , (Make, [Name "Makefile", Name "makefile", Name "GNUmakefile", Ext "mk", Ext "mak"])@@ -88,6 +95,7 @@ , (Ruby, [Ext "rb", Ext "ruby"]) , (Scala, [Ext "scala"]) , (Shell, [Ext "sh", Ext "bash", Ext "csh", Ext "tcsh", Ext "ksh", Ext "zsh"])+ , (Swift, [Ext "swift"]) , (Tcl, [Ext "tcl", Ext "tk"]) , (Text, [Ext "txt", Ext "md", Ext "markdown", Ext "mdown", Ext "mkdn", Ext "mkd", Ext "mdwn", Ext "mdtxt", Ext "mdtext", Ext "text", Name "README", Name "INSTALL", Name "VERSION", Name "LICENSE", Name "AUTHORS", Name "CHANGELOG"]) , (VHDL, [Ext "vhd", Ext "vhdl"])@@ -133,4 +141,3 @@ | '+':xs <- l = (l1, prettyRead xs "Lang" : l2, l3) | '-':xs <- l = (l1, l2, prettyRead xs "Lang" : l3) | otherwise = (prettyRead l "Lang" : l1, l2, l3)-
src/CGrep/Output.hs view
@@ -17,14 +17,14 @@ {-# LANGUAGE CPP #-} -module CGrep.Output (Output(..),- mkOutput,- putPrettyHeader,- putPrettyFooter,- prettyOutput,- showFileName,- showFile,- showBold) where+module CGrep.Output ( Output(..)+ , mkOutput+ , putPrettyHeader+ , putPrettyFooter+ , prettyOutput+ , showFileName+ , showFile+ , showBold) where import qualified Data.ByteString as B import qualified Data.ByteString.Char8 as C@@ -77,6 +77,7 @@ (len_prc, last_prc) = foldl' (\(len,_) cur -> (len + 1, cur)) (0,off) prc in (len_prc, off - last_prc - 1) + mkOutput :: (Monad m) => FilePath -> Text8 -> Text8 -> [Token] -> OptionT m [Output] mkOutput f text multi ts = do invert <- reader (invert_match . snd)@@ -304,4 +305,3 @@ hilightIndicies :: [Token] -> [(Int, Int)] hilightIndicies = foldr (\t a -> let b = fst t in (b, b + length (snd t) - 1) : a) [] . filter (notNull . snd)-
src/CGrep/Token.hs view
@@ -18,8 +18,10 @@ {-# LANGUAGE FlexibleInstances #-} --module CGrep.Token (Token, MatchLine, tokens, tokenizer) where+module CGrep.Token ( Token+ , MatchLine+ , tokens+ , tokenizer) where import qualified Data.ByteString.Char8 as C import qualified Data.DList as DL@@ -90,8 +92,10 @@ tokenizer :: Text8 -> [Token]-tokenizer xs = (\(TokenAccum _ off acc out) -> DL.toList (if null (DL.toList acc) then out- else out `DL.snoc` mkToken off acc)) $ C.foldl' tokens' (TokenAccum StateSpace 0 DL.empty DL.empty) xs+tokenizer xs = (\(TokenAccum _ off acc out) ->+ DL.toList (if null (DL.toList acc) then out+ else out `DL.snoc` mkToken off acc)) $+ C.foldl' tokens' (TokenAccum StateSpace 0 DL.empty DL.empty) xs where tokens' :: TokenAccum -> Char -> TokenAccum tokens' (TokenAccum StateSpace off _ out) x = case () of@@ -133,4 +137,3 @@ else TokenAccum StateDigit (off+1) (DL.singleton x) (out `DL.snoc` mkToken off acc) | isBracketLT ! x -> TokenAccum StateBracket (off+1) (DL.singleton x) (out `DL.snoc` mkToken off acc) | otherwise -> TokenAccum StateOther (off+1) (acc `DL.snoc` x) out-
src/CmdOptions.hs view
@@ -58,7 +58,7 @@ , deference_recursive = False &= help "Recursive, follow symlinks" &= explicit &= name "deference-recursive" &= name "R" , show_match = False &= groupname "\nOutput format" &= help "Show list of matching tokens" &= explicit &= name "show-match" , 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_color = False &= help "Do not use colors (override config file)" &= explicit &= name "no-color" , no_filename = False &= help "Suppress the file name prefix on output" &= explicit &= name "h" &= name "no-filename" , 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"
src/Config.hs view
@@ -16,6 +16,10 @@ -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -- +{-# LANGUAGE DeriveGeneric #-}+{-# LANGUAGE RecordWildCards #-}+{-# LANGUAGE OverloadedStrings #-}+ module Config where import Data.List@@ -25,10 +29,15 @@ import System.Directory import System.FilePath ((</>)) import System.Console.ANSI+import Data.Semigroup ((<>), Semigroup(..)) -import Util-import CGrep.Lang+import qualified Data.Yaml as Y+import Data.Aeson+import Data.Maybe +import GHC.Generics+import CGrep.Lang+import Util cgreprc :: FilePath cgreprc = "cgreprc"@@ -37,35 +46,75 @@ data Config = Config { configLanguages :: [Lang] , configPruneDirs :: [String]- , configAutoColor :: Bool+ , configColors :: Bool , configColorFile :: [SGR] , configColorMatch :: [SGR] } deriving (Show, Read) defaultConfig :: Config- defaultConfig = Config { configLanguages = [] , configPruneDirs = []- , configAutoColor = False+ , configColors = False , configColorFile = [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Blue] , configColorMatch = [SetConsoleIntensity BoldIntensity] } -dropComments :: String -> String-dropComments = unlines . filter notComment . lines- where notComment = (not . ("#" `isPrefixOf`)) . dropWhile isSpace+mkConfig :: YamlConfig -> Config+mkConfig YamlConfig{..} =+ let configLanguages = mapMaybe readMaybe yamlLanguages+ configPruneDirs = yamlPruneDirs+ configColors = yamlColors+ configColorFile = fromMaybe [] (yamlColorFileName >>= readColor)+ configColorMatch = fromMaybe [] (yamlColorMatch >>= readColor)+ in Config {..} +data YamlConfig = YamlConfig+ { yamlLanguages :: [String]+ , yamlPruneDirs :: [String]+ , yamlColors :: Bool+ , yamlColorFileName :: Maybe String+ , yamlColorMatch :: Maybe String+ } deriving (Show, Generic)+++instance Y.FromJSON YamlConfig where+ parseJSON (Y.Object v) =+ YamlConfig <$> v .:? "languages" .!= []+ <*> v .:? "prune_dirs" .!= []+ <*> v .:? "colors" .!= False+ <*> v .:? "color_filename" .!= Nothing+ <*> v .:? "color_match" .!= Nothing++ getConfig :: IO (Config, Maybe FilePath) getConfig = do home <- getHomeDirectory confs <- filterM doesFileExist [cgreprc, "." ++ cgreprc, home </> "." ++ cgreprc, "/etc" </> cgreprc] if notNull confs- then fmap dropComments (readFile (head confs)) >>= \xs ->- return (prettyRead xs "Config error" :: Config, Just (head confs))+ then do+ conf <- Y.decodeFileEither (head confs)+ print conf+ case conf of+ Left e -> errorWithoutStackTrace $ Y.prettyPrintParseException e+ Right yconf -> return (mkConfig yconf, Just (head confs)) else return (defaultConfig, Nothing)+++readColor :: String -> Maybe [SGR]+readColor "Bold" = Just [SetConsoleIntensity BoldIntensity]+readColor "Red" = Just [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Red]+readColor "Green" = Just [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Green]+readColor "Yellow" = Just [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Yellow]+readColor "Blue" = Just [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Blue]+readColor "Magenta" = Just [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Magenta]+readColor "Cyan" = Just [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid Cyan]+readColor "White" = Just [SetConsoleIntensity BoldIntensity, SetColor Foreground Vivid White]+readColor _ = Nothing++
src/Main.hs view
@@ -219,7 +219,7 @@ -- read command-line options opts <- (if isTermOut- then \o -> o { color = color o || configAutoColor conf }+ then \o -> o { color = color o || configColors conf } else id) <$> cmdArgsRun options -- check for multiple backends...@@ -274,6 +274,7 @@ runReaderT (do putStrLevel1 $ "Cgrep " ++ showVersion version ++ "!" putStrLevel1 $ "options : " ++ show opts+ putStrLevel1 $ "config : " ++ show conf putStrLevel1 $ "languages : " ++ show langs putStrLevel1 $ "pattern : " ++ show patterns' putStrLevel1 $ "files : " ++ show paths