diff --git a/rasa-ext-files/rasa-ext-files.cabal b/rasa-ext-files/rasa-ext-files.cabal index 5c5285e..a71fef6 100644 --- a/rasa-ext-files/rasa-ext-files.cabal +++ b/rasa-ext-files/rasa-ext-files.cabal @@ -17,6 +17,7 @@ library hs-source-dirs: src exposed-modules: Rasa.Ext.Files + Rasa.Ext.Files.Internal.Events build-depends: base >= 4.8 && < 5 , rasa @@ -27,6 +28,9 @@ library , text , mtl , yi-rope + , filepath + , directory + default-language: Haskell2010 default-extensions: diff --git a/rasa-ext-files/src/Rasa/Ext/Files.hs b/rasa-ext-files/src/Rasa/Ext/Files.hs index 72624b2..ba1e947 100644 --- a/rasa-ext-files/src/Rasa/Ext/Files.hs +++ b/rasa-ext-files/src/Rasa/Ext/Files.hs @@ -5,6 +5,10 @@ module Rasa.Ext.Files ( files , save + -- Events + , onFileLoaded + , dispatchFileLoaded + , FileLoaded(..), Extension(..) ) where import qualified Data.Text.IO as TIO @@ -22,15 +26,20 @@ import Rasa.Ext import Rasa.Ext.Views import Rasa.Ext.Cmd +import Rasa.Ext.Files.Internal.Events + +import qualified System.FilePath as FilePath +import System.Directory + -- | Stores filename data FileInfo = - FileInfo (Maybe String) + FileInfo (Maybe FilePath) deriving (Typeable, Show, Eq) instance Default FileInfo where def = FileInfo Nothing -type Filename = String +type Filename = FilePath -- | Stores File status; Clean means all changes are saved data FileStatus = @@ -96,18 +105,28 @@ save = do setFileStatus Clean -- | Set the filename -setFilename :: String -> BufAction () +setFilename :: FilePath -> BufAction () setFilename fname = setBufExt $ FileInfo (Just fname) -- | Add a buffer for a file -addFile :: String -> Y.YiString -> App () -addFile fname txt = do - newBuf <- addBuffer txt - bufDo_ newBuf (setFilename fname) +addFile :: FilePath -> Y.YiString -> App () +addFile fname txt = + do + newBuf <- addBuffer txt + dispatchFileLoaded $ extOf fname newBuf + bufDo_ newBuf (setFilename fname) + where + extOf f buf = FileLoaded (safeExt f) buf + safeExt f = case FilePath.takeExtensions f of + ext -> Extension $ ext + "" -> UnknownExtension -- | Load files from command line loadFiles :: App () loadFiles = do fileNames <- liftIO getArgs - fileTexts <- liftIO $ traverse TIO.readFile fileNames - mapM_ (uncurry addFile) $ zip fileNames (Y.fromText <$> fileTexts) + -- Silently filter out invalid filenames or unexisting files for now + validFileNames <- return $ filter FilePath.isValid fileNames + existingFileNames <- liftIO $ filterM doesFileExist validFileNames + fileTexts <- liftIO $ traverse TIO.readFile existingFileNames + mapM_ (uncurry addFile) $ zip existingFileNames (Y.fromText <$> fileTexts) diff --git a/rasa-ext-files/src/Rasa/Ext/Files/Internal/Events.hs b/rasa-ext-files/src/Rasa/Ext/Files/Internal/Events.hs new file mode 100644 index 0000000..ee1c8b6 --- /dev/null +++ b/rasa-ext-files/src/Rasa/Ext/Files/Internal/Events.hs @@ -0,0 +1,18 @@ +module Rasa.Ext.Files.Internal.Events where + +import Rasa.Ext + +import Control.Monad + +data Extension = Extension String | UnknownExtension deriving (Eq) + +data FileLoaded + = FileLoaded Extension BufRef + +-- | Trigger an 'App' on a 'Keypress' +onFileLoaded :: (FileLoaded -> App result) -> App ListenerId +onFileLoaded actionF = addListener (void <$> actionF) + +-- | Dispatch a 'Keypress' event. +dispatchFileLoaded :: FileLoaded -> App () +dispatchFileLoaded = dispatchEvent