aboutsummaryrefslogtreecommitdiff
path: root/lib/Daffm/Action/Core.hs
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-12-25 12:28:47 +0530
committerAkshay Nair <phenax5@gmail.com>2025-12-25 13:48:00 +0530
commitb4fe44842fb6914f9060b0d018de53983b0aab0b (patch)
treeee0d740e4500153c859d5fa02d4f6f8fa95404e6 /lib/Daffm/Action/Core.hs
parent2a2ee4a51e160406522c8c3136dfbc030ad3dcdd (diff)
downloaddaffm-b4fe44842fb6914f9060b0d018de53983b0aab0b.tar.gz
daffm-b4fe44842fb6914f9060b0d018de53983b0aab0b.zip
Add env vars for managing escape-free commands
Diffstat (limited to 'lib/Daffm/Action/Core.hs')
-rw-r--r--lib/Daffm/Action/Core.hs21
1 files changed, 17 insertions, 4 deletions
diff --git a/lib/Daffm/Action/Core.hs b/lib/Daffm/Action/Core.hs
index ba50638..1ff5737 100644
--- a/lib/Daffm/Action/Core.hs
+++ b/lib/Daffm/Action/Core.hs
@@ -49,7 +49,7 @@ openSelectedFile = do
Just (FileInfo {filePath, fileType = Directory}) -> loadDir filePath
Just (FileInfo {filePath, fileLinkType = Just Directory}) -> loadDir filePath
Just _ -> do
- opener <- gets (fromMaybe "echo '%F' | xargs -i xdg-open {}" . stateOpenerScript)
+ opener <- gets (fromMaybe "echo \"$files\" | xargs -i xdg-open {}" . stateOpenerScript)
cmdSubstitutions opener >>= suspendAndRunShellCommand False
Nothing -> pure ()
@@ -74,19 +74,32 @@ cmdSubstitutions cmd = gets (`substitute` cmd)
-- When exit code is non-zero, it will print it and prompt for key press regardless of waitForKey
suspendAndRunShellCommand :: Bool -> Text.Text -> AppEvent ()
suspendAndRunShellCommand waitForKey cmd = do
+ env <- gets geCommandEnvFromState
suspendAndResume' $ do
- shellCommand (Text.unpack cmd) Map.empty >>= \case
+ shellCommand (Text.unpack cmd) env >>= \case
Proc.ExitFailure code -> do
putStrLn $ "Process exited with " <> show code
putStrLn "Press any key to continue" >> void getChar
_ | 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
- currentEnv <- getEnvironment
+ cmdEnv <- (++ Map.toList env) <$> getEnvironment
Proc.withCreateProcess
- (Proc.shell cmd) {Proc.delegate_ctlc = True, Proc.env = Just $ currentEnv ++ Map.toList env}
+ (Proc.shell cmd) {Proc.delegate_ctlc = True, Proc.env = Just cmdEnv}
$ \_ _ _ p -> Proc.waitForProcess p
currentFile :: AppEvent (Maybe FileInfo)