1
- {-# LANGUAGE TemplateHaskell, OverloadedStrings #-}
1
+ {-# language
2
+ OverloadedStrings
3
+ #-}
2
4
3
5
module Rasa.Ext.Files
4
6
( files
@@ -8,59 +10,104 @@ module Rasa.Ext.Files
8
10
import qualified Data.Text.IO as TIO
9
11
import System.Environment
10
12
11
- import Data.Foldable
12
13
import Data.Typeable
13
14
import Data.Default
14
- import Data.Monoid
15
+ import Data.Maybe
15
16
16
- import Control.Monad.IO.Class
17
+ import Control.Monad
18
+ import Control.Monad.Trans
17
19
import qualified Yi.Rope as Y
18
20
19
21
import Rasa.Ext
20
22
import Rasa.Ext.Views
21
23
import Rasa.Ext.Cmd
22
- import Rasa.Ext.StatusBar
23
24
25
+ -- | Stores filename
24
26
data FileInfo =
25
- FileInfo (Maybe Y. YiString )
27
+ FileInfo (Maybe String )
26
28
deriving (Typeable , Show , Eq )
27
29
28
30
instance Default FileInfo where
29
31
def = FileInfo Nothing
30
32
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
31
59
files :: Action ()
32
60
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
35
67
loadFiles
36
- addCmd " save" $ focusDo_ . saveAs . Y. fromString
68
+ addCmd " save" $ focusDo_ . saveAs
37
69
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
43
85
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
46
88
89
+ -- | Save the buffer if we have a filename
47
90
save :: BufAction ()
48
91
save = do
49
92
FileInfo mName <- getBufExt
50
93
case mName of
51
94
Just fName -> saveAs fName
52
95
Nothing -> return ()
96
+ setFileStatus Clean
53
97
54
- setFilename :: Y. YiString -> BufAction ()
98
+ -- | Set the filename
99
+ setFilename :: String -> BufAction ()
55
100
setFilename fname = setBufExt $ FileInfo (Just fname)
56
101
57
- addFile :: Y. YiString -> Y. YiString -> Action ()
102
+ -- | Add a buffer for a file
103
+ addFile :: String -> Y. YiString -> Action ()
58
104
addFile fname txt = do
59
- newBuf <- newBuffer txt
105
+ newBuf <- addBuffer txt
60
106
bufDo_ newBuf (setFilename fname)
61
107
108
+ -- | Load files from command line
62
109
loadFiles :: Action ()
63
110
loadFiles = do
64
111
fileNames <- liftIO getArgs
65
112
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)
0 commit comments