aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--docs/daffm.117
-rw-r--r--docs/daffm.md17
-rw-r--r--example-config.toml1
-rw-r--r--exe/Main.hs4
-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
8 files changed, 58 insertions, 23 deletions
diff --git a/docs/daffm.1 b/docs/daffm.1
index 25f26e8..200bb91 100644
--- a/docs/daffm.1
+++ b/docs/daffm.1
@@ -119,7 +119,7 @@ Same as shell but it adds a "Press any key to continue" prompt after the command
.SH COMMAND SUBSTITUIONS
-The following pattern (%,%d,%f,%s,%F,%S) are replaced with absolute paths
+The following pattern (%,%d,%f,%s,%F,%S,%0) are replaced with absolute paths
%: File under cursor
@@ -133,6 +133,21 @@ The following pattern (%,%d,%f,%s,%F,%S) are replaced with absolute paths
%F: Same as %S but if there are no selections, uses file under cursor
+%0: First path that was loaded when daffm was initialized
+
+
+
+.SH ENV VARIABLES
+In addition to substitutions, there are also some env variables available to access values those substitutions without escaping.
+
+$files: Same as %f but unescaped
+
+$selections: Same as %s but unescaped
+
+$init_path: Same as %0 but unescaped
+
+$cursor: Same as % but unescaped
+
.SH AUTHORS
Akshay Nair <phenax5@gmail.com>
diff --git a/docs/daffm.md b/docs/daffm.md
index d89a429..5fd319f 100644
--- a/docs/daffm.md
+++ b/docs/daffm.md
@@ -171,7 +171,7 @@ integrate with system utils for file management features.
# COMMAND SUBSTITUIONS
-The following pattern (%,%d,%f,%s,%F,%S) are replaced with absolute
+The following pattern (%,%d,%f,%s,%F,%S,%0) are replaced with absolute
paths
%: File under cursor
@@ -186,6 +186,21 @@ paths
%F: Same as %S but if there are no selections, uses file under cursor
+%0: First path that was loaded when daffm was initialized
+
+# ENV VARIABLES
+
+In addition to substitutions, there are also some env variables
+available to access values those substitutions without escaping.
+
+\$files: Same as %f but unescaped
+
+\$selections: Same as %s but unescaped
+
+\$init_path: Same as %0 but unescaped
+
+\$cursor: Same as % but unescaped
+
# AUTHORS
Akshay Nair \<phenax5@gmail.com\>
diff --git a/example-config.toml b/example-config.toml
index 93d8ada..96372ca 100644
--- a/example-config.toml
+++ b/example-config.toml
@@ -50,6 +50,7 @@ echo "$files" | sed 's|^|file://|' | xclip -selection clipboard -t text/uri-list
"""
# Mark directories
+"<space>0" = "cd %0"
"m1" = """eval echo "<daffm>map <space>1 cd %d" """
"m2" = """eval echo "<daffm>map <space>2 cd %d" """
"m3" = """eval echo "<daffm>map <space>3 cd %d" """
diff --git a/exe/Main.hs b/exe/Main.hs
index 6a85858..4b40d1d 100644
--- a/exe/Main.hs
+++ b/exe/Main.hs
@@ -17,5 +17,5 @@ evaluateArgs (Args {argsHelp = True}) = putStrLn Args.helpMenuContents
evaluateArgs (Args {argsDirOrFile, argsConfigFile}) = do
cwd <- getCurrentDirectory
config <- loadConfigFile argsConfigFile
- let dir = fromMaybe (Text.pack cwd) argsDirOrFile
- Daffm.initApp dir config
+ let initPath = fromMaybe (Text.pack cwd) argsDirOrFile
+ Daffm.initApp initPath config
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)