Created
August 3, 2014 07:32
-
-
Save aiya000/ed0f9d721c0cf789c7ae to your computer and use it in GitHub Desktop.
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
import Control.Monad | |
import Control.Applicative | |
import Control.Exception | |
import System.IO | |
import Data.IORef | |
main = do | |
-- args <- getArgs | |
-- if length args < 1 then | |
-- putStrLn | |
lineEdit "read.txt" | |
prompt :: String -> IO String | |
prompt msg = do | |
putStr msg | |
hFlush stdout | |
getLine | |
type FileName = String | |
type Line = String | |
type Text = [Line] | |
top = fst | |
under = snd | |
type Posit = IORef Int | |
type EditState = (Posit, Text) | |
lineEdit fileName = do | |
text <- lines <$> readFile fileName | |
state <- newIORef 0 | |
let edit = (state, text) | |
forM_ [0,0..] $ \i -> do | |
op <- prompt ":" | |
command edit op | |
command :: EditState -> String -> IO () | |
command edit op = --{{{ | |
case op of | |
"p" -> do | |
posit <- readIORef (fst edit) | |
putStr " " | |
putStr $ show (posit + 1) | |
putStr "|" | |
putStrLn $ (snd edit) !! posit | |
"j" -> do | |
posit <- readIORef (fst edit) | |
if posit < (length $ snd edit) - 1 then | |
modifyIORef (fst edit) ((+) 1) | |
else return () | |
"k" -> do | |
posit <- readIORef (fst edit) | |
if posit > 0 then | |
modifyIORef (fst edit) ((-) 1) | |
else return () | |
"q" -> error "強制終了の合図です" | |
_ -> putStrLn "コマンドないよ" | |
--}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment