Skip to content

Commit 113cad6

Browse files
authored
Merge pull request #45 from ChrisPenner/add-renderable
Refactor Views rendering; add Widgets
2 parents 26ad934 + f39bed4 commit 113cad6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+816
-1772
lines changed

README.md

-4
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ You can find hackage documentation for rasa and some extensions here:
2222
- [rasa-ext-cursors](https://hackage.haskell.org/package/rasa-ext-cursors)
2323
- [rasa-ext-files](https://hackage.haskell.org/package/rasa-ext-files)
2424
- [rasa-ext-logger](https://hackage.haskell.org/package/rasa-ext-logger)
25-
- [rasa-ext-status-bar](https://hackage.haskell.org/package/rasa-ext-status-bar)
26-
- [rasa-ext-style](https://hackage.haskell.org/package/rasa-ext-style)
2725

2826
What people are saying
2927
----------------------
@@ -178,13 +176,11 @@ extra-deps:
178176
- rasa-0.1.0.0
179177
- rasa-ext-cursors-0.1.0.0
180178
- rasa-ext-logger-0.1.0.0
181-
- rasa-ext-status-bar-0.1.0.0
182179
- rasa-ext-vim-0.1.0.0
183180
- text-lens-0.1.0.0
184181
- rasa-ext-files-0.1.0.0
185182
- rasa-ext-cmd-0.1.0.0
186183
- rasa-ext-slate-0.1.0.0
187-
- rasa-ext-style-0.1.0.0
188184
- vty-5.14
189185
```
190186

cabal.project

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ packages:
88
./rasa-ext-files
99
./rasa-ext-logger
1010
./rasa-ext-slate
11-
./rasa-ext-status-bar
12-
./rasa-ext-style
1311
./rasa-ext-vim
1412
./rasa-example-config
1513
./text-lens

rasa-example-config/app/Main.hs

+4-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ module Main where
44
import Rasa (rasa)
55
import Rasa.Ext
66
import Rasa.Ext.Views
7-
import Rasa.Ext.Style
87
import Rasa.Ext.Vim
98
import Rasa.Ext.Files
10-
import Rasa.Ext.StatusBar
119
import Rasa.Ext.Logger
1210
import Rasa.Ext.Cursors
1311
import Rasa.Ext.Slate
1412

13+
-- import Control.Monad
14+
-- import Control.Monad.Trans
15+
1516
-- | This is the main of an executable that runs rasa with any extensions the
1617
-- user wants
1718
--
@@ -21,10 +22,8 @@ main :: IO ()
2122
main = rasa $ do
2223
viewports
2324
vim
24-
statusBar
2525
files
2626
cursors
2727
logger
2828
slate
29-
style
30-
onInit $ newBuffer "This is a buffer to get you started!\nYou can also pass command line args to rasa"
29+
afterInit $ addBuffer "This is a buffer to get you started!\nYou can also pass command line args to rasa"

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ executable rasa
2020
, rasa
2121
, rasa-ext-views
2222
, rasa-ext-vim
23-
, rasa-ext-style
2423
, rasa-ext-cursors
25-
, rasa-ext-status-bar
2624
, rasa-ext-logger
2725
, rasa-ext-files
2826
, rasa-ext-slate
2927
, lens
28+
, data-default
29+
, yi-rope
3030
, mtl
3131
default-language: Haskell2010
3232
ghc-options: -Wall -threaded -rtsopts -with-rtsopts=-N

rasa-ext-cursors/rasa-ext-cursors.cabal

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ library
2020
Rasa.Ext.Cursors.Internal.Base
2121
build-depends: base >= 4.8 && < 5
2222
, rasa
23-
, rasa-ext-style
2423
, text-lens
2524
, lens
2625
, mtl

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ import Rasa.Ext.Cursors.Internal.Actions
2727

2828
-- | Registers listeners for the extension. The user should add this to their config.
2929
cursors :: Action ()
30-
cursors = beforeEveryRender_ $ buffersDo_ displayRange
30+
cursors = onInit . onBufAdded $
31+
\(BufAdded bufRef) -> bufDo_ bufRef setStyleProvider

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ module Rasa.Ext.Cursors.Internal.Base
88
, setRanges
99
, overEachRange
1010
, addRange
11-
, displayRange
11+
, setStyleProvider
1212
) where
1313

1414

1515
import Rasa.Ext
16-
import Rasa.Ext.Style
1716

1817
import Control.Monad.State
1918
import Control.Lens
@@ -73,9 +72,9 @@ overEachRange f = rangeDo f >>= setRanges
7372
addRange :: CrdRange -> BufAction ()
7473
addRange r = overRanges (++[r])
7574

76-
-- | Sets style attributes to show a given range.
77-
displayRange :: BufAction ()
78-
displayRange = rangeDo_ setStyle
75+
-- | Adds cursor specific styles
76+
setStyleProvider :: BufAction ()
77+
setStyleProvider = void . addStyleProvider $ rangeDo setStyle
7978
where
80-
setStyle :: CrdRange -> BufAction ()
81-
setStyle r = addStyle r (flair ReverseVideo)
79+
setStyle :: CrdRange -> BufAction (Span CrdRange Style)
80+
setStyle r = return $ Span r (flair ReverseVideo)

rasa-ext-files/rasa-ext-files.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ library
2121
build-depends: base >= 4.8 && < 5
2222
, rasa
2323
, rasa-ext-views
24-
, rasa-ext-status-bar
2524
, rasa-ext-cmd
2625
, data-default
2726
, lens
2827
, text
28+
, mtl
2929
, yi-rope
3030
default-language: Haskell2010
3131

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

+67-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
{-# LANGUAGE TemplateHaskell, OverloadedStrings #-}
1+
{-# language
2+
OverloadedStrings
3+
#-}
24

35
module Rasa.Ext.Files
46
( files
@@ -8,59 +10,104 @@ module Rasa.Ext.Files
810
import qualified Data.Text.IO as TIO
911
import System.Environment
1012

11-
import Data.Foldable
1213
import Data.Typeable
1314
import Data.Default
14-
import Data.Monoid
15+
import Data.Maybe
1516

16-
import Control.Monad.IO.Class
17+
import Control.Monad
18+
import Control.Monad.Trans
1719
import qualified Yi.Rope as Y
1820

1921
import Rasa.Ext
2022
import Rasa.Ext.Views
2123
import Rasa.Ext.Cmd
22-
import Rasa.Ext.StatusBar
2324

25+
-- | Stores filename
2426
data FileInfo =
25-
FileInfo (Maybe Y.YiString)
27+
FileInfo (Maybe String)
2628
deriving (Typeable, Show, Eq)
2729

2830
instance Default FileInfo where
2931
def = FileInfo Nothing
3032

33+
type Filename = String
34+
35+
-- | Stores File status; Clean means all changes are saved
36+
data FileStatus =
37+
Dirty
38+
| Clean
39+
deriving Show
40+
41+
instance Default FileStatus where
42+
def = Clean
43+
44+
-- | Returns 'FileStatus' of current buffer
45+
getFileStatus :: BufAction FileStatus
46+
getFileStatus = getBufExt
47+
48+
-- | Sets 'FileStatus' of current buffer
49+
setFileStatus :: FileStatus -> BufAction ()
50+
setFileStatus = setBufExt
51+
52+
-- | Gets filename of current buffer
53+
getFilename :: BufAction (Maybe Filename)
54+
getFilename = do
55+
FileInfo filename <- getBufExt
56+
return filename
57+
58+
-- | Main export, use this in your Rasa config
3159
files :: Action ()
3260
files = do
33-
beforeEveryRender_ showFilename
34-
onInit $ do
61+
onEveryNewBuffer_ $ do
62+
void . onBufTextChanged $ bufferChanged
63+
void . addTopStatus $ fileStatus
64+
void . addTopStatus $ (fmap Y.fromString <$> getFilename)
65+
66+
afterInit $ do
3567
loadFiles
36-
addCmd "save" $ focusDo_ . saveAs . Y.fromString
68+
addCmd "save" $ focusDo_ . saveAs
3769

38-
showFilename :: Action ()
39-
showFilename = focusDo_ $ do
40-
FileInfo mName <- getBufExt
41-
traverse_ (leftStatus . disp) mName
42-
where disp name = "<" <> name <> ">"
70+
-- | Renders the current file status
71+
fileStatus :: BufAction (Maybe RenderInfo)
72+
fileStatus = do
73+
hasFilename <- isJust <$> getFilename
74+
status <- getFileStatus
75+
if hasFilename
76+
then return . Just $
77+
case status of
78+
Dirty -> styleText "" $ fg Red
79+
Clean -> styleText "" $ fg Green
80+
else return Nothing
81+
82+
-- | Keeps track of buffer status
83+
bufferChanged :: BufTextChanged -> BufAction ()
84+
bufferChanged _ = setFileStatus Dirty
4385

44-
saveAs :: Y.YiString -> BufAction ()
45-
saveAs fName = getText >>= liftIO . TIO.writeFile (Y.toString fName) . Y.toText
86+
saveAs :: String -> BufAction ()
87+
saveAs fName = getText >>= liftIO . TIO.writeFile fName . Y.toText
4688

89+
-- | Save the buffer if we have a filename
4790
save :: BufAction ()
4891
save = do
4992
FileInfo mName <- getBufExt
5093
case mName of
5194
Just fName -> saveAs fName
5295
Nothing -> return ()
96+
setFileStatus Clean
5397

54-
setFilename :: Y.YiString -> BufAction ()
98+
-- | Set the filename
99+
setFilename :: String -> BufAction ()
55100
setFilename fname = setBufExt $ FileInfo (Just fname)
56101

57-
addFile :: Y.YiString -> Y.YiString -> Action ()
102+
-- | Add a buffer for a file
103+
addFile :: String -> Y.YiString -> Action ()
58104
addFile fname txt = do
59-
newBuf <- newBuffer txt
105+
newBuf <- addBuffer txt
60106
bufDo_ newBuf (setFilename fname)
61107

108+
-- | Load files from command line
62109
loadFiles :: Action ()
63110
loadFiles = do
64111
fileNames <- liftIO getArgs
65112
fileTexts <- liftIO $ traverse TIO.readFile fileNames
66-
mapM_ (uncurry addFile) $ zip (Y.fromString <$> fileNames) (Y.fromText <$> fileTexts)
113+
mapM_ (uncurry addFile) $ zip fileNames (Y.fromText <$> fileTexts)

rasa-ext-slate/rasa-ext-slate.cabal

-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ library
2424

2525
build-depends: base >= 4.8 && < 5
2626
, rasa
27-
, rasa-ext-style
28-
, rasa-ext-status-bar
2927
, rasa-ext-views
3028
, recursion-schemes
3129
, text

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module Rasa.Ext.Slate (slate) where
22

33
import Rasa.Ext
4-
import Rasa.Ext.Slate.Internal.Render (render)
5-
import Rasa.Ext.Slate.Internal.Event (terminalEvents)
6-
import Rasa.Ext.Slate.Internal.State (getVty)
4+
import Rasa.Ext.Slate.Internal.Render
5+
import Rasa.Ext.Slate.Internal.Event
6+
import Rasa.Ext.Slate.Internal.State
77

88
import qualified Graphics.Vty as V
99
import Control.Monad.IO.Class
@@ -18,7 +18,7 @@ import Control.Monad.IO.Class
1818
slate :: Action ()
1919
slate = do
2020
onInit terminalEvents
21-
onEveryRender_ render
21+
onEveryRender_ renderAll
2222
onExit shutdown
2323

2424
-- | Call vty shutdown procedure (if this doesn't happen the terminal ends up in strange states)

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
module Rasa.Ext.Slate.Internal.Attributes where
33

44
import Rasa.Ext
5-
import Rasa.Ext.Style
65
import qualified Yi.Rope as Y
76
import qualified Graphics.Vty as V
87
import Data.Bifunctor
@@ -51,17 +50,18 @@ instance Monoid AttrMonoid where
5150
AttrMonoid v `mappend` AttrMonoid v' = AttrMonoid $ v `mappend` v'
5251

5352
-- | Apply a list of styles to the given text, resulting in a 'V.Image'.
54-
applyAttrs :: [Span CrdRange V.Attr] -> Y.YiString -> V.Image
55-
applyAttrs atts txt = textAndStylesToImage mergedSpans (padSpaces <$> Y.lines txt)
53+
applyAttrs :: RenderInfo -> V.Image
54+
applyAttrs (RenderInfo txt styles) = textAndStylesToImage mergedSpans (padSpaces <$> Y.lines txt)
5655
where mergedSpans = second getAttr <$> combineSpans (fmap AttrMonoid <$> atts)
5756
-- Newlines aren't rendered; so we replace them with spaces so they're selectable
5857
padSpaces = (`Y.append` " ")
58+
atts = second convertStyle <$> styles
5959

60+
-- | Makes and image from text and styles
6061
textAndStylesToImage :: [(Coord, V.Attr)] -> [Y.YiString] -> V.Image
61-
textAndStylesToImage atts lines' = vertCat $ (reset V.<|>) . uncurry attrLine <$> pairLines atts lines'
62+
textAndStylesToImage atts lines' = V.vertCat $ wrapResets . uncurry attrLine <$> pairLines atts lines'
6263
where
63-
vertCat = foldr ((V.<->) . (V.<|> reset)) V.emptyImage
64-
64+
wrapResets img = reset V.<|> img V.<|> reset
6565

6666
-- | Applies the list of attrs to the line and returns a 'V.Image'. It assumes that the list
6767
-- contains only 'Coord's on the same line (i.e. row == 0)

0 commit comments

Comments
 (0)