diff options
| author | Akshay Nair <phenax5@gmail.com> | 2026-06-12 00:40:39 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2026-06-12 00:47:18 +0530 |
| commit | 8828075fa3a25f37ec79507c0cf5179b76f35948 (patch) | |
| tree | 86646a646f423343b016f5d353d8384802e8b8b0 /lib/Daffm/Action/Core.hs | |
| parent | ed728dfdf33ad58113b7dd7b4402d3b15dca91d8 (diff) | |
| download | daffm-8828075fa3a25f37ec79507c0cf5179b76f35948.tar.gz daffm-8828075fa3a25f37ec79507c0cf5179b76f35948.zip | |
Add init path substitution
Diffstat (limited to 'lib/Daffm/Action/Core.hs')
| -rw-r--r-- | lib/Daffm/Action/Core.hs | 28 |
1 files changed, 15 insertions, 13 deletions
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 |
