Skip to content

Commit 5ef066b

Browse files
authored
Merge pull request #49 from ChrisPenner/eve
Use the Eve framework
2 parents 5ccc307 + 89fdf2f commit 5ef066b

File tree

38 files changed

+400
-1268
lines changed

38 files changed

+400
-1268
lines changed

docs/Building-An-Extension.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ main :: IO ()
7070
main = rasa $ do
7171
-- some plugins...
7272
-- Add the new action here!
73-
onInit helloWorld
73+
helloWorld
7474
```
7575

7676
Okay let's try again! `stack build && stack exec rasa` (you may want to alias
@@ -136,26 +136,26 @@ rasa/rasa-example-config/app/Main.hs:28:10: error:
136136
with actual type ‘Keypress -> Action ()’
137137
```
138138

139-
Hrmm, right! Now that we're listening for keypress events we don't want to use
140-
`onInit` anymore, Rasa provides a way to register listeners for different events;
141-
we'll learn how to listen for any arbitrary event later; but for now it's time for
142-
`onKeypress`!
139+
Hrmm, right! Now that we're listening for keypress events we need to set up an
140+
event-listener! Rasa provides a way to register listeners for different events;
141+
we'll learn how to listen for any arbitrary event later; but for now it's time
142+
for `onKeypress`!
143143

144144
```haskell
145145
onKeypress :: (Keypress -> Action ()) -> Action ListenerId
146146
```
147147

148148
So we've got our function from our event type (Keypress), so let's try
149-
registering it using `onKeypress`; we'll do all of this within `onInit` so the
150-
listener is registered when Rasa starts. The onKeypress function
151-
returns a reference to the newly created listener so that we could cancel the
152-
listener using `removeListener` later if we wanted to; but since we don't need
153-
to do that; we'll just ignore it.
149+
registering it using `onKeypress`; The onKeypress function returns a reference
150+
to the newly created listener so that we could cancel the listener using
151+
`removeListener` later if we wanted to; but since we don't need to do that;
152+
we'll just ignore it by using `void` from `Control.Monad`.
154153

155154
```haskell
155+
import Control.Monad
156156
main = rasa $ do
157157
-- other extensions
158-
onInit $ onKeypress helloWorld
158+
void $ onKeypress helloWorld
159159
```
160160

161161
Okay, let's build that and run it, now in a separate terminal we'll run
@@ -213,7 +213,7 @@ import qualified Yi.Rope as Y
213213
main = rasa $ do
214214
-- other extensions
215215
cursors
216-
onInit $ onKeypress copyPasta
216+
void $ onKeypress copyPasta
217217

218218
copyPasta :: Keypress -> Action ()
219219
copyPasta (Keypress 'y' _) = focusDo_ $ rangeDo_ copier
@@ -504,7 +504,7 @@ newBuf (BufAdded bufRef) = bufDo_ bufRef (addCopyListener copyListener)
504504

