Browse code
Refactoring and clarifying
fiddlerwoaroof authored on 29/09/2015 04:44:55
Showing 1 changed files
Showing 1 changed files
... | ... |
@@ -18,11 +18,6 @@ import Data.STRef |
18 | 18 |
import qualified UI.HSCurses.Curses as HSCurses |
19 | 19 |
import qualified UI.HSCurses.CursesHelper as HSCursesHelper |
20 | 20 |
|
21 |
-a :: Integer |
|
22 |
-a = runST $ do |
|
23 |
- n <- newSTRef 0 |
|
24 |
- readSTRef n |
|
25 |
- |
|
26 | 21 |
mvWAddStr2 :: HSCurses.Window -> Int -> Int -> String -> IO () |
27 | 22 |
mvWAddStr2 w y x s = do |
28 | 23 |
(rows, cols) <- HSCurses.scrSize |
... | ... |
@@ -35,49 +30,45 @@ mvWAddStr2 w y x s = do |
35 | 30 |
dispatch :: HSCurses.Key -> IO () |
36 | 31 |
dispatch _ = return () |
37 | 32 |
|
33 |
+(--->) :: Monad m => (a -> m b) -> (a -> m b) -> a -> m b |
|
34 |
+(--->) = liftM2 (>>) |
|
35 |
+ |
|
36 |
+passTo :: a -> (a -> b) -> b |
|
37 |
+passTo = flip ($) |
|
38 |
+ |
|
39 |
+ |
|
38 | 40 |
mainLoop :: Int -> Handle -> Int -> Bool -> IO () |
39 |
-mainLoop lim filein cols infinitep = do |
|
40 |
- n <- stToIO $ newSTRef 1 |
|
41 |
- when True $ forever $ do |
|
42 |
- (x,nextX) <- initialize n |
|
43 |
- |
|
44 |
- let handleInputp = not infinitep && ( nextX `mod` lim == 0) |
|
45 |
- eofp <- atEnd |
|
46 |
- if eofp then threadDelay 100 |
|
47 |
- else do |
|
48 |
- displayNextLine x |
|
49 |
- handleInput handleInputp |
|
50 |
- |
|
51 |
- HSCurses.refresh |
|
41 |
+mainLoop lim filein cols infinitep = stToIO (newSTRef 1) >>= loop |
|
52 | 42 |
where |
53 |
- initialize n = do |
|
54 |
- let getX = stToIO $ readSTRef n |
|
55 |
- x <- getX |
|
56 |
- stToIO $ writeSTRef n (x+1) |
|
57 |
- nextX <- getX |
|
58 |
- return (x,nextX) |
|
59 |
- |
|
60 |
- atEnd = hIsEOF filein |
|
61 |
- getLineFromFile = hGetLine filein |
|
62 |
- showDivider x lim = |
|
63 |
- moveAndAddString ( ((x+1)`mod`lim) - 1) 0 |
|
64 |
- handleInput handleInputp = when handleInputp $ HSCurses.getCh >>= dispatch |
|
65 |
- moveAndAddString = mvWAddStr2 HSCurses.stdScr |
|
43 |
+ loop = discardResult . forever . wrapCode step HSCurses.refresh . stToIO . initialize |
|
66 | 44 |
|
67 |
- displayNextLine x = do |
|
68 |
- let xModLim = x `mod` lim - 1 |
|
69 |
- let marker = "--- break ---" |
|
70 |
- let divider = marker ++ replicate (cols - length marker + 1) '-' :: String |
|
71 |
- let clearLine x cols line = moveAndAddString xModLim 0 $ replicate (cols+1) ' ' -- overwrite line |
|
72 |
- let showLine x cols line = moveAndAddString xModLim 0 $ show x ++ line -- show new line |
|
45 |
+ initialize n = readSTRef n >>= incSTRef n `keepOldBinding` addSTRefToTuple n |
|
73 | 46 |
|
74 |
- line <- getLineFromFile |
|
75 |
- let line = ' ':line |
|
47 |
+ step (x,nextX) = discardResult $ do |
|
48 |
+ let handleInputp = not infinitep && ( nextX `mod` lim == 0) |
|
49 |
+ eofp <- atEnd |
|
50 |
+ if eofp then threadDelay 100 |
|
51 |
+ else do |
|
52 |
+ line <- hGetLine filein |
|
53 |
+ x `passTo` (clearLine ---> showLine line ---> showDivider) |
|
54 |
+ handleInput handleInputp |
|
76 | 55 |
|
77 |
- clearLine x cols line |
|
78 |
- showLine x cols line |
|
79 |
- showDivider x lim divider |
|
56 |
+ clearLine x = moveAndAddString (getLinePos x) 0 $ replicate (cols+1) ' ' -- overwrite line |
|
57 |
+ showLine line x = moveAndAddString (getLinePos x) 0 $ show x ++ (' ':line) -- show new line |
|
58 |
+ showDivider x = moveAndAddString ( getLinePos (x+1) ) 0 div |
|
80 | 59 |
|
60 |
+ addSTRefToTuple n = flippedLiftM (readSTRef n) . (,) |
|
61 |
+ atEnd = hIsEOF filein |
|
62 |
+ discardResult = when True |
|
63 |
+ div = marker ++ replicate (cols - length marker + 1) '-' |
|
64 |
+ flippedLiftM = flip liftM |
|
65 |
+ getLinePos = subtract 1 . (`mod` lim) |
|
66 |
+ handleInput handleInputp = when handleInputp $ HSCurses.getCh >>= dispatch |
|
67 |
+ incSTRef n = writeSTRef n . (+ 1) |
|
68 |
+ keepOldBinding = liftM2 (>>) |
|
69 |
+ marker = "--- break ---" |
|
70 |
+ moveAndAddString = mvWAddStr2 HSCurses.stdScr |
|
71 |
+ wrapCode run finalize init = (init >>= run) >> finalize |
|
81 | 72 |
|
82 | 73 |
allocate :: IO () |
83 | 74 |
allocate = do |
... | ... |
@@ -140,18 +131,3 @@ main = do |
140 | 131 |
|
141 | 132 |
optionHandler :: MyOptions -> IO () |
142 | 133 |
optionHandler opts@MyOptions{..} = Exception.bracket_ allocate deallocate (work encoding run infinite stream) |
143 |
- |
|
144 |
- |