aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--lib/Daffm.hs4
-rw-r--r--lib/Daffm/Action/Core.hs28
-rw-r--r--lib/Daffm/State.hs7
-rw-r--r--lib/Daffm/Types.hs3
4 files changed, 23 insertions, 19 deletions
diff --git a/lib/Daffm.hs b/lib/Daffm.hs
index a4ced9f..a9b1cd5 100644
--- a/lib/Daffm.hs
+++ b/lib/Daffm.hs
@@ -19,6 +19,6 @@ app =
}
initApp :: FilePathText -> Configuration -> IO ()
-initApp dir config = do
- initialState <- loadDirToState dir $ mkEmptyAppState config
+initApp initPath config = do
+ initialState <- loadDirToState initPath $ mkEmptyAppState config initPath
void $ B.defaultMain app initialState
diff --git a/lib/Daffm/Action/Core.hs b/lib/Daffm/Action/Core.hs
index 1ff5737..0fc9d57 100644
--- a/lib/Daffm/Action/Core.hs
+++ b/lib/Daffm/Action/Core.hs
@@ -57,18 +57,32 @@ cmdSubstitutions :: Text.Text -> AppEvent Text.Text
cmdSubstitutions cmd = gets (`substitute` cmd)
where
escape = (\s -> "'" <> s <> "'") . Text.replace "'" "'\\''"
- substitute (AppState {stateFiles, stateCwd, stateFileSelections}) =
+ substitute (AppState {stateFiles, stateCwd, stateFileSelections, stateInitPath}) =
Text.replace "%" (escape cursorFile)
. Text.replace "%d" (escape stateCwd)
. Text.replace "%s" (Text.unwords $ map escape selections)
. Text.replace "%S" (Text.dropWhileEnd (== '\n') $ Text.unlines selections)
. Text.replace "%f" (Text.unwords $ map escape selectionsOrCursor)
. Text.replace "%F" (Text.dropWhileEnd (== '\n') $ Text.unlines selectionsOrCursor)
+ . Text.replace "%0" (escape stateInitPath)
where
cursorFile = maybe "" (filePath . snd) . L.listSelectedElement $ stateFiles
selections = Set.elems stateFileSelections
selectionsOrCursor = if Set.null stateFileSelections then [cursorFile] else selections
+geCommandEnvFromState :: AppState -> Map.Map String String
+geCommandEnvFromState (AppState {stateFileSelections, stateFiles, stateInitPath}) = do
+ Map.fromList
+ [ ("files", Text.unpack $ Text.dropWhileEnd (== '\n') $ Text.unlines selectionsOrCursor),
+ ("selections", Text.unpack $ Text.dropWhileEnd (== '\n') $ Text.unlines selections),
+ ("init_path", Text.unpack $ stateInitPath),
+ ("cursor", Text.unpack cursorFile)
+ ]
+ where
+ cursorFile = maybe "" (filePath . snd) . L.listSelectedElement $ stateFiles
+ selections = Set.elems stateFileSelections
+ selectionsOrCursor = if Set.null stateFileSelections then [cursorFile] else selections
+
-- Suspend tui and run shell command
-- When waitForKey is true, it will prompt for a key press on success
-- When exit code is non-zero, it will print it and prompt for key press regardless of waitForKey
@@ -83,18 +97,6 @@ suspendAndRunShellCommand waitForKey cmd = do
_ | waitForKey -> putStrLn "Press any key to continue" >> void getChar
_ -> pure ()
-geCommandEnvFromState :: AppState -> Map.Map String String
-geCommandEnvFromState (AppState {stateFileSelections, stateFiles}) = do
- Map.fromList
- [ ("files", Text.unpack $ Text.dropWhileEnd (== '\n') $ Text.unlines selectionsOrCursor),
- ("selections", Text.unpack $ Text.dropWhileEnd (== '\n') $ Text.unlines selections),
- ("cursor", Text.unpack cursorFile)
- ]
- where
- cursorFile = maybe "" (filePath . snd) . L.listSelectedElement $ stateFiles
- selections = Set.elems stateFileSelections
- selectionsOrCursor = if Set.null stateFileSelections then [cursorFile] else selections
-
shellCommand :: String -> Map.Map String String -> IO Proc.ExitCode
shellCommand cmd env = do
cmdEnv <- (++ Map.toList env) <$> getEnvironment
diff --git a/lib/Daffm/State.hs b/lib/Daffm/State.hs
index fca72b6..c86df36 100644
--- a/lib/Daffm/State.hs
+++ b/lib/Daffm/State.hs
@@ -27,8 +27,8 @@ import qualified System.PosixCompat as Posix
mkEditor :: (Zipper.GenericTextZipper a) => a -> Editor.Editor a FocusTarget
mkEditor = Editor.editor FocusCmdline (Just 1)
-mkEmptyAppState :: Configuration -> AppState
-mkEmptyAppState config =
+mkEmptyAppState :: Configuration -> FilePathText -> AppState
+mkEmptyAppState config initPath =
applyConfigToState config $
AppState
{ stateFiles = L.list FocusMain (Vec.fromList []) 1,
@@ -44,7 +44,8 @@ mkEmptyAppState config =
stateSearchTerm = Nothing,
stateSearchMatches = Vec.empty,
stateCustomCommands = Map.empty,
- stateSearchIndex = 0
+ stateSearchIndex = 0,
+ stateInitPath = initPath
}
applyConfigToState :: Configuration -> AppState -> AppState
diff --git a/lib/Daffm/Types.hs b/lib/Daffm/Types.hs
index a1095d4..f806253 100644
--- a/lib/Daffm/Types.hs
+++ b/lib/Daffm/Types.hs
@@ -53,7 +53,8 @@ data AppState = AppState
stateOpenerScript :: Maybe Text.Text,
stateSearchTerm :: Maybe Text.Text,
stateSearchMatches :: Vec.Vector Int,
- stateSearchIndex :: Int
+ stateSearchIndex :: Int,
+ stateInitPath :: FilePathText
}
deriving (Show)