Created
January 16, 2015 13:10
-
-
Save bmjames/868e21159801dbae8eb6 to your computer and use it in GitHub Desktop.
Utility which pretty-prints and highlights JSON in output from ngrep (with -W byline)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Data.Aeson hiding (Result) | |
import Data.Aeson.Encode.Pretty (encodePretty) | |
import Data.Attoparsec.ByteString | |
import Data.ByteString (ByteString) | |
import qualified Data.ByteString.Char8 as BS | |
import Data.ByteString.Lazy (toStrict) | |
import System.Console.ANSI | |
go :: IO () | |
go = do line <- getLine | |
step ([line], parse json line) | |
where | |
step :: ([ByteString], Result Value) -> IO () | |
step (ls, Fail _ _ _) = do mapM_ BS.putStrLn (reverse ls) | |
go | |
step (ls, Partial f) = do l <- getLine | |
step (l:ls, f l) | |
step (_, Done _ val) = do setSGR [green] | |
BS.putStrLn $ toStrict $ encodePretty val | |
setSGR [] | |
go | |
getLine :: IO ByteString | |
getLine = do line <- BS.getLine | |
return $ case BS.unsnoc line of | |
Just (s, '.') -> s | |
_ -> line | |
green :: SGR | |
green = SetColor Foreground Dull Green | |
main :: IO () | |
main = go |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment