Skip to content

Instantly share code, notes, and snippets.

@bmjames
Created January 16, 2015 13:10
Show Gist options
  • Save bmjames/868e21159801dbae8eb6 to your computer and use it in GitHub Desktop.
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)
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