Skip to content

Instantly share code, notes, and snippets.

@aiya000
Created August 3, 2014 07:32
Show Gist options
  • Save aiya000/ed0f9d721c0cf789c7ae to your computer and use it in GitHub Desktop.
Save aiya000/ed0f9d721c0cf789c7ae to your computer and use it in GitHub Desktop.
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