Created
September 11, 2014 16:12
-
-
Save banacorn/3be9c34372ffaf77a81d to your computer and use it in GitHub Desktop.
eavesdropping on Agda and Emacs
This file contains 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
{-# 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