aboutsummaryrefslogtreecommitdiff
path: root/lib/Daffm/Action/Core.hs
blob: 9fed688d40d27c2ab51f449ccf276193442438a3 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{-# 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, modify, put)
import Daffm.State (loadDirInAppState, toggleFileSelection)
import Daffm.Types (AppEvent, AppState (..), FileInfo (..), FileType (..))
import qualified Data.Set as Set
import System.Directory (getHomeDirectory)
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))

goHome :: AppEvent ()
goHome = do
  dir <- liftIO getHomeDirectory
  modifyM (liftIO . loadDirInAppState dir (takeDirectory dir))

openSelectedFile :: AppEvent ()
openSelectedFile = do
  fileM <- currentFile
  case fileM of
    Just file -> openFile file
    Nothing -> pure ()

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

currentFile :: AppEvent (Maybe FileInfo)
currentFile = do
  gets (fmap snd . L.listSelectedElement . stateFiles)

toggleCurrentFileSelection :: AppEvent ()
toggleCurrentFileSelection = do
  fileM <- currentFile
  case fileM of
    Just file -> modify $ toggleFileSelection (filePath file)
    Nothing -> pure ()
  moveCurrent 1

clearFileSelections :: AppEvent ()
clearFileSelections =
  modify $ \s -> s {stateFileSelections = Set.empty}

moveCurrent :: Int -> AppEvent ()
moveCurrent count = do
  files <- gets stateFiles
  modify $ \s -> s {stateFiles = L.listMoveBy count files}