505505
main = rasa $ do
506506
-- other extensions
507-
onInit . onBufAdded $ newBuf
507+
onBufAdded_ $ newBuf
508508
```
509509

510510
Okay so this works; but there was a bit of boiler-plate to get it going!
@@ -599,7 +599,7 @@ newtype Copied = Copied String
599599
-- We've renamed things so we can export a single 'Action'
600600
-- that the user can embed in their config.
601601
copyPasta :: Action ()
602-
copyPasta = onInit $ onKeypress keyListener
602+
copyPasta = void $ onKeypress keyListener
603603

604604
keyListener :: Keypress -> Action ()
605605
keyListener (Keypress 'y' _) = do

rasa-example-config/rasa-example-config.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ executable rasa
2828
, data-default
2929
, yi-rope
3030
, mtl
31+
, eve
3132
default-language: Haskell2010
3233
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N
3334

rasa-ext-cmd/src/Rasa/Ext/Cmd.hs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{-# LANGUAGE TemplateHaskell #-}
2-
31
module Rasa.Ext.Cmd
42
( addCmd
53
, runCmd
@@ -13,7 +11,7 @@ import Data.Default
1311
import Data.Typeable
1412

1513
data Cmd =
16-
Cmd (Map String (String -> Action ()))
14+
Cmd (Map String (String -> App ()))
1715
deriving (Typeable)
1816

1917
instance Show Cmd where
@@ -23,15 +21,15 @@ instance Default Cmd where
2321
def = Cmd empty
2422

2523
-- It would be nice to make this a little more generic, but I'm not sure how right now.
26-
-- TODO try switching to T.Text -> (T.Text -> a) -> Action ()
27-
addCmd :: String -> (String -> Action ()) -> Action ()
24+
-- TODO try switching to T.Text -> (T.Text -> a) -> App ()
25+
addCmd :: String -> (String -> App ()) -> App ()
2826
addCmd alias mkEvent =
29-
overExt add
27+
stateLens %= add
3028
where add (Cmd commands) = Cmd $ commands & at alias ?~ mkEvent
3129

32-
runCmd :: String -> String -> Action ()
30+
runCmd :: String -> String -> App ()
3331
runCmd alias args = do
34-
Cmd commands <- getExt
32+
Cmd commands <- use stateLens
3533
let mCmd = commands^.at alias
3634
case mCmd of
3735
Just cmd -> cmd args

rasa-ext-cursors/src/Rasa/Ext/Cursors.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ import Rasa.Ext.Cursors.Internal.Base
2626
import Rasa.Ext.Cursors.Internal.Actions
2727

2828
-- | Registers listeners for the extension. The user should add this to their config.
29-
cursors :: Action ()
30-
cursors = onInit . onBufAdded $
29+
cursors :: App ()
30+
cursors = onBufAdded_ $
3131
\(BufAdded bufRef) -> bufDo_ bufRef setStyleProvider

rasa-ext-files/src/Rasa/Ext/Files.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ getFilename = do
5656
return filename
5757

5858
-- | Main export, use this in your Rasa config
59-
files :: Action ()
59+
files :: App ()
6060
files = do
6161
onEveryNewBuffer_ $ do
6262
void . onBufTextChanged $ bufferChanged
@@ -100,13 +100,13 @@ setFilename :: String -> BufAction ()
100100
setFilename fname = setBufExt $ FileInfo (Just fname)
101101

102102
-- | Add a buffer for a file
103-
addFile :: String -> Y.YiString -> Action ()
103+
addFile :: String -> Y.YiString -> App ()
104104
addFile fname txt = do
105105
newBuf <- addBuffer txt
106106
bufDo_ newBuf (setFilename fname)
107107

108108
-- | Load files from command line
109-
loadFiles :: Action ()
109+
loadFiles :: App ()
110110
loadFiles = do
111111
fileNames <- liftIO getArgs
112112
fileTexts <- liftIO $ traverse TIO.readFile fileNames

rasa-ext-logger/src/Rasa/Ext/Logger.hs

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ import Rasa.Ext
88

99
import Control.Monad.State
1010

11-
logger :: Action ()
11+
logger :: App ()
1212
logger = do
13-
onInit $ liftIO $ writeFile "logs.log" "Event Log\n"
14-
onEveryRender_ $ do
15-
ed <- getEditor
16-
liftIO $ appendFile "logs.log" (show ed)
13+
liftIO $ writeFile "logs.log" "Event Log\n"
14+
onEveryRender_ $ return ()
15+
-- ed <- getEditor
16+
-- liftIO $ appendFile "logs.log" (show ed)
1717

18-
logInfo :: String -> Action ()
18+
logInfo :: String -> App ()
1919
logInfo msg = liftIO $ appendFile "info.log" ("INFO: " ++ msg ++ "\n")
2020

21-
logError :: String -> Action ()
21+
logError :: String -> App ()
2222
logError msg = liftIO $ appendFile "error.log" ("ERROR: " ++ msg ++ "\n")

rasa-ext-slate/src/Rasa/Ext/Slate.hs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import Control.Monad.IO.Class
1515
-- > rasa $ do
1616
-- > slate
1717
-- > ...
18-
slate :: Action ()
18+
slate :: App ()
1919
slate = do
20-
onInit terminalEvents
20+
terminalEvents
2121
onEveryRender_ renderAll
2222
onExit shutdown
2323

2424
-- | Call vty shutdown procedure (if this doesn't happen the terminal ends up in strange states)
25-
shutdown :: Action ()
25+
shutdown :: App ()
2626
shutdown = do
2727
v <- getVty
2828
liftIO $ V.shutdown v

rasa-ext-slate/src/Rasa/Ext/Slate/Internal/Event.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Control.Monad
88
import qualified Graphics.Vty as V
99

1010
-- | Provides keypress events from the terminal, converted from Vty events.
11-
terminalEvents :: Action ()
11+
terminalEvents :: App ()
1212
terminalEvents = do
1313
v <- getVty
1414
asyncActionProvider $ getEvents v

rasa-ext-slate/src/Rasa/Ext/Slate/Internal/Render.hs

+6-6
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ import Control.Lens
2020
import Control.Monad.IO.Class
2121

2222
-- | Get the current terminal size.
23-
getSize :: Action (Width, Height)
23+
getSize :: App (Width, Height)
2424
getSize = do
2525
v <- getVty
2626
liftIO $ V.displayBounds $ V.outputIface v
2727

2828
-- | Render the Editor
29-
renderAll :: Action ()
29+
renderAll :: App ()
3030
renderAll = do
3131
(width, height) <- getSize
3232
mViews <- getViews
@@ -56,7 +56,7 @@ splitByRule (FromEnd amt) sz = (start, end)
5656
end = min sz amt
5757

5858
-- | Recursively render components of a Window to a 'V.Image' combining the results in the proper locations.
59-
renderWindow :: BiTree Split View -> Width -> Height -> Action V.Image
59+
renderWindow :: BiTree Split View -> Width -> Height -> App V.Image
6060
renderWindow = cata alg
6161
where
6262
mkBorder = V.charFill (V.defAttr `V.withForeColor` V.green)
@@ -88,7 +88,7 @@ type Left = V.Image
8888
type Right = V.Image
8989

9090
-- | Renders widgets to images
91-
widgetsToImages :: Width -> Height -> ScrollPos -> Widgets -> Action (Top, Bottom, Left, Right)
91+
widgetsToImages :: Width -> Height -> ScrollPos -> Widgets -> App (Top, Bottom, Left, Right)
9292
widgetsToImages width height scrollAmt widgets = do
9393
top <- renderHorBar width (widgets^.topBar)
9494
bottom <- renderHorBar width (widgets^.bottomBar)
@@ -101,7 +101,7 @@ widgetsToImages width height scrollAmt widgets = do
101101
renderVertBar h rs = V.resizeHeight h . V.horizCat <$> traverse (renderToImage 1 h scrollAmt) rs
102102

103103
-- | Render a given 'View' to a 'V.Image' given the context of the associated buffer and a size to render it in.
104-
renderView :: Width -> Height -> View -> Action V.Image
104+
renderView :: Width -> Height -> View -> App V.Image
105105
renderView width height vw = do
106106
widgets <- computeWidgets vw
107107
(top, bottom, left, right) <- widgetsToImages width height scrollAmt widgets
@@ -112,5 +112,5 @@ renderView width height vw = do
112112
where
113113
scrollAmt = vw^.scrollPos
114114

115-
renderToImage :: Renderable r => Width -> Height -> ScrollPos -> r -> Action V.Image
115+
renderToImage :: Renderable r => Width -> Height -> ScrollPos -> r -> App V.Image
116116
renderToImage w h scroll r = maybe V.emptyImage applyAttrs <$> render w h scroll r

rasa-ext-slate/src/Rasa/Ext/Slate/Internal/State.hs

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ module Rasa.Ext.Slate.Internal.State (getVty) where
22

33
import Rasa.Ext
44

5-
import Control.Monad.IO.Class
5+
import Control.Lens
6+
import Control.Monad.Trans
67
import qualified Graphics.Vty as V
78

89
-- | Store 'V.Vty' state globally
@@ -11,17 +12,17 @@ instance Show Slate where
1112
show _ = "Slate"
1213

1314
-- | V.Vty must be initialized, this takes IO to perform.
14-
initUi :: Action V.Vty
15+
initUi :: App V.Vty
1516
initUi = do
1617
cfg <- liftIO V.standardIOConfig
1718
v <- liftIO $ V.mkVty cfg
18-
setExt $ Just (Slate v)
19+
stateLens .= Just (Slate v)
1920
return v
2021

2122
-- | Gets vty by checking if it has been initialized yet, if not it runs the initialization.
22-
getVty :: Action V.Vty
23+
getVty :: App V.Vty
2324
getVty = do
24-
v <- getExt
25+
v <- use stateLens
2526
case v of
2627
Just (Slate v') -> return v'
2728
Nothing -> initUi

rasa-ext-views/src/Rasa/Ext/Views.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Rasa.Ext.Views
1919
, BiTreeF(..)
2020

2121

22-
-- * Provided Actions
22+
-- * ProvidedApps
2323
, A.rotate
2424
, A.closeInactive
2525
, A.focusViewLeft
@@ -74,7 +74,7 @@ import Rasa.Ext.Views.Internal.StatusBar
7474
import Rasa.Ext.Views.Internal.Actions as A
7575

7676
-- | Main export from the views extension, add this to your rasa config.
77-
viewports :: Action ()
77+
viewports :: App ()
7878
viewports = do
7979
onBufAdded_ A.addSplit
8080
lineNumbers

0 commit comments

Comments
 (0)