Skip to content

Instantly share code, notes, and snippets.

@banacorn
Created September 11, 2014 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banacorn/3be9c34372ffaf77a81d to your computer and use it in GitHub Desktop.
Save banacorn/3be9c34372ffaf77a81d to your computer and use it in GitHub Desktop.
eavesdropping on Agda and Emacs
{-# LANGUAGE OverloadedStrings #-}
import Control.Concurrent
import Data.ByteString (ByteString, empty)
import Data.ByteString.Char8 (pack, unpack)
import System.Environment (getArgs)
import System.IO (openFile, IOMode(..))
import System.IO.Streams
import System.Console.ANSI
import Prelude hiding (read)
--
-- Agda Eve Bob
-- ^ ^
-- | |
-- hout -----------|--------> stdout
-- |
-- hin <-------------------- stdin
--
main :: IO ()
main = do
args <- getArgs
(hin, hout, _, _) <- runAgda args
-- stream to Eve's log
logHandle <- openFile logPath AppendMode
logStream <- handleToOutputStream logHandle
-- eavesdropping on Agda <------ Bob
toAgda <- makeOutputStream $ \x -> do
write x hin
write (Just empty) hin
write (fmap paintInput x) logStream
write (Just empty) logStream
forkIO $ stdin `connect` toAgda
-- eavesdropping on Agda ------> Bob
fromAgda <- makeOutputStream $ \x -> do
write x stdout
write (Just empty) stdout
write (fmap paintOutput x) logStream
write (Just empty) logStream
hout `connect` fromAgda
where runAgda args = runInteractiveProcess
agdaPath
args
Nothing
Nothing
agdaPath = "/Users/banacorn/Library/Haskell/ghc-7.8.2/lib/Agda-2.4.2/bin/agda"
logPath = "/Users/banacorn/Library/Haskell/var/agda-mode.log"
-- | helper function
paintOutput :: ByteString -> ByteString
paintOutput s = pack $ setSGRCode [SetColor Foreground Vivid Cyan] ++ unpack s ++ setSGRCode []
paintInput :: ByteString -> ByteString
paintInput s = pack $ setSGRCode [SetColor Foreground Vivid Green] ++ unpack s ++ setSGRCode []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment