aboutsummaryrefslogtreecommitdiff
path: root/lib/Daffm/Action/Core.hs
blob: 48792c47ebac49e9d8aebfccbfd7a64b6a9e87b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}

{-# HLINT ignore "Use for_" #-}
module Daffm.Action.Core where

import Brick (suspendAndResume')
import qualified Brick.Widgets.List as L
import Control.Monad.State (MonadIO (liftIO), MonadState, get, gets, put)
import Daffm.State (loadDirInAppState)
import Daffm.Types (AppEvent, AppState (..), FileInfo (..), FileType (..))
import Data.Vector ((!?))
import System.FilePath (takeDirectory)
import System.Process (callProcess)

modifyM :: (MonadState s m) => (s -> m s) -> m ()
modifyM f = get >>= f >>= put

reloadDir :: AppEvent ()
reloadDir = do
  AppState {stateCwd, stateParentDir} <- get
  modifyM (liftIO . loadDirInAppState stateCwd stateParentDir)

goBackToParentDir :: AppEvent ()
goBackToParentDir = do
  dir <- gets stateParentDir
  modifyM (liftIO . loadDirInAppState dir (takeDirectory dir))

openSelectedFile :: AppEvent ()
openSelectedFile = do
  appState <- get
  let indexM = L.listSelected $ stateFiles appState
  let files = L.listElements $ stateFiles appState
  case indexM >>= (files !?) of
    Just file -> openFile appState file
    Nothing -> pure ()
  pure ()

openFile :: AppState -> FileInfo -> AppEvent ()
openFile appState (FileInfo {filePath, fileType = Directory}) = do
  modifyM (liftIO . loadDirInAppState filePath (stateCwd appState))
openFile _appState (FileInfo {filePath, fileType}) = do
  suspendAndResume' $ do
    putStrLn $ "Opening " <> show fileType <> ": " <> filePath
    callProcess "nvim" [filePath]
  pure ()