-- vim: sw=2: ts=2: expandtab: autoindent: -- module MemScript where -- module Main where import System.IO import Data.List import Control.Monad import Data.Maybe import System.Environment (getArgs) import System.Console.Readline main = do filenames <- getArgs if length filenames == 1 -- then memScript (head filenames) then initialize >> memScriptReadLine (head filenames) else putStrLn "Usage: memscript " memScriptReadLine filename = checkVersesByLine =<< getVersesFromFile filename getVersesFromFile = liftM lines . getFileContents getFileContents filename = hGetContents =<< openFile filename ReadMode checkVersesByLine = checkVersesByLineWith (readlineUTF8withAddHistory "% ") checkVersesByLineWith _ [] = return () checkVersesByLineWith readLine verses@(v:vs) = {- do mg <- readLine case mg of Just g -> checkOneVerseAndContinue g Nothing -> handleEOF -} maybe handleEOF checkOneLineAndContinue =<< readLine where handleEOF = printDiff (concat $ intersperse "\n< " verses) "" checkOneLineAndContinue g | v == g = checkVersesByLineWith readLine vs | otherwise = printDiff v g >> checkVersesByLineWith readLine verses readlineUTF8withAddHistory prompt = do ms <- readline prompt whenJust addHistory ms return ms whenJust = maybe (return ()) showDiff v g = concat $ intersperse "\n" ["===", "< "++v, "---", "> "++g, "==="] printDiff v g = putStrLn $ showDiff v